【中级篇】详细讲解使用SpringBoot + Jooq整合(一)

一、什么是JOOQ:

JOOQ 是基于Java访问关系型数据库的工具包。JOOQ 既吸取了传统ORM操作数据的简单性和安全性,又保留了原生sql的灵活性,它更像是介于 ORMS和JDBC的中间层。

DSL(Domain Specific Language )风格,代码够简单和清晰。遇到不会写的sql可以充分利用IDEA代码提示功能轻松完成。
保留了传统ORM 的优点,简单操作性,安全性,类型安全等。不需要复杂的配置,并且可以利用Java 8 Stream API 做更加复杂的数据转换。
支持主流的RDMS和更多的特性,如self-joins,union,存储过程,复杂的子查询等等。
丰富的Fluent API和完善文档。
runtime schema mapping 可以支持多个数据库schema访问。简单来说使用一个连接池可以访问N个DB schema,使用比较多的就是SaaS应用的多租户场景。


如何反向生成Jooq?

前言:本次项目的搭建使用SpringBoot + MySQL + Jooq整合

1.创建好SpringBoot项目后在pom.xml中导入jooq所需包:



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.4.RELEASE
		 
	
	com.demo
	springjooq-1
	0.0.1-SNAPSHOT
	springjooq-1
	Demo project for Spring Boot

	
		1.8
	

	
		
		    org.springframework.boot
		    spring-boot-starter-web
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
            mysql
            mysql-connector-java
            5.1.21
        
        
		    org.springframework.boot
		    spring-boot-starter-jooq
		
 		
            org.projectlombok
            lombok
            1.16.18
        

        
            com.alibaba
            fastjson
            1.2.15
        
        
            com.alibaba
            druid
            1.1.3
        
        
            org.jooq
            jooq-meta
        
        
            org.jooq
            jooq-codegen
        
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
			
		    org.jooq
		    jooq-codegen-maven
		    ${jooq.version}
		    
		        
		            
		                generate
		            
		        
		    
		    
		    
		        
		            mysql
		            mysql-connector-java
		            5.1.21
		        
		    
		    
		        
		        src/main/resources/JooqConfig.xml
		    
		
		
	


2.在application.properties中加入访问数据库路径

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3.在SpringBoot项目结构的src/main/resources下创建一个JooqConfig.xml



    
        com.mysql.jdbc.Driver
        
        jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=UTF-8
        
        root
        
        123456
    
    
        
     
        
            
 
            
 
            org.jooq.meta.mysql.MySQLDatabase
 
            
            .*
            
 
            
            demo
        
 
        
            
           true
            true
            
            true
            
            
            false
        
 
        
            
            com.demo.main.jooq
            src/main/java
        
    

4.右击项目选择Run->Maven install运行即可,如图 (注:如使用IDEA在编辑器的右边点击Maven找到指定项目点击install即可 )

【中级篇】详细讲解使用SpringBoot + Jooq整合(一)_第1张图片

5.生成好后可以看到项目的结构

【中级篇】详细讲解使用SpringBoot + Jooq整合(一)_第2张图片

6.生成的每个类是做什么的!

1.先讲解下jooq包下面的每个类的作用: 

【中级篇】详细讲解使用SpringBoot + Jooq整合(一)_第3张图片

DefaultCatalog.java:里面存放的是Demo.java也就是数据库名

/*
 * This file is generated by jOOQ.
 */
package com.demo.main.jooq;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Generated;

import org.jooq.Schema;
import org.jooq.impl.CatalogImpl;


/**
 * This class is generated by jOOQ.
 */
