jooq自动生成pojo

最近使用了jooq,,jooq确实很少用。。不过真的很少用,,mybatis可以自动生成dao层,,jooq可以生成tables等,如图:

jooq自动生成pojo_第1张图片

可以直接操作这些来操作数据库。

下面码出步骤:

1、项目结构:

jooq自动生成pojo_第2张图片

如图,MyStrategy类是策略类,里面可以写自定义的生成策略,,db包下即我们要指定的生成jooq的地方

2、添加pom文件

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.0modelVersion>

    <groupId>com.yufenggroupId>
    <artifactId>jooq-mavenartifactId>
    <version>1.0-SNAPSHOTversion>

    <packaging>pompackaging>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <mysql-jdbc-driver>5.1.34mysql-jdbc-driver>
        
        <vapor-version>1.0.5-SNAPSHOTvapor-version>
        
        <jooq-version>3.7.0jooq-version>
        <javax-validation-version>1.1.0.Finaljavax-validation-version>
        <spring-boot-version>1.5.6.RELEASEspring-boot-version>
    properties>
    
    
    <dependencies>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jooqartifactId>
            <version>${spring-boot-version}version>
        dependency>
        

        
        <dependency>
            <groupId>org.jooqgroupId>
            <artifactId>jooqartifactId>
            <version>${jooq-version}version>
        dependency>
        <dependency>
            <groupId>org.jooqgroupId>
            <artifactId>jooq-metaartifactId>
            <version>${jooq-version}version>
        dependency>
        <dependency>
            <groupId>org.jooqgroupId>
            <artifactId>jooq-codegenartifactId>
            <version>${jooq-version}version>
        dependency>
        

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>${mysql-jdbc-driver}version>
        dependency>

        <dependency>
            <groupId>javax.validationgroupId>
            <artifactId>validation-apiartifactId>
            <version>${javax-validation-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>
                        <id>generate-mysqlid>
                        <phase>generate-sourcesphase>
                        <goals>
                            <goal>generategoal>
                        goals>
                    execution>
                executions>
                <configuration>
                    
                    <jdbc>
                        <driver>com.mysql.jdbc.Driverdriver>
                        
                        
                        
                        <user>usernameuser>
                        
                        <password>passwordpassword>
                    jdbc>
                    
                    <generator>
                        <name>org.jooq.util.JavaGeneratorname>
                        <strategy>
                            <name>ele.me.lizijian.strategy.MyStrategyname>
                        strategy>
                        <database>
                            <name>org.jooq.util.mysql.MySQLDatabasename>
                            <excludes>QRTZ_.*|jooq-maven\..*\.is_deleteexcludes>
                            <includeExcludeColumns>trueincludeExcludeColumns>
                            <inputSchema>atfcapiinputSchema>
                        database>
                        <generate>
                            <deprecated>falsedeprecated>
                            <daos>truedaos>
                            <interfaces>trueinterfaces>
                            <validationAnnotations>truevalidationAnnotations>
                        generate>
                        <target>
                            <packageName>ele.me.atfcapi.tools.dbpackageName>
                            <directory>src/main/javadirectory>
                        target>
                    generator>
                configuration>
            plugin>
        plugins>
    build>
project>

3、完善自定义策略类:指定生成pojo的规则

  1. @Configuration  
  2. public class MyStrategy extends DefaultGeneratorStrategy {  
  3.     private String tablePrefix = "tb_";  
  4.   
  5.     public String getTablePrefix() {  
  6.         return tablePrefix;  
  7.     }  
  8.   
  9.     public void setTablePrefix(String tablePrefix) {  
  10.         this.tablePrefix = tablePrefix;  
  11.     }  
  12.   
  13.     @Override  
  14.     public String getJavaClassName(Definition definition, Mode mode) {  
  15.         StringBuilder result = new StringBuilder();  
  16.   
  17.         String outputName = definition.getOutputName();  
  18.         if (!StringUtils.isBlank(tablePrefix) && outputName.startsWith(tablePrefix)) {  
  19.             outputName = outputName.substring(tablePrefix.length());  
  20.         }  
  21.         result.append(StringUtils.toCamelCase(outputName));  
  22.   
  23.         if (mode == Mode.RECORD) {  
  24.             result.append("Record");  
  25.         } else if (mode == Mode.DAO) {  
  26.             result.append("Dao");  
  27.         } else if (mode == Mode.INTERFACE) {  
  28.             result.insert(0"I");  
  29.         }  
  30.         return result.toString();  
  31.     }  
  32. }  

4、生成

准备工作已经完成了,接下来生成代码:别用mvn clean install
1、rebuild project,为.java文件生成相应的.class文件

2、在命令行里输入mvn install

代码生成完毕。。。。

你可能感兴趣的:(工具,测试开发,jooq)