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>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<configuration>
	                <fork>true</fork>
	        </configuration>
	        <!-- springloaded 热部署 hot deploy-->
			<dependencies>
				
				<dependency>
					<groupId>org.springframework</groupId>
					<artifactId>springloaded</artifactId>
					<version>1.2.4.RELEASE</version>
				</dependency>
			</dependencies>
			<executions>
				<execution>
					<goals>
						<goal>repackage</goal>
					</goals>
					<configuration>
						<classifier>exec</classifier>
					</configuration>
				</execution>
			</executions>
		</plugin>
		
		<!--数据库代码生成的插件 -->
		<!--数据库代码生成的插件 -->
		<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>util.Timestamp2DateConverter</converter>
                            </customType>
                            <customType>
                                <name>byte2IntegerConverter</name>
                                <type>java.lang.Integer</type>
                                <converter>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>jooq.generated</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;

	@Override
    public Integer from(Byte databaseObject) {
        return Objects.isNull(databaseObject) ? null : databaseObject.intValue();
    }

    @Override
    public Byte to(Integer userObject) {
        return Objects.isNull(userObject) ? null : userObject.byteValue();
    }

    @Override
    public Class<Byte> fromType() {
        return Byte.class;
    }

    @Override
    public 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;

	@Override
    public Date from(Timestamp databaseObject) {
        return Objects.isNull(databaseObject) ? null : new Date(databaseObject.getTime());
    }

    @Override
    public Timestamp to(Date userObject) {
        return Objects.isNull(userObject) ? null : new Timestamp(userObject.getTime());
    }

    @Override
    public Class<Timestamp> fromType() {
        return Timestamp.class;
    }

    @Override
    public Class<Date> toType() {
        return Date.class;
    }

}


阅读更多
个人分类: 开发工具的使用
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