使用 jooq-codegen-maven mysql-connector-javamysql maven自動生成資料庫程式碼總結
之前使用的是jooq3.8生成資料庫程式碼,之後很久沒有使用這個功能,再次用的時候想改為jooq最新版3.11.4,發現不能生成程式碼了。
so 一番折騰後,重新整理了這個內容。
This post walks you through the process of creating a jOOQ example with MySQL.
What you'll need
- JDK 1.7+
- Maven 3+
- MySQL 5.6+
Stack
- Java
- jOOQ
Project structure
├── src │ └── main │ └── java │ └── com │ └── hellokoding │ └── jooq │ ├── model │ │ ├── tables │ │ │ ├── records │ │ │ │ └── AuthorRecord.java │ │ │ └── Author.java │ │ ├── DefaultCatalog.java │ │ ├── Keys.java │ │ ├── Library.java │ │ └── Tables.java │ └── Application.java ├── create_db.sql └── pom.xml
Files and sub-directories in com/hellokoding/jooq/model
directory will be auto generated by jOOQ. We only play with pom.xml
, create_db.sql
and com/hellokoding/jooq/Application.java
Project dependencies
<?xml version="1.0" encoding="UTF-8"?> <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.0</modelVersion> <groupId>com.hellokoding</groupId> <artifactId>hello-jooq</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Hello jOOQ</name> <description>Hello jOOQ</description> <properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.3</version> </dependency> <dependency> <groupId>org.jooq</groupId> <artifactId>jooq</artifactId> <version>3.8.3</version> </dependency> <dependency> <groupId>org.jooq</groupId> <artifactId>jooq-meta</artifactId> <version>3.8.3</version> </dependency> <dependency> <groupId>org.jooq</groupId> <artifactId>jooq-codegen</artifactId> <version>3.8.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.jooq</groupId> <artifactId>jooq-codegen-maven</artifactId> <version>3.8.3</version> <!-- The plugin should hook into the generate goal --> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies/> <configuration> <jdbc> <driver>${jdbc.driver}</driver> <url>${jdbc.url}</url> <user>${jdbc.user}</user> <password>${jdbc.password}</password> </jdbc> <generator> <database> <name>org.jooq.util.mysql.MySQLDatabase</name> <includes>.*</includes> <excludes></excludes> <inputSchema>library</inputSchema> </database> <target> <packageName>com.hellokoding.jooq.model</packageName> <directory>src/main/java</directory> </target> </generator> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.5.0</version> <executions> <execution> <goals> <goal>exec</goal> </goals> </execution> </executions> <configuration> <systemProperties> <systemProperty> <key>jdbc.driver</key> <value>${jdbc.driver}</value> </systemProperty> <systemProperty> <key>jdbc.user</key> <value>${jdbc.user}</value> </systemProperty> <systemProperty> <key>jdbc.password</key> <value>${jdbc.password}</value> </systemProperty> <systemProperty> <key>jdbc.url</key> <value>${jdbc.url}</value> </systemProperty> </systemProperties> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>default</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <jdbc.user>hellokoding</jdbc.user> <jdbc.password>hellokoding</jdbc.password> <jdbc.url>jdbc:mysql://localhost:3306/library?serverTimezone=UTC</jdbc.url> <jdbc.driver>com.mysql.cj.jdbc.Driver</jdbc.driver> </properties> </profile> </profiles> </project>
jooq-codegen-maven
plugin's used for generate domain and DAO objects in com/hellokoding/jooq/model
Create database
Run below script to create a sample MySQL database
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`) ); INSERT INTO author(1, 'Hello', 'Koding'); INSERT INTO author(2, 'jOOQ', 'Example');
Run
package com.hellokoding.jooq;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.SQLDialect;
import org.jooq.impl.DSL;
import java.sql.Connection;
import java.sql.DriverManager;
import static com.hellokoding.jooq.model.Tables.*;
public class Application {
public static void main(String[] args) throws Exception {
String user = System.getProperty("jdbc.user");
String password = System.getProperty("jdbc.password");
String url = System.getProperty("jdbc.url");
String driver = System.getProperty("jdbc.driver");
Class.forName(driver).newInstance();
try (Connection connection = DriverManager.getConnection(url, user, password)) {
DSLContext dslContext = DSL.using(connection, SQLDialect.MYSQL);
Result<Record> result = dslContext.select().from(AUTHOR).fetch();
for (Record r : result) {
Integer id = r.getValue(AUTHOR.ID);
String firstName = r.getValue(AUTHOR.FIRST_NAME);
String lastName = r.getValue(AUTHOR.LAST_NAME);
System.out.println("ID: " + id + " first name: " + firstName + " last name: " + lastName);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
System.property()
's bound to systemProperty
of exec-maven-plugin
configuration in pom.xml
Run with this command mvn clean compile exec:java -Dexec.mainClass=com.hellokoding.jooq.Application -Dexec.cleanupDaemonThreads=false
Source code
上面的配置是完美的,只是當JOOQ升級到3.11以後,編譯時會出現以下error:
[WARNING] Type not found : Your configured org.jooq.util type was not found.
Do note that in jOOQ 3.11, jOOQ-meta and jOOQ-codegen packages have been renamed. New package names are:
- org.jooq.meta
- org.jooq.meta.extensions
- org.jooq.codegen
- org.jooq.codegen.maven
See https://github.com/jOOQ/jOOQ/issues/7419 for details
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.211 s
[INFO] Finished at: 2018-09-10T14:31:32+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.jooq:jooq-codegen-maven:3.11.4:generate (default) on project JOOQTest: Error running jOOQ code generation tool: Your configured org.jooq.util type was not found.
[ERROR] Do note that in jOOQ 3.11, jOOQ-meta and jOOQ-codegen packages have been renamed. New package names are:
[ERROR] - org.jooq.meta
[ERROR] - org.jooq.meta.extensions
[ERROR] - org.jooq.codegen
[ERROR] - org.jooq.codegen.maven
[ERROR] See https://github.com/jOOQ/jOOQ/issues/7419 for details: org.jooq.util.mysql.MySQLDatabase
[ERROR] -> [Help 1]
[ERROR]
解決辦法就是根據說明修改pom.xml中的 org.jooq.util 為 org.jooq.meta
<database>
<name>org.jooq.meta.mysql.MySQLDatabase</name>
<includes>.*</includes>
<excludes></excludes>
<inputSchema>library</inputSchema>
</database>
好了,現在mvn clean compile就可以生成資料庫的程式碼了。