SpringBoot整合JOOQ配置(一)

项目Demo地址:https://github.com/shileishmily/spring-boot-jooq-demo

1、依赖,因为我用了jooq+mysql的环境

compile group: 'org.jooq', name: 'jooq-meta', version: '3.11.11'
compile group: 'org.jooq', name: 'jooq-codegen', version: '3.11.11'
compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.39'

 

2、配置表到JAVA代码自动生成的配置类JOOQGenerator,代码如下

package com.suixingpay.jooq.generator;

import org.apache.commons.lang.StringUtils;
import org.jooq.codegen.GenerationTool;
import org.jooq.codegen.JavaGenerator;
import org.jooq.meta.jaxb.Configuration;
import org.jooq.meta.jaxb.Database;
import org.jooq.meta.jaxb.Generate;
import org.jooq.meta.jaxb.Generator;
import org.jooq.meta.jaxb.Jdbc;
import org.jooq.meta.jaxb.Target;

public class JOOQGenerator extends JavaGenerator {


    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private static final String JDBC_URL = "jdbc:mysql://localhost:3306";
    private static final String JDBC_USERNAME = "root";
    private static final String JDBC_PASSWORD = "111";

    private static final String JOOQ_DATABASE_NAME = "org.jooq.meta.jdbc.JDBCDatabase";

    private static final String GENERATOR_PATH = "test-core/src/main/java";

    public static void genTables(String schemaName, String packageName, String includes, String excludes) throws Exception {

        Jdbc jdbc = createJdbc();
        Generator generator = createGenerator(schemaName, packageName, includes, excludes);

        Configuration configuration = new Configuration()
                .withJdbc(jdbc)
                .withGenerator(generator);

        GenerationTool.generate(configuration);

    }

    private static Generator createGenerator(String schemaName, String packageName, String includes, String excludes) {

        Database database = createDatabase(schemaName, includes, excludes);
        Target target = createTarget(packageName);
        Generate generate = createGenerate();

        return new Generator()
                .withDatabase(database)
                .withTarget(target)
                .withGenerate(generate);
    }

    private static Jdbc createJdbc() {
        return new Jdbc()
                .withDriver(JDBC_DRIVER)
                .withUrl(JDBC_URL)
                .withUser(JDBC_USERNAME)
                .withPassword(JDBC_PASSWORD);
    }

    private static Target createTarget(String packageName) {

        return new Target().withDirectory(GENERATOR_PATH).withPackageName(packageName);
    }

    private static Database createDatabase(String schemaName, String includes, String excludes) {
        Database database = new Database().withName(JOOQ_DATABASE_NAME).withInputSchema(schemaName);
        if (StringUtils.isNotBlank(includes)) {
            database.withIncludes(includes);
        }

        if (StringUtils.isNotBlank(excludes)) {
            database.withExcludes(excludes);
        }
        return database;
    }

    private static Generate createGenerate() {
        Generate generate = new Generate();
        generate.withDaos(false)
                .withPojos(true);
        return generate;
    }

    private static void generate() throws Exception {
        genTables("jooq_test", "com.suixingpay.jooq.entity", ".*", "");
    }

    public static void main(String[] args) throws Exception {

        generate();

    }

}

3、新建一张表sys_user,表结构如下

CREATE TABLE `sys_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(100) DEFAULT NULL COMMENT '用户名,登录账户',
  `real_name` varchar(100) DEFAULT NULL COMMENT '真实姓名',
  `email` varchar(100) DEFAULT NULL COMMENT '邮箱',
  `phone` varchar(15) DEFAULT NULL COMMENT '手机号',
  `pswd` varchar(32) DEFAULT NULL COMMENT '密码',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `last_login_time` datetime DEFAULT NULL COMMENT '最后登录时间',
  `user_status` int(1) DEFAULT '1' COMMENT '1:有效,0:禁止登录',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

4、运行JOOQGenerator的Main方法

    public static void main(String[] args) throws Exception {
        generate();
    }

5、在test-core模块生成了如下代码

 

 

 

项目Demo地址:https://github.com/shileishmily/spring-boot-jooq-demo

posted @ 2019-06-12 22:42  shileishmily  阅读(2962)  评论(0编辑  收藏  举报