Code ví dụ jOOQ, generate/tạo class từ table, database

Code ví dụ jOOQ, generate/tạo class từ table, database

Trong ví dụ này mình sẽ thực hiện tạo maven project, kết nối tới database mySQL để tạo ra các class entity tương ứng với các bảng trong database.

Code ví dụ jOOQ, generate/tạo class từ table, database

B1: Tạo database

Đầu tiên mình tạo database library với table auto như sau:

CREATE DATABASE `library`;

USE `library`;

CREATE TABLE `author` (
  `id` int NOT NULL,
  `first_name` varchar(255) DEFAULT NULL,
  `last_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

Code ví dụ jOOQ, generate/tạo class từ table, database

Kết quả:

Code ví dụ jOOQ, generate/tạo class từ table, database

B2: Tạo maven project và add các thư viện liên quan:

 

Trong file pom.xml mình sử dụng các thư viện sau:

Thư viện JDBC connection:

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>8.0.16</version>
</dependency>

Thư viện cho jOOQ:

<dependency>
  <groupId>org.jooq</groupId>
  <artifactId>jooq</artifactId>
  <version>3.11.11</version>
</dependency>
<dependency>
  <groupId>org.jooq</groupId>
  <artifactId>jooq-meta</artifactId>
  <version>3.11.11</version>
</dependency>
<dependency>
  <groupId>org.jooq</groupId>
  <artifactId>jooq-codegen</artifactId>
  <version>3.11.11</version>
</dependency>

Các bạn lưu ý là jOOQ có cả bản trả phí (commercial) và bản miễn phí (open source). Phần thư viện, mình sử dụng open source nhé.

Khai báo plugin jOOQ:

<build>
    <plugins>
      <plugin>
        <groupId>org.jooq</groupId>
        <artifactId>jooq-codegen-maven</artifactId>
        <executions>
          <execution>
            <id>jooq</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>generate</goal>
            </goals>
            <configuration>
              <skip>${maven.generate-sources.skip}</skip>
            </configuration>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
          </dependency>
        </dependencies>
        <configuration>
          <jdbc>
            <driver>com.mysql.cj.jdbc.Driver</driver>
            <url>jdbc:mysql://localhost:3306/library</url>
            <user>root</user>
            <password>admin1234</password>
          </jdbc>
          <generator>
            <name>org.jooq.codegen.JavaGenerator</name>
            <database>
              <name>org.jooq.meta.mysql.MySQLDatabase</name>
              <includes>.*</includes>
              <excludes />
              <inputSchema>library</inputSchema>
            </database>
            <target>
              <packageName>stackjava.com.jooq</packageName>
              <directory>src/main/java</directory>
            </target>
          </generator>
        </configuration>
      </plugin>
    </plugins>
  </build>

Trong đó:

  • Thẻ <jdbc> chứa thông tin kết nối database
  • Thẻ <generator> cấu hình việc generate class như sẽ generate class từ schema nào, table và generate ra package nào. (thẻ <includes> để khai báo những table được generate, thẻ <exclude> khai báo các table bị bỏ qua khi generate class. Trong ví dụ này mình đang để là generate class cho tất cả table)

Tiếp theo chạy lệnh sau để generate class theo database:

mvn generate-sources

Kết quả:

[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for stackjava.com:jOOQDemo:jar:0.0
.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.jooq:jooq-codegen-maven is missing. @ line 41, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] -----------------------< stackjava.com:jOOQDemo >-----------------------
[INFO] Building jOOQDemo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- jooq-codegen-maven:3.11.11:generate (jooq) @ jOOQDemo ---
[INFO] No <inputCatalog/> was provided. Generating ALL available catalogs instead.
[INFO] License parameters
[INFO] ----------------------------------------------------------
[INFO]   Thank you for using jOOQ and jOOQ's code generator
[INFO]
[INFO] Database parameters
[INFO] ----------------------------------------------------------
[INFO]   dialect                : MYSQL
[INFO]   URL                    : jdbc:mysql://localhost:3306/library
[INFO]   target dir             : C:\app\workspace\jOOQDemo\src\main\java
[INFO]   target package         : stackjava.com.jooq
[INFO]   includes               : [.*]
[INFO]   excludes               : []
[INFO]   includeExcludeColumns  : false
[INFO] ----------------------------------------------------------
[INFO]
[INFO] JavaGenerator parameters
[INFO] ----------------------------------------------------------
[INFO]   annotations (generated): true
[INFO]   annotations (JPA: any) : false
[INFO]   annotations (JPA: version):
[INFO]   annotations (validation): false
[INFO]   comments               : true
[INFO]   comments on attributes : true
[INFO]   comments on catalogs   : true
[INFO]   comments on columns    : true
[INFO]   comments on keys       : true
[INFO]   comments on links      : true
[INFO]   comments on packages   : true
[INFO]   comments on parameters : true
[INFO]   comments on queues     : true
[INFO]   comments on routines   : true
[INFO]   comments on schemas    : true
[INFO]   comments on sequences  : true
[INFO]   comments on tables     : true
[INFO]   comments on udts       : true
[INFO]   daos                   : false
[INFO]   deprecated code        : true
[INFO]   global references (any): true
[INFO]   global references (catalogs): true
[INFO]   global references (keys): true
[INFO]   global references (links): true
[INFO]   global references (queues): true
[INFO]   global references (routines): true
[INFO]   global references (schemas): true
[INFO]   global references (sequences): true
[INFO]   global references (tables): true
[INFO]   global references (udts): true
[INFO]   indexes                : true
[INFO]   instance fields        : true
[INFO]   interfaces             : false
[INFO]   interfaces (immutable) : false
[INFO]   javadoc                : true
[INFO]   keys                   : true
[INFO]   links                  : true
[INFO]   pojos                  : false
[INFO]   pojos (immutable)      : false
[INFO]   queues                 : true
[INFO]   records                : true
[INFO]   routines               : true
[INFO]   sequences              : true
[INFO]   table-valued functions : true
[INFO]   tables                 : true
[INFO]   udts                   : true
[INFO]   relations              : true
[INFO] ----------------------------------------------------------
[INFO]
[INFO] Generation remarks
[INFO] ----------------------------------------------------------
[INFO]
[INFO] ----------------------------------------------------------
[INFO] Generating catalogs      : Total: 1
[INFO]

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@  @@        @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@        @@@@@@@@@@
@@@@@@@@@@@@@@@@  @@  @@    @@@@@@@@@@
@@@@@@@@@@  @@@@  @@  @@    @@@@@@@@@@
@@@@@@@@@@        @@        @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@        @@        @@@@@@@@@@
@@@@@@@@@@    @@  @@  @@@@  @@@@@@@@@@
@@@@@@@@@@    @@  @@  @@@@  @@@@@@@@@@
@@@@@@@@@@        @@  @  @  @@@@@@@@@@
@@@@@@@@@@        @@        @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@  @@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  Thank you for using jOOQ 3.11.11

[INFO] ARRAYs fetched           : 0 (0 included, 0 excluded)
[INFO] Enums fetched            : 0 (0 included, 0 excluded)
[INFO] Packages fetched         : 0 (0 included, 0 excluded)
[INFO] Routines fetched         : 0 (0 included, 0 excluded)
[INFO] Tables fetched           : 1 (1 included, 0 excluded)
[INFO] No schema version is applied for catalog . Regenerating.
[INFO]
[INFO] Generating catalog       : DefaultCatalog.java
[INFO] ==========================================================
[INFO] Generating schemata      : Total: 1
[INFO] No schema version is applied for schema library. Regenerating.
[INFO] Generating schema        : Library.java
[INFO] ----------------------------------------------------------
[INFO] Sequences fetched        : 0 (0 included, 0 excluded)
[INFO] UDTs fetched             : 0 (0 included, 0 excluded)
[INFO] Generating tables
[INFO] Synthetic primary keys   : 0 (0 included, 0 excluded)
[INFO] Overriding primary keys  : 1 (0 included, 1 excluded)
[INFO] Generating table         : Author.java [input=author, output=author, pk=KEY_author_PRIMARY]
[INFO] Indexes fetched          : 1 (1 included, 0 excluded)
[INFO] Tables generated         : Total: 687.43ms
[INFO] Generating table references
[INFO] Table refs generated     : Total: 695.327ms, +7.897ms
[INFO] Generating Keys
[INFO] Keys generated           : Total: 705.92ms, +10.592ms
[INFO] Generating Indexes
[INFO] Indexes generated        : Total: 714.565ms, +8.645ms
[INFO] Generating table records
[INFO] Generating record        : AuthorRecord.java
[INFO] Table records generated  : Total: 732.861ms, +18.296ms
[INFO] Domains fetched          : 0 (0 included, 0 excluded)
[INFO] Generation finished: library: Total: 736.748ms, +3.886ms
[INFO]
[INFO] Removing excess files
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.110 s
[INFO] Finished at: 2019-05-31T00:13:10+07:00
[INFO] ------------------------------------------------------------------------

Đây là project sau khi các class được generate

  • package stackjava.com.joop chứa các class mô tả về database
  • package stackjava.com.jooq.table chứa các class mô tả table
  • package stackjava.com.jooq.tables.records mô tả các bản ghi (các column) của table.

Code ví dụ jOOQ, generate/tạo class từ table, database

 

 

Okay, Done!

Download code ví dụ trên tại đây.

References:

http://www.jooq.org/doc/3.11/manual/getting-started/tutorials/jooq-in-7-steps/

stackjava.com