ORM框架---JOOQ简单介绍

一,什么是JOOQ

继Hibernate Mybatis 之后的又一ORM框架

二,特点

1.get back in control of your sql

2.DSL(Domain Specific Language )风格,代码够简单和清晰

3.支持主流的RDMS�和更多的特性,如self-joins,union,存储过程,复杂的子查询等等。

4.

 

三,使用

pom



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.2.1.RELEASE
		 
	
	com.example
	springdemo
	0.0.1-SNAPSHOT
	spring-demo
	Demo project for Spring Boot

	
		1.8
		3.12.1
		com.mysql.jdbc.Driver
		jdbc:mysql://127.0.0.1:3306/db_test?characterEncoding=UTF-8
		root
		123456
		db_test
	

	
		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			org.springframework.boot
			spring-boot-starter-web
		

		
			mysql
			mysql-connector-java
			runtime
		

		
		
			org.jooq
			jooq
			${jooq.version}
		
		
			org.jooq
			jooq-meta
			${jooq.version}
		
		
			org.jooq
			jooq-codegen
			${jooq.version}
		

		
		
			com.alibaba
			druid
			1.1.6
		

		
			org.springframework.boot
			spring-boot-starter-data-jpa
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
			
				
					org.junit.vintage
					junit-vintage-engine
				
			
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



	
	
		
			jooq
			
			
				
					jooq
				
			
			
				
					
						org.jooq
						jooq-codegen-maven
						${jooq.version}
						
							
								
									generate
								
							
						
						
							
								mysql
								mysql-connector-java
								${mysql.version}
							
						
						
							
								${jdbc.driver}
								${jdbc.url}
								${jdbc.user}
								${jdbc.password}
							
							
								
									org.jooq.meta.mysql.MySQLDatabase
									.*
									
									${jdbc.database.name}
									
										
											BOOLEAN
											(?i:TINYINT(\s*\(\d+\))?(\s*UNSIGNED)?)
										
									
								
								
									false
								

								
								
								
									com.example.demo.jooq
									src/main/java
								
								
									false
									false
								
							
						
					
				
			
		
	


自动生成的代码结构:

ORM框架---JOOQ简单介绍_第1张图片

service

package com.example.demo.service;

import com.example.demo.jooq.Tables;
import com.example.demo.jooq.tables.Student;
import com.example.demo.jooq.tables.records.StudentRecord;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zyl on 2019/11/20.
 */
@Service
public class JooqService {

    @Autowired
    DSLContext dslContext;


    public int insert(){
        int count = dslContext.insertInto(Tables.STUDENT)
                .set(Student.STUDENT.ID,1001012)
                .set(Student.STUDENT.NAME,"朱二龙")
                .set(Student.STUDENT.AGE,18)
                .set(Student.STUDENT.ADDRESS,"湖南")
                .set(Student.STUDENT.GENDER,"male")
                .execute();
        return count;
    }

    public int query(){
        int pageNum  = 2;//页数,从0开始
        int pageSize = 1;//每页数据数
        //查总数
        Integer into = dslContext.selectCount().from(Tables.STUDENT).fetchOne().into(Integer.class);
        System.out.println("查总数: "+ into);

        //分页查询list
        List studentRecords = dslContext.select().from(Tables.STUDENT)
                .orderBy(Tables.STUDENT.ID.desc())
                .limit(pageSize).offset(pageNum-1)
                .fetchInto(StudentRecord.class);

        studentRecords.forEach(s->{
            System.out.println(s.toString());
        });

        return studentRecords.size();
    }

    public int delete(){
        int execute = dslContext.delete(Tables.STUDENT)
                .where(Tables.STUDENT.ID.eq(1001012))
                .execute();
        return execute;
    }
    public int update(){
        int count = dslContext.update(Tables.STUDENT)
                .set(Tables.STUDENT.ADDRESS, "湖南省长沙市")
                .execute();
        return count;
    }






}

OVER

你可能感兴趣的:(技术)