- 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>
- 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);
}
}