@Generated(
    value = {
        "http://www.jooq.org",
        "jOOQ version:3.11.9"
    },
    comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class DefaultCatalog extends CatalogImpl {

    private static final long serialVersionUID = 1891688733;

    /**
     * The reference instance of 
     */
    public static final DefaultCatalog DEFAULT_CATALOG = new DefaultCatalog();

    /**
     * The schema demo.
     */
    public final Demo DEMO = com.demo.main.jooq.Demo.DEMO;

    /**
     * No further instances allowed
     */
    private DefaultCatalog() {
        super("");
    }

    @Override
    public final List getSchemas() {
        List result = new ArrayList();
        result.addAll(getSchemas0());
        return result;
    }

    private final List getSchemas0() {
        return Arrays.asList(
            Demo.DEMO);
    }
}

 Demo.java里面存放的是数据库的表

/*
 * This file is generated by jOOQ.
 */
package com.demo.main.jooq;


import com.demo.main.jooq.tables.SClass;
import com.demo.main.jooq.tables.Student;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.Generated;

import org.jooq.Catalog;
import org.jooq.Table;
import org.jooq.impl.SchemaImpl;


/**
 * This class is generated by jOOQ.
 */
@Generated(
    value = {
        "http://www.jooq.org",
        "jOOQ version:3.11.9"
    },
    comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Demo extends SchemaImpl {

    private static final long serialVersionUID = 979506244;

    /**
     * The reference instance of demo
     */
    public static final Demo DEMO = new Demo();

    /**
     * The table demo.student.
     */
    public final Student STUDENT = com.demo.main.jooq.tables.Student.STUDENT;

    /**
     * The table demo.s_class.
     */
    public final SClass S_CLASS = com.demo.main.jooq.tables.SClass.S_CLASS;

    /**
     * No further instances allowed
     */
    private Demo() {
        super("demo", null);
    }


    /**
     * {@inheritDoc}
     */
    @Override
    public Catalog getCatalog() {
        return DefaultCatalog.DEFAULT_CATALOG;
    }

    @Override
    public final List> getTables() {
        List result = new ArrayList();
        result.addAll(getTables0());
        return result;
    }

    private final List> getTables0() {
        return Arrays.>asList(
            Student.STUDENT,
            SClass.S_CLASS);
    }
}

 Indexes.java存放的是表的主键(索引)

/*
 * This file is generated by jOOQ.
 */
package com.demo.main.jooq;


import com.demo.main.jooq.tables.SClass;

import javax.annotation.Generated;

import org.jooq.Index;
import org.jooq.OrderField;
import org.jooq.impl.Internal;


/**
 * A class modelling indexes of tables of the demo schema.
 */
@Generated(
    value = {
        "http://www.jooq.org",
        "jOOQ version:3.11.9"
    },
    comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Indexes {

    // -------------------------------------------------------------------------
    // INDEX definitions
    // -------------------------------------------------------------------------

    public static final Index S_CLASS_PRIMARY = Indexes0.S_CLASS_PRIMARY;

    // -------------------------------------------------------------------------
    // [#1459] distribute members to avoid static initialisers > 64kb
    // -------------------------------------------------------------------------

    private static class Indexes0 {//数据库中设置SClass.ID为主键
        public static Index S_CLASS_PRIMARY = Internal.createIndex("PRIMARY", SClass.S_CLASS, new OrderField[] { SClass.S_CLASS.ID }, true);
    }
}

 Keys.java里面有两个类Identities0存放的自增字段,UniqueKeys0存放的是唯一字段

/*
 * This file is generated by jOOQ.
 */
package com.demo.main.jooq;


import com.demo.main.jooq.tables.SClass;
import com.demo.main.jooq.tables.records.SClassRecord;

import javax.annotation.Generated;

import org.jooq.Identity;
import org.jooq.UniqueKey;
import org.jooq.impl.Internal;


/**
 * A class modelling foreign key relationships and constraints of tables of 
 * the demo schema.
 */
@Generated(
    value = {
        "http://www.jooq.org",
        "jOOQ version:3.11.9"
    },
    comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Keys {

    // -------------------------------------------------------------------------
    // IDENTITY definitions
    // -------------------------------------------------------------------------

    public static final Identity IDENTITY_S_CLASS = Identities0.IDENTITY_S_CLASS;

    // -------------------------------------------------------------------------
    // UNIQUE and PRIMARY KEY definitions
    // -------------------------------------------------------------------------

    public static final UniqueKey KEY_S_CLASS_PRIMARY = UniqueKeys0.KEY_S_CLASS_PRIMARY;

    // -------------------------------------------------------------------------
    // FOREIGN KEY definitions
    // -------------------------------------------------------------------------


    // -------------------------------------------------------------------------
    // [#1459] distribute members to avoid static initialisers > 64kb
    // -------------------------------------------------------------------------

    private static class Identities0 {//自增
        public static Identity IDENTITY_S_CLASS = Internal.createIdentity(SClass.S_CLASS, SClass.S_CLASS.ID);
    }

    private static class UniqueKeys0 {//主键
        public static final UniqueKey KEY_S_CLASS_PRIMARY = Internal.createUniqueKey(SClass.S_CLASS, "KEY_s_class_PRIMARY", SClass.S_CLASS.ID);
    }
}

 Tables.java里面存放的是表名,一般都是通过这个类去访问表和字段的

/*
 * This file is generated by jOOQ.
 */
package com.demo.main.jooq;


import com.demo.main.jooq.tables.SClass;
import com.demo.main.jooq.tables.Student;

import javax.annotation.Generated;


/**
 * Convenience access to all tables in demo
 */
@Generated(
    value = {
        "http://www.jooq.org",
        "jOOQ version:3.11.9"
    },
    comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Tables {

    /**
     * The table demo.student.
     */
    public static final Student STUDENT = com.demo.main.jooq.tables.Student.STUDENT;

    /**
     * The table demo.s_class.
     */
    public static final SClass S_CLASS = com.demo.main.jooq.tables.SClass.S_CLASS;
}

2.然后讲下tables包下面的两个类对应着数据库的表, 类里面就是表的字段了(注:只展示一个类的代码,方便理解)

 

 

/*
 * This file is generated by jOOQ.
 */
package com.demo.main.jooq.tables;


import com.demo.main.jooq.Demo;
import com.demo.main.jooq.Indexes;
import com.demo.main.jooq.Keys;
import com.demo.main.jooq.tables.records.SClassRecord;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Generated;

import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Identity;
import org.jooq.Index;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Schema;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.UniqueKey;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;


/**
 * This class is generated by jOOQ.
 */
@Generated(
    value = {
        "http://www.jooq.org",
        "jOOQ version:3.11.9"
    },
    comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class SClass extends TableImpl {

    private static final long serialVersionUID = -1991044853;

    /**
     * The reference instance of demo.s_class
     */
    public static final SClass S_CLASS = new SClass();

    /**
     * The class holding records for this type
     */
    @Override
    public Class getRecordType() {
        return SClassRecord.class;
    }

    /**
     * The column demo.s_class.id.
     */
    public final TableField ID = createField("id", org.jooq.impl.SQLDataType.INTEGER.nullable(false).identity(true), this, "");

    /**
     * The column demo.s_class.classname.
     */
    public final TableField CLASSNAME = createField("classname", org.jooq.impl.SQLDataType.VARCHAR(255), this, "");

    /**
     * Create a demo.s_class table reference
     */
    public SClass() {
        this(DSL.name("s_class"), null);
    }

    /**
     * Create an aliased demo.s_class table reference
     */
    public SClass(String alias) {
        this(DSL.name(alias), S_CLASS);
    }

    /**
     * Create an aliased demo.s_class table reference
     */
    public SClass(Name alias) {
        this(alias, S_CLASS);
    }

    private SClass(Name alias, Table aliased) {
        this(alias, aliased, null);
    }

    private SClass(Name alias, Table aliased, Field[] parameters) {
        super(alias, null, aliased, parameters, DSL.comment(""));
    }

    public  SClass(Table child, ForeignKey key) {
        super(child, key, S_CLASS);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Schema getSchema() {
        return Demo.DEMO;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List getIndexes() {
        return Arrays.asList(Indexes.S_CLASS_PRIMARY);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Identity getIdentity() {
        return Keys.IDENTITY_S_CLASS;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public UniqueKey getPrimaryKey() {
        return Keys.KEY_S_CLASS_PRIMARY;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List> getKeys() {
        return Arrays.>asList(Keys.KEY_S_CLASS_PRIMARY);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SClass as(String alias) {
        return new SClass(DSL.name(alias), this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SClass as(Name alias) {
        return new SClass(alias, this);
    }

    /**
     * Rename this table
     */
    @Override
    public SClass rename(String name) {
        return new SClass(DSL.name(name), null);
    }

    /**
     * Rename this table
     */
    @Override
    public SClass rename(Name name) {
        return new SClass(name, null);
    }
}

 3.daos包下面的dao层会有自动生成的一些定义好的增删改方法,这里只生成了一个因为另一张表没有设置索引

/*
 * This file is generated by jOOQ.
 */
package com.demo.main.jooq.tables.daos;


import com.demo.main.jooq.tables.SClass;
import com.demo.main.jooq.tables.records.SClassRecord;

import java.util.List;

import javax.annotation.Generated;

import org.jooq.Configuration;
import org.jooq.impl.DAOImpl;


/**
 * This class is generated by jOOQ.
 */
@Generated(
    value = {
        "http://www.jooq.org",
        "jOOQ version:3.11.9"
    },
    comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class SClassDao extends DAOImpl {

    /**
     * Create a new SClassDao without any configuration
     */
    public SClassDao() {
        super(SClass.S_CLASS, com.demo.main.jooq.tables.pojos.SClass.class);
    }

    /**
     * Create a new SClassDao with an attached configuration
     */
    public SClassDao(Configuration configuration) {
        super(SClass.S_CLASS, com.demo.main.jooq.tables.pojos.SClass.class, configuration);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected Integer getId(com.demo.main.jooq.tables.pojos.SClass object) {
        return object.getId();
    }

    /**
     * Fetch records that have id IN (values)
     */
    public List fetchById(Integer... values) {
        return fetch(SClass.S_CLASS.ID, values);
    }

    /**
     * Fetch a unique record that has id = value
     */
    public com.demo.main.jooq.tables.pojos.SClass fetchOneById(Integer value) {
        return fetchOne(SClass.S_CLASS.ID, value);
    }

    /**
     * Fetch records that have classname IN (values)
     */
    public List fetchByClassname(String... values) {
        return fetch(SClass.S_CLASS.CLASSNAME, values);
    }
}

 

4. pojos包下面顾名思义就是实体类了(注:只展示一个类的代码,方便理解)

/*
 * This file is generated by jOOQ.
 */
package com.demo.main.jooq.tables.pojos;


import java.io.Serializable;

import javax.annotation.Generated;


/**
 * This class is generated by jOOQ.
 */
@Generated(
    value = {
        "http://www.jooq.org",
        "jOOQ version:3.11.9"
    },
    comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class SClass implements Serializable {

    private static final long serialVersionUID = 1278405963;

    private Integer id;
    private String  classname;

    public SClass() {}

    public SClass(SClass value) {
        this.id = value.id;
        this.classname = value.classname;
    }

    public SClass(
        Integer id,
        String  classname
    ) {
        this.id = id;
        this.classname = classname;
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getClassname() {
        return this.classname;
    }

    public void setClassname(String classname) {
        this.classname = classname;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("SClass (");

        sb.append(id);
        sb.append(", ").append(classname);

        sb.append(")");
        return sb.toString();
    }
}

 

 5.Record包下面记录一些表字段(注:只展示一个类的代码,方便理解)

/*
 * This file is generated by jOOQ.
 */
package com.demo.main.jooq.tables.records;


import com.demo.main.jooq.tables.SClass;

import javax.annotation.Generated;

import org.jooq.Field;
import org.jooq.Record1;
import org.jooq.Record2;
import org.jooq.Row2;
import org.jooq.impl.UpdatableRecordImpl;


/**
 * This class is generated by jOOQ.
 */
@Generated(
    value = {
        "http://www.jooq.org",
        "jOOQ version:3.11.9"
    },
    comments = "This class is generated by jOOQ"
)
@SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class SClassRecord extends UpdatableRecordImpl implements Record2 {

    private static final long serialVersionUID = -7367157;

    /**
     * Setter for demo.s_class.id.
     */
    public void setId(Integer value) {
        set(0, value);
    }

    /**
     * Getter for demo.s_class.id.
     */
    public Integer getId() {
        return (Integer) get(0);
    }

    /**
     * Setter for demo.s_class.classname.
     */
    public void setClassname(String value) {
        set(1, value);
    }

    /**
     * Getter for demo.s_class.classname.
     */
    public String getClassname() {
        return (String) get(1);
    }

    // -------------------------------------------------------------------------
    // Primary key information
    // -------------------------------------------------------------------------

    /**
     * {@inheritDoc}
     */
    @Override
    public Record1 key() {
        return (Record1) super.key();
    }

    // -------------------------------------------------------------------------
    // Record2 type implementation
    // -------------------------------------------------------------------------

    /**
     * {@inheritDoc}
     */
    @Override
    public Row2 fieldsRow() {
        return (Row2) super.fieldsRow();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Row2 valuesRow() {
        return (Row2) super.valuesRow();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Field field1() {
        return SClass.S_CLASS.ID;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Field field2() {
        return SClass.S_CLASS.CLASSNAME;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer component1() {
        return getId();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String component2() {
        return getClassname();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer value1() {
        return getId();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String value2() {
        return getClassname();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SClassRecord value1(Integer value) {
        setId(value);
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SClassRecord value2(String value) {
        setClassname(value);
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public SClassRecord values(Integer value1, String value2) {
        value1(value1);
        value2(value2);
        return this;
    }

    // -------------------------------------------------------------------------
    // Constructors
    // -------------------------------------------------------------------------

    /**
     * Create a detached SClassRecord
     */
    public SClassRecord() {
        super(SClass.S_CLASS);
    }

    /**
     * Create a detached, initialised SClassRecord
     */
    public SClassRecord(Integer id, String classname) {
        super(SClass.S_CLASS);

        set(0, id);
        set(1, classname);
    }
}

 

你可能感兴趣的:(Jooq从入门到精通)