自定义代码生成策略:MyStratege
package com.code.generator.stratege;
import org.jooq.tools.StringUtils;
import org.jooq.util.CatalogDefinition;
import org.jooq.util.DefaultGeneratorStrategy;
import org.jooq.util.Definition;
import org.jooq.util.SchemaDefinition;
public class MyStratege extends DefaultGeneratorStrategy {
public String getJavaClassName(Definition definition, Mode mode) {
String name = getFixedJavaClassName(definition);
return name != null ? name : this.getJavaClassName0(definition, mode);
}
private String getJavaClassName0(Definition definition, Mode mode) {
StringBuilder result = new StringBuilder();
result.append(StringUtils.toCamelCase(definition.getOutputName().replace(' ', '_').replace('-', '_').replace('.', '_')));
if (mode == Mode.RECORD) {
result.append("Record");
} else if (mode == Mode.DAO) {
result.append("Dao");
} else if (mode == Mode.POJO) {
result.append("Bo");
} else if (mode == Mode.INTERFACE) {
result.insert(0, "I");
}
return result.toString();
}
final String getFixedJavaClassName(Definition definition) {
if (definition instanceof CatalogDefinition && ((CatalogDefinition) definition).isDefaultCatalog()) {
return "DefaultCatalog";
} else {
return definition instanceof SchemaDefinition && ((SchemaDefinition) definition).isDefaultSchema() ? "DefaultSchema" : null;
}
}
}
generator子模块 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.codegroupId>
<artifactId>generatorartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>generatorname>
<description>generator code projectdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.6.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.jooqgroupId>
<artifactId>jooq-metaartifactId>
<version>${jooq.version}version>
dependency>
<dependency>
<groupId>org.jooqgroupId>
<artifactId>jooq-codegenartifactId>
<version>${jooq.version}version>
dependency>
dependencies>
project>
jooq的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.studygroupId>
<artifactId>jooqartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>pompackaging>
<name>jooqname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.6.RELEASEversion>
<relativePath/>
parent>
<modules>
<module>generatormodule>
modules>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<jooq.version>3.9.5jooq.version>
<mysql.version>6.0.5mysql.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jooqartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<configuration>
<source>1.8source>
<target>1.8target>
configuration>
plugin>
<plugin>
<groupId>org.jooqgroupId>
<artifactId>jooq-codegen-mavenartifactId>
<version>${jooq.version}version>
<executions>
<execution>
<goals>
<goal>generategoal>
goals>
execution>
executions>
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>com.codegroupId>
<artifactId>generatorartifactId>
<version>0.0.1-SNAPSHOTversion>
<scope>systemscope>
<systemPath>${project.basedir}/generator/target/generator-0.0.1-SNAPSHOT.jarsystemPath>
dependency>
dependencies>
<configuration>
<configurationFile>src/main/resources/jooqConfig.xmlconfigurationFile>
configuration>
plugin>
plugins>
build>
project>
jooq代码生成相关配置:src/main/resources/jooqConfig.xml
<configuration>
<jdbc>
<driver>com.mysql.cj.jdbc.Driverdriver>
<url>jdbc:mysql://localhost:3306?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&useSSL=false&serverTimezone=UTCurl>
<user>rootuser>
<password>rootpassword>
jdbc>
<generator>
<strategy>
<name>com.code.generator.stratege.MyStrategename>
strategy>
<database>
<name>org.jooq.util.mysql.MySQLDatabasename>
<includes>.*includes>
<excludes/>
<inputSchema>worldinputSchema>
<forcedTypes>
<forcedType>
<name>BOOLEANname>
<types>(?i:TINYINT(\s*\(\d+\))?(\s*UNSIGNED)?)types>
forcedType>
forcedTypes>
database>
<target>
<packageName>com.study.jooq.dbpackageName>
<directory>src/main/javadirectory>
target>
<generate>
<pojos>truepojos>
<daos>truedaos>
<deprecated>falsedeprecated>
generate>
generator>
configuration>
使用mvn clean install 进行编译,则可生成代码!
F:\ideaworkplace\jooq>mvn clean install
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.study:jooq:pom:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin[org.jooq:jooq-codegen-maven].dependencies.dependency.systemPath' for com.code:generator:jar refers to a non-existing file F:\ideaworkplace\jooq\generator\target\generator-0.0.1-SNAPS
HOT.jar @ line 89, column 37
[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] Reactor Build Order:
[INFO]
[INFO] generator
[INFO] jooq
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building generator 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ generator ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ generator ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\ideaworkplace\jooq\generator\src\main\resources
[INFO] skip non existing resourceDirectory F:\ideaworkplace\jooq\generator\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ generator ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to F:\ideaworkplace\jooq\generator\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ generator ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory F:\ideaworkplace\jooq\generator\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ generator ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ generator ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ generator ---
[INFO] Building jar: F:\ideaworkplace\jooq\generator\target\generator-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ generator ---
[INFO] Installing F:\ideaworkplace\jooq\generator\target\generator-0.0.1-SNAPSHOT.jar to C:\Users\Natasha\.m2\repository\com\code\generator\0.0.1-SNAPSHOT\generator-0.0.1-SNAPSHOT.jar
[INFO] Installing F:\ideaworkplace\jooq\generator\pom.xml to C:\Users\Natasha\.m2\repository\com\code\generator\0.0.1-SNAPSHOT\generator-0.0.1-SNAPSHOT.pom
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building jooq 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ jooq ---
[INFO]
[INFO] --- jooq-codegen-maven:3.9.5:generate (default) @ jooq ---
[INFO] Reading external configuration
[INFO] No 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?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&useSSL=false&serverTimezone=UTC
[INFO] target dir : F:\ideaworkplace\jooq\src/main/java
[INFO] target package : com.study.jooq.db
[INFO] includes : [.*]
[INFO] excludes : []
[INFO] includeExcludeColumns : false
[INFO] ----------------------------------------------------------
[INFO]
[INFO] JavaGenerator parameters
[INFO] ----------------------------------------------------------
[INFO] strategy : class com.code.generator.stratege.MyStratege
[INFO] deprecated : false
[INFO] generated annotation : true
[INFO] JPA annotations : false
[INFO] validation annotations : false
[INFO] instance fields : true
[INFO] sequences : true
[INFO] udts : true
[INFO] routines : true
[INFO] tables : true
[INFO] records : true
[INFO] pojos : true
[INFO] immutable pojos : false
[INFO] interfaces : false
[INFO] immutable interfaces : false
[INFO] daos : true
[INFO] relations : true
[INFO] table-valued functions : true
[INFO] global references : true
[INFO] ----------------------------------------------------------
[INFO]
[INFO] Generation remarks
[INFO] ----------------------------------------------------------
[INFO]
[INFO] ----------------------------------------------------------
[INFO] Generating catalogs : Total: 1
[INFO]
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@ @@ @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@
@@@@@@@@@@@@@@@@ @@ @@ @@@@@@@@@@
@@@@@@@@@@ @@@@ @@ @@ @@@@@@@@@@
@@@@@@@@@@ @@ @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@ @@ @@@@@@@@@@
@@@@@@@@@@ @@ @@ @@@@ @@@@@@@@@@
@@@@@@@@@@ @@ @@ @@@@ @@@@@@@@@@
@@@@@@@@@@ @@ @ @ @@@@@@@@@@
@@@@@@@@@@ @@ @@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Thank you for using jOOQ 3.9.5
[INFO] ARRAYs fetched : 0 (0 included, 0 excluded)
[INFO] Tables fetched : 3 (3 included, 0 excluded)
[INFO] Enums fetched : 2 (2 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 world. Regenerating.
[INFO] Generating schema : World.java
[INFO] ----------------------------------------------------------
[INFO] Sequences fetched : 0 (0 included, 0 excluded)
[INFO] UDTs fetched : 0 (0 included, 0 excluded)
[INFO] Generating tables
[INFO] Adding foreign key : city_ibfk_1 (world.city.CountryCode) referencing KEY_country_PRIMARY
[INFO] Adding foreign key : countryLanguage_ibfk_1 (world.countrylanguage.CountryCode) referencing KEY_country_PRIMARY
[INFO] Synthetic primary keys : 0 (0 included, 0 excluded)
[INFO] Overriding primary keys : 3 (0 included, 3 excluded)
[INFO] Generating table : City.java [input=city, output=city, pk=KEY_city_PRIMARY]
[INFO] Generating table : Country.java [input=country, output=country, pk=KEY_country_PRIMARY]
[INFO] Generating table : Countrylanguage.java [input=countrylanguage, output=countrylanguage, pk=KEY_countrylanguage_PRIMARY]
[INFO] Tables generated : Total: 690.4ms
[INFO] Generating table POJOs
[INFO] Generating POJO : CityBo.java
[INFO] Generating POJO : CountryBo.java
[INFO] Generating POJO : CountrylanguageBo.java
[INFO] Table POJOs generated : Total: 720.12ms, +29.72ms
[INFO] Generating DAOs
[INFO] Generating DAO : CityDao.java
[INFO] Generating DAO : CountryDao.java
[INFO] Generating DAO : CountrylanguageDao.java
[INFO] Table DAOs generated : Total: 731.969ms, +11.848ms
[INFO] Generating table references
[INFO] Table refs generated : Total: 737.721ms, +5.751ms
[INFO] Generating Keys
[INFO] Keys generated : Total: 740.715ms, +2.993ms
[INFO] Generating table records
[INFO] Generating record : CityRecord.java
[INFO] Generating record : CountryRecord.java
[INFO] Generating record : CountrylanguageRecord.java
[INFO] Table records generated : Total: 771.507ms, +30.792ms
[INFO] Generating ENUMs
[INFO] Generating ENUM : CountryContinent.java
[INFO] Generating ENUM : CountrylanguageIsofficial.java
[INFO] Enums generated : Total: 775.367ms, +3.86ms
[INFO] Domains fetched : 0 (0 included, 0 excluded)
[INFO] Routines fetched : 0 (0 included, 0 excluded)
[INFO] Generation finished: world: Total: 791.212ms, +15.844ms
[INFO]
[INFO] Removing excess files
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ jooq ---
[INFO] Installing F:\ideaworkplace\jooq\pom.xml to C:\Users\Natasha\.m2\repository\com\study\jooq\0.0.1-SNAPSHOT\jooq-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] generator .......................................... SUCCESS [ 1.466 s]
[INFO] jooq ............................................... SUCCESS [ 1.662 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.536 s
[INFO] Finished at: 2017-08-13T16:06:15+08:00
[INFO] Final Memory: 33M/391M
[INFO] ------------------------------------------------------------------------
public class JooqGeneratorUtil {
public static void main(String[] args) throws Exception {
generate("jooqConfig.xml");
}
private static void generate(String xml) throws Exception {
URL url = JooqGeneratorUtil.class.getClassLoader().getResource(xml);
String s = URLDecoder.decode(url.getFile(), "utf-8");
Configuration configuration = JAXB.unmarshal(new File(s), Configuration.class);
GenerationTool.generate(configuration);
}
}