本文主要是介绍jooq自动生成java代码【无脑,史上最简单】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
环境:eclipse+maven
1.首先新建一个maven项目
2.然后把我下面的代码贴到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.0</modelVersion><groupId>com</groupId><artifactId>myspringboot</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>myspringboot</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version><!--防止maven改动IDE的language level --><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><!--数据库迁移所用的参数 --><db.driver>com.mysql.jdbc.Driver</db.driver><db.url>jdbc:mysql://localhost:3306</db.url><db.username>root</db.username><db.password>root</db.password><db.schema>test</db.schema></properties><parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><scope>provided</scope></dependency><dependency><groupId>com.jolbox</groupId><artifactId>bonecp</artifactId><version>0.8.0.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--JDBC连接池 --><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId></dependency><!--数据库schema代码生成器 --><dependency><groupId>org.jooq</groupId><artifactId>jooq-codegen</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId><exclusions><exclusion><!-- 取消引入tomcat jdbc --><groupId>org.apache.tomcat</groupId><artifactId>tomcat-jdbc</artifactId></exclusion></exclusions></dependency></dependencies><!--数据库代码生成的插件 --><build><plugins><plugin><!-- Specify the maven code generator plugin --><groupId>org.jooq</groupId><artifactId>jooq-codegen-maven</artifactId><!-- The plugin should hook into the generate goal --><executions><execution><goals><goal>generate</goal></goals></execution></executions><configuration><!-- JDBC connection parameters --><jdbc><driver>com.mysql.jdbc.Driver</driver><url>${db.url}</url><user>${db.username}</user><password>${db.password}</password></jdbc><!-- Generator parameters --><generator><database><name>org.jooq.util.mysql.MySQLDatabase</name><includes>.*</includes><inputSchema>${db.schema}</inputSchema><unsignedTypes>false</unsignedTypes><customTypes><customType><name>timestamp2DateConverter</name><type>java.util.Date</type><converter>com.util.Timestamp2DateConverter</converter></customType><customType><name>sqlDate2DateConverter</name><type>java.util.Date</type><converter>com.util.SQLDate2DateConverter</converter></customType><customType><name>byte2IntegerConverter</name><type>java.lang.Integer</type><converter>com.util.Byte2IntegerConverter</converter></customType></customTypes><forcedTypes><forcedType><name>timestamp2DateConverter</name><types>DATETIME|TIMESTAMP</types></forcedType><forcedType><name>byte2IntegerConverter</name><types>TINYINT</types></forcedType></forcedTypes></database><generate><fluentSetters>true</fluentSetters><pojos>true</pojos><pojosToString>true</pojosToString><pojosEqualsAndHashCode>true</pojosEqualsAndHashCode><daos>true</daos><springAnnotations>true</springAnnotations></generate><target><packageName>sxj.db</packageName><directory>src/main/java</directory></target></generator></configuration></plugin></plugins><pluginManagement><plugins><!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.--><plugin><groupId>org.eclipse.m2e</groupId><artifactId>lifecycle-mapping</artifactId><version>1.0.0</version><configuration><lifecycleMappingMetadata><pluginExecutions><pluginExecution><pluginExecutionFilter><groupId>org.jooq</groupId><artifactId>jooq-codegen-maven</artifactId><versionRange>[3.8.4,)</versionRange><goals><goal>generate</goal></goals></pluginExecutionFilter><action><ignore></ignore></action></pluginExecution></pluginExecutions></lifecycleMappingMetadata></configuration></plugin></plugins></pluginManagement></build></project>
3.右击项目-run as -maven generate-sources
4.完成了。
备注:有三个类你们可能用得上
Byte2IntegerConverter.java
package com.util;import org.jooq.Converter;import java.util.Objects;public class Byte2IntegerConverter implements Converter<Byte, Integer> {private static final long serialVersionUID = 1L;@Overridepublic Integer from(Byte databaseObject) {return Objects.isNull(databaseObject) ? null : databaseObject.intValue();}@Overridepublic Byte to(Integer userObject) {return Objects.isNull(userObject) ? null : userObject.byteValue();}@Overridepublic Class<Byte> fromType() {return Byte.class;}@Overridepublic Class<Integer> toType() {return Integer.class;}}
Timestamp2DateConverter.java
package com.util;import org.jooq.Converter;import java.sql.Timestamp;
import java.util.Date;
import java.util.Objects;public class Timestamp2DateConverter implements Converter<Timestamp, Date> {private static final long serialVersionUID = 1529338732611199027L;@Overridepublic Date from(Timestamp databaseObject) {return Objects.isNull(databaseObject) ? null : new Date(databaseObject.getTime());}@Overridepublic Timestamp to(Date userObject) {return Objects.isNull(userObject) ? null : new Timestamp(userObject.getTime());}@Overridepublic Class<Timestamp> fromType() {return Timestamp.class;}@Overridepublic Class<Date> toType() {return Date.class;}}
SQLDate2DateConverter.java
package com.util;import org.jooq.Converter;import java.util.Date;
import java.util.Objects;public class SQLDate2DateConverter implements Converter<java.sql.Date, Date> {@Overridepublic Date from(java.sql.Date databaseObject) {return Objects.isNull(databaseObject) ? null : new Date(databaseObject.getTime());}@Overridepublic java.sql.Date to(Date userObject) {return Objects.isNull(userObject) ? null : new java.sql.Date(userObject.getTime());}@Overridepublic Class<java.sql.Date> fromType() {return java.sql.Date.class;}@Overridepublic Class<Date> toType() {return Date.class;}}
这篇关于jooq自动生成java代码【无脑,史上最简单】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!