JOOQ代码生成

9 阅读1分钟
  1. maven插件
<plugin>
    <groupId>org.jooq</groupId>
    <artifactId>jooq-codegen-maven</artifactId>
    <version>3.14.16</version>
    <executions>
        <execution>
            <id>jooq-codegen</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <jdbc>
            <!-- 数据库相关配置 -->
            <driver>com.mysql.cj.jdbc.Driver</driver>
            <url>jdbc:mysql://地址:3306/库名</url>
            <user>用户民</user>
            <password>密码</password>
        </jdbc>
        <generator>
            <name>org.jooq.codegen.JavaGenerator</name>
            <database>
                <name>org.jooq.meta.mysql.MySQLDatabase</name>
                <inputSchema>库名</inputSchema>
                <outputSchema>输出的映射库名,对应的库常量类以此为名</outputSchema>
                <includes>表1|表2</includes>
            </database>

            <generate>
                <pojos>true</pojos>
            </generate>

            <target>
                <!-- 自动生成的类的包名,以及路径 -->
                <packageName>com.生成目录</packageName>
                <directory>src/main/java</directory>
            </target>
        </generator>
    </configuration>
</plugin>
  1. java代码
package com.hy.dusk.jooq;

import com.google.common.collect.Lists;
import org.jooq.codegen.GenerationTool;
import org.jooq.meta.jaxb.CatalogMappingType;
import org.jooq.meta.jaxb.Configuration;
import org.jooq.meta.jaxb.Database;
import org.jooq.meta.jaxb.Generate;
import org.jooq.meta.jaxb.Generator;
import org.jooq.meta.jaxb.Jdbc;
import org.jooq.meta.jaxb.SchemaMappingType;
import org.jooq.meta.jaxb.Target;

public class General {
    public static void main(String[] args) throws Exception {
        Configuration configuration = new Configuration()
                .withJdbc(new Jdbc()
                                  .withDriver("com.mysql.cj.jdbc.Driver")
                                  .withUrl("jdbc:mysql://mysql地址:短端口/库名")
                                  .withUser(用户名)
                                  .withPassword(密码))
                .withGenerator(new Generator()
                                       .withDatabase(new Database()
                                                             .withName("org.jooq.meta.mysql.MySQLDatabase")
                                                             .withInputSchema("库名")
                                                             .withOutputSchema("输出的映射库名,对应的库常量类以此为名")
                                                             .withIncludes("表1|表2")
                                       )
                                       .withGenerate(new Generate().
                                                             withPojos(true)
                                                             .withIndexes(false)
                                                             .withKeys(false)
                                                             .withGlobalSchemaReferences(false)
                                                             .withEmptyCatalogs(false))
                                       .withTarget(new Target()
                                                           .withPackageName("com.生成目录")
                                                           .withDirectory("src/main/java")));

        GenerationTool.generate(configuration);
    }
}