使用 jooq-codegen-maven mysql-connector-javamysql maven自动生成数据库代码总结

之前使用的是jooq3.8生成数据库代码,之后很久没有使用这个功能,再次用的时候想改为jooq最新版3.11.4,发现不能生成代码了。

so 一番折腾后,重新整理了这个内容。

对于jooq的配置网上很多了,这里转一个我参考的:https://hellokoding.com/jooq-example-with-mysql/

感谢Giau Ngo的总结:

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/modeldirectory will be auto generated by jOOQ. We only play with pom.xml, create_db.sql and com/hellokoding/jooq/Application.java

Project dependencies

pom.xml



	4.0.0

	com.hellokoding
	hello-jooq
	0.0.1-SNAPSHOT
	jar

	Hello jOOQ
	Hello jOOQ

	
		1.7
		1.7
	

	
		
			mysql
			mysql-connector-java
			6.0.3
		
		
			org.jooq
			jooq
			3.8.3
		
		
			org.jooq
			jooq-meta
			3.8.3
		
		
			org.jooq
			jooq-codegen
			3.8.3
		
	
	
	
		
			
				org.jooq
				jooq-codegen-maven
				3.8.3

				
				
					
						
							generate
						
					
				

				

				
					
						${jdbc.driver}
						${jdbc.url}
						${jdbc.user}
						${jdbc.password}
					

					
						
							org.jooq.util.mysql.MySQLDatabase
							.*
							
							library
						
						
							com.hellokoding.jooq.model
							src/main/java
						
					
				
			

			
				org.codehaus.mojo
				exec-maven-plugin
				1.5.0
				
					
						
							exec
						
					
				
				
					
						
							jdbc.driver
							${jdbc.driver}
						
						
							jdbc.user
							${jdbc.user}
						
						
							jdbc.password
							${jdbc.password}
						
						
							jdbc.url
							${jdbc.url}
						
					
				
			
		
	

	
		
			default
			
				true
			
			
				hellokoding
				hellokoding
				jdbc:mysql://localhost:3306/library?serverTimezone=UTC
				com.mysql.cj.jdbc.Driver
			
		
	
	


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_db.sql

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

src/main/java/com/hellokoding/jooq/Application.java

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 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

[email protected]:hellokoding/jooq-mysql.git https://github.com/hellokoding/jooq-mysql

上面的配置是完美的,只是当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


org.jooq.meta.mysql.MySQLDatabase
.*

library

好了,现在mvn clean compile就可以生成数据库的代码了。

你可能感兴趣的:(原创)