6 months ago

以前要 reverse engineering 來做 code generation 大概都會用 Hibernate Tools 來做

但是實在太囉唆啦....
又要依附在IDE 又要先設定 JDBC 又要建立專案 又要產生 XML 配置 還會生成 SessionFactory 但最後這些通通用不到....只留下要用的 Entity 而已.

自從用過 MyBatis Generator 可參考 mybatis 3 整合開發
整理來說就只需要產生器的相關 Jar 檔 跟一個 XML 配置檔, 就可以產生需要的 資料庫對應的 Entity , 而且別人有改動到表格的時候, 靠這個 XML 他就可以產生異動後的 Entity , 而不用搞上面那一堆配置, 這不是輕鬆很多嗎?

而且之後大概都會以 Spring Data Jpa 來操作為主, MyBatis 是不錯用, 但是 Spring Data 更方便啊!!!!!! 可以擴展成 Spring Data Rest 還可以再加上 HATEOAS 這種數據關聯性, 直接少寫一堆 Crud.

在尋尋覓覓之後, 終於找到就是它 JOOQ !! 喔!! 是 jOOQ's code generator 啦
我沒有要用 JOOQ...XD

下面是官網介紹
Configure jOOQ's code generator

可以看到也是很間單 XML Jar檔 JDBCJar檔 就沒了, 你需要的都在這了XD
jooq-3.10.5.jar
jooq-meta-3.10.5.jar
jooq-codegen-3.10.5.jar
mysql-connector-java-5.1.45.jar
library.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns="http://www.jooq.org/xsd/jooq-codegen-3.10.0.xsd">
   <!-- Configure the database connection here -->
   <jdbc>
      <driver>com.mysql.jdbc.Driver</driver>
      <url>jdbc:mysql://db01.mysqldb.cloudapi.tw:3306</url>
      <user>UserName</user>
      <password>IAmPassWord</password>
   </jdbc>
   <generator>
      <!-- The default code generator. You can override this one, to generate your own code style.
         Supported generators:
         - org.jooq.util.JavaGenerator
         - org.jooq.util.ScalaGenerator
         Defaults to org.jooq.util.JavaGenerator -->
      <name>org.jooq.util.JavaGenerator</name>
      <database>
         <!-- The database type. The format here is:
           org.util.[database].[database]Database -->
         <name>org.jooq.util.mysql.MySQLDatabase</name>
         <!-- The database schema (or in the absence of schema support, in your RDBMS this
           can be the owner, user, database name) to be generated -->
         <inputSchema>gym_member</inputSchema>
         <!-- All elements that are generated from your schema
           (A Java regular expression. Use the pipe to separate several expressions)
           Watch out for case-sensitivity. Depending on your database, this might be important! -->
         <includes>importusers|importfinance</includes>
         <!-- All elements that are excluded from your schema
           (A Java regular expression. Use the pipe to separate several expressions).
           Excludes match before includes, i.e. excludes have a higher priority -->
         <excludes />
      </database>
      <target>
         <!-- The destination package of your generated classes (within the destination directory) -->
         <packageName>test.generated</packageName>
         <!-- The destination directory of your generated classes. Using Maven directory layout here -->
         <directory>/Users/sam/jooqtest/MySQLTest/src/main/java</directory>
      </target>
      <generate>
         <records>false</records>
         <pojos>true</pojos>
         <jpaAnnotations>true</jpaAnnotations>
         <validationAnnotations>true</validationAnnotations>
         <pojosToString>false</pojosToString>
         <javaTimeTypes>true</javaTimeTypes>
         <!-- 使用 Java 8 的 LocalDateTime -->
      </generate>
   </generator>
</configuration>

includes 說明一下好了 預設是所有 Table 啦
但你只有一部分需要生成的話可以用 正則式來比對或是像我比較少表格就直接用 | 串接
pojos 這個要設定為 true 不然預設他只會生成 JOOQ 的操作物件而已, pojos 才是我們要的 Entity 物件
除 pojos 外 還要再加上 jpaAnnotations = true
validationAnnotations 看你需不需要囉
javaTimeTypes 就是要不要用 Java 8 的 LocalDateTime 還是傳統的 Timestamp 物件

好了

生成過程

zhushanglide-MacBook-Air:jooqtest sam$ java -classpath jooq-3.10.5.jar:jooq-meta-3.10.5.jar:jooq-codegen-3.10.5.jar:mysql-connector-java-5.1.45.jar:. org.jooq.util.GenerationTool library.xml
三月 19, 2018 2:57:24 下午 org.jooq.tools.JooqLogger info
資訊: Initialising properties  : library.xml
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: No <inputCatalog/> was provided. Generating ALL available catalogs instead.
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: License parameters
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: ----------------------------------------------------------
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   Thank you for using jOOQ and jOOQ's code generator
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: Database parameters
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: ----------------------------------------------------------
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   dialect                : MYSQL
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   URL                    : jdbc:mysql://yddb01.mysqldb.chinacloudapi.cn:3306
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   target dir             : /Users/sam/jooqtest/MySQLTest/src/main/java
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   target package         : test.generated
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   includes               : [importusers|importfinance]
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   excludes               : []
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   includeExcludeColumns  : false
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: ----------------------------------------------------------
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: JavaGenerator parameters
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: ----------------------------------------------------------
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   strategy               : class org.jooq.util.DefaultGeneratorStrategy
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   deprecated             : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   generated annotation   : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   JPA annotations        : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   validation annotations : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   instance fields        : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   sequences              : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   udts                   : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   routines               : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   tables                 : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   records                : false
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   pojos                  : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   immutable pojos        : false
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   interfaces             : false
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   immutable interfaces   : false
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   daos                   : false
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   indexes                : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   relations              : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   table-valued functions : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:   global references      : true
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: ----------------------------------------------------------
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: Generation remarks
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: ----------------------------------------------------------
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊:
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: ----------------------------------------------------------
三月 19, 2018 2:57:25 下午 org.jooq.tools.JooqLogger info
資訊: Generating catalogs      : Total: 1
三月 19, 2018 2:57:26 下午 org.jooq.tools.JooqLogger info
資訊:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@  @@        @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@        @@@@@@@@@@
@@@@@@@@@@@@@@@@  @@  @@    @@@@@@@@@@
@@@@@@@@@@  @@@@  @@  @@    @@@@@@@@@@
@@@@@@@@@@        @@        @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@        @@        @@@@@@@@@@
@@@@@@@@@@    @@  @@  @@@@  @@@@@@@@@@
@@@@@@@@@@    @@  @@  @@@@  @@@@@@@@@@
@@@@@@@@@@        @@  @  @  @@@@@@@@@@
@@@@@@@@@@        @@        @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@  @@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  Thank you for using jOOQ 3.10.5

三月 19, 2018 2:57:26 下午 org.jooq.tools.JooqLogger info
資訊: ARRAYs fetched           : 0 (0 included, 0 excluded)
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: Enums fetched            : 0 (0 included, 0 excluded)
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: Packages fetched         : 0 (0 included, 0 excluded)
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: Routines fetched         : 0 (0 included, 0 excluded)
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: Tables fetched           : 14 (2 included, 12 excluded)
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: No schema version is applied for catalog . Regenerating.
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊:
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: Generating catalog       : DefaultCatalog.java
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: ==========================================================
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: Generating schemata      : Total: 1
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: No schema version is applied for schema gym_member. Regenerating.
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: Generating schema        : GymMember.java
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: ----------------------------------------------------------
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: Sequences fetched        : 0 (0 included, 0 excluded)
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: UDTs fetched             : 0 (0 included, 0 excluded)
三月 19, 2018 2:57:27 下午 org.jooq.tools.JooqLogger info
資訊: Generating tables
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Synthetic primary keys   : 0 (0 included, 0 excluded)
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Overriding primary keys  : 2 (0 included, 2 excluded)
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Generating table         : Importfinance.java [input=importfinance, output=importfinance, pk=KEY_importfinance_PRIMARY]
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Indexes fetched          : 4 (4 included, 0 excluded)
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Generating table         : Importusers.java [input=importusers, output=importusers, pk=KEY_importusers_PRIMARY]
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Tables generated         : Total: 3.286s
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Generating table POJOs
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Generating POJO          : Importfinance.java
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Generating POJO          : Importusers.java
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Table POJOs generated    : Total: 3.346s, +60.097ms
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Generating table references
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Table refs generated     : Total: 3.349s, +3.647ms
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Generating Keys
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Keys generated           : Total: 3.357s, +7.912ms
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Generating Indexes
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Indexes generated        : Total: 3.365s, +8.005ms
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Domains fetched          : 0 (0 included, 0 excluded)
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Generation finished: gym_member: Total: 3.367s, +1.434ms
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊:
三月 19, 2018 2:57:28 下午 org.jooq.tools.JooqLogger info
資訊: Removing excess files
zhushanglide-MacBook-Air:jooqtest sam$

生成的物件

package test.generated.tables.pojos;

import java.io.Serializable;
import java.time.LocalDateTime;
import javax.annotation.Generated;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;


/**
 * This class is generated by jOOQ.
 */
@Generated(
    value = {
        "http://www.jooq.org",
        "jOOQ version:3.10.5"
    },
    comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
@Entity
@Table(name = "importfinance", schema = "gym_member", indexes = {
    @Index(name = "platform", columnList = "platform ASC, ppk ASC"),
    @Index(name = "PRIMARY", unique = true, columnList = "serid ASC")
})
public class Importfinance implements Serializable {

    private static final long serialVersionUID = -2060775195;

    private Long          serid;
    private String        platform;
    private String        ppk;
    private String        rulecodes;
    private Double        saleamount;
    private Double        tradeamount;
    private Long          source;
    private Integer       sourcetype;
    private String        outerid;
    private Integer       outertype;
    private Long          terminususerid;
    private Integer       exported;
    private LocalDateTime createddate;
    private LocalDateTime lastmodifieddate;
    private String        vccardtype;

    public Importfinance() {}

    public Importfinance(Importfinance value) {
        this.serid = value.serid;
        this.platform = value.platform;
        this.ppk = value.ppk;
        this.rulecodes = value.rulecodes;
        this.saleamount = value.saleamount;
        this.tradeamount = value.tradeamount;
        this.source = value.source;
        this.sourcetype = value.sourcetype;
        this.outerid = value.outerid;
        this.outertype = value.outertype;
        this.terminususerid = value.terminususerid;
        this.exported = value.exported;
        this.createddate = value.createddate;
        this.lastmodifieddate = value.lastmodifieddate;
        this.vccardtype = value.vccardtype;
    }

    public Importfinance(
        Long          serid,
        String        platform,
        String        ppk,
        String        rulecodes,
        Double        saleamount,
        Double        tradeamount,
        Long          source,
        Integer       sourcetype,
        String        outerid,
        Integer       outertype,
        Long          terminususerid,
        Integer       exported,
        LocalDateTime createddate,
        LocalDateTime lastmodifieddate,
        String        vccardtype
    ) {
        this.serid = serid;
        this.platform = platform;
        this.ppk = ppk;
        this.rulecodes = rulecodes;
        this.saleamount = saleamount;
        this.tradeamount = tradeamount;
        this.source = source;
        this.sourcetype = sourcetype;
        this.outerid = outerid;
        this.outertype = outertype;
        this.terminususerid = terminususerid;
        this.exported = exported;
        this.createddate = createddate;
        this.lastmodifieddate = lastmodifieddate;
        this.vccardtype = vccardtype;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "serid", unique = true, nullable = false, precision = 19)
    public Long getSerid() {
        return this.serid;
    }

    public void setSerid(Long serid) {
        this.serid = serid;
    }

    @Column(name = "platform", nullable = false, length = 50)
    @NotNull
    @Size(max = 50)
    public String getPlatform() {
        return this.platform;
    }

    public void setPlatform(String platform) {
        this.platform = platform;
    }

    @Column(name = "ppk", nullable = false, length = 50)
    @NotNull
    @Size(max = 50)
    public String getPpk() {
        return this.ppk;
    }

    public void setPpk(String ppk) {
        this.ppk = ppk;
    }

    @Column(name = "rulecodes", nullable = false, length = 50)
    @NotNull
    @Size(max = 50)
    public String getRulecodes() {
        return this.rulecodes;
    }

    public void setRulecodes(String rulecodes) {
        this.rulecodes = rulecodes;
    }

    @Column(name = "saleamount", nullable = false, precision = 22)
    @NotNull
    public Double getSaleamount() {
        return this.saleamount;
    }

    public void setSaleamount(Double saleamount) {
        this.saleamount = saleamount;
    }

    @Column(name = "tradeamount", nullable = false, precision = 22)
    @NotNull
    public Double getTradeamount() {
        return this.tradeamount;
    }

    public void setTradeamount(Double tradeamount) {
        this.tradeamount = tradeamount;
    }

    @Column(name = "source", nullable = false, precision = 19)
    @NotNull
    public Long getSource() {
        return this.source;
    }

    public void setSource(Long source) {
        this.source = source;
    }

    @Column(name = "sourcetype", nullable = false, precision = 10)
    @NotNull
    public Integer getSourcetype() {
        return this.sourcetype;
    }

    public void setSourcetype(Integer sourcetype) {
        this.sourcetype = sourcetype;
    }

    @Column(name = "outerid", nullable = false, length = 50)
    @NotNull
    @Size(max = 50)
    public String getOuterid() {
        return this.outerid;
    }

    public void setOuterid(String outerid) {
        this.outerid = outerid;
    }

    @Column(name = "outertype", nullable = false, precision = 10)
    @NotNull
    public Integer getOutertype() {
        return this.outertype;
    }

    public void setOutertype(Integer outertype) {
        this.outertype = outertype;
    }

    @Column(name = "terminususerid", nullable = false, precision = 19)
    @NotNull
    public Long getTerminususerid() {
        return this.terminususerid;
    }

    public void setTerminususerid(Long terminususerid) {
        this.terminususerid = terminususerid;
    }

    @Column(name = "exported", nullable = false, precision = 10)
    @NotNull
    public Integer getExported() {
        return this.exported;
    }

    public void setExported(Integer exported) {
        this.exported = exported;
    }

    @Column(name = "createddate", nullable = false)
    @NotNull
    public LocalDateTime getCreateddate() {
        return this.createddate;
    }

    public void setCreateddate(LocalDateTime createddate) {
        this.createddate = createddate;
    }

    @Column(name = "lastmodifieddate")
    public LocalDateTime getLastmodifieddate() {
        return this.lastmodifieddate;
    }

    public void setLastmodifieddate(LocalDateTime lastmodifieddate) {
        this.lastmodifieddate = lastmodifieddate;
    }

    @Column(name = "vccardtype", length = 200)
    @Size(max = 200)
    public String getVccardtype() {
        return this.vccardtype;
    }

    public void setVccardtype(String vccardtype) {
        this.vccardtype = vccardtype;
    }
}

雖然註解在 get 上面啦, 但已經方便很多了

Done

← SpringBatch 3 how to use JobScope JobExecutionListener Tasklet SpringBoot2新功能-整合Prometheus →
 
comments powered by Disqus