Searching...
Saturday, 21 December 2013

How to generate jOOQ code using eclipse ?

12:39

jOOQ generates Java code from your database and lets you build typesafe SQL queries through its fluent API. In this example we will see how to generate code using eclipse and MySql

1. Create a Java project and add below jars in the class path

1. jooq-xxxx.jar –This is the core library for jOOQ

2. jooq-meta-xxxx.jar- This jar includes utility class to navigate database schema for code generation

3. jooq-codegen-xxxx.jar- Utility to generate code for database schema

All these jars are available in http://www.jooq.org/download/

4. Java mysql connector jar- Available in http://www.mysql.com/products/connector/

2. Create a database schema and table in MySql database

                     image

3. Create below xml under project src folder (student.xml)

In this xml file we need to mention database connection and schema details to generate code

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.2.0.xsd">
  <jdbc>
    <driver>com.mysql.jdbc.Driver</driver>
    <url>jdbc:mysql://localhost:3306/studentdatabase</url>
    <user>root</user>
    <password>root</password>
  </jdbc>
  <generator>
    <name>org.jooq.util.DefaultGenerator</name>
    <database>
      <name>org.jooq.util.mysql.MySQLDatabase</name>
      <inputSchema>studentdatabase</inputSchema>
      <excludes></excludes>
    </database>
    <target>
      <packageName>com.pretech.jooq</packageName>
      <directory>D:\Projects\stsworkspace\JooqStudentExample\src</directory>
    </target>
  </generator>
</configuration>

4. Eclipse run configuration


Right click on the project and select ‘Run As’ Run configuration and Select main class org.jooq.util.GenerationTool


image l


Argument parameter should be the xml file which we created in step 3


image


5. Run it

Run the org.jooq.util.GenerationTool and we can see the codes are generated in the target directory which we mentioned in the student.xml file
Final structure of the project
image

0 comments: