详细讲解比Jpa更好用的持久化框架:SpringBoot + Jooq实现CURD(二)

前言

查询

查询总数两种方式

 动态条件查询

 删除用法

 修改的两种用法

 新增的两种用法

前言

第一篇已经讲解了什么是Jooq,下面带大家学习一下使用Jooq实现增、删、改、查

查询

jOOQ提供的流式(fluent)API是通过org.jooq.DSLContext接口初始化的,Spring Boot将自动配置一个DSLContext为Spring Bean,并将它跟应用的DataSource连接起来。想要使用DSLContext,只需@Autowire注入它:

package com.demo.main.service.impl;

import java.util.List;

import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.main.jooq.Tables;
import com.demo.main.jooq.tables.pojos.Student;
import com.demo.main.jooq.tables.pojos.StudentVo;

@Service
@Transactional
public class ServiceImpl implements com.demo.main.service.Service{

	@Autowired
	private DSLContext context;
	/**
	 * 查询的一些方法
	 */
	@Override
	public void select() {
		//这个是查询所有   into(对象.class)转换指定对象
		List<Student> list=context.select().from(Tables.STUDENT)
        .fetch().into(Student.class);
		System.out.println("==========查询所有============");
		for (Student student : list) {
			System.out.println(student.toString());
		}
		//查询sum和avg//定义一个Vo对象
		StudentVo listfuction=context
        .select(Tables.STUDENT.AGE.avg(),Tables.STUDENT.AGE.sum())
        .from(Tables.STUDENT)
        .fetchOne().into(StudentVo.class);
		System.out.println(listfuction.toString());
				
		//这个是查询指定字段
		List<Student> list1=context.select(Tables.STUDENT.NAME).from(Tables.STUDENT)
        .fetch().into(Student.class);
		System.out.println("==========查询指定字段============");
		for (Student student : list1) {
			System.out.println(student.toString());
		}
		//根据条件查询 eq等于 lt小于 gt大于
		List<Student> list2=context.select().from(Tables.STUDENT)
        .where(Tables.STUDENT.ID.eq(1)).fetch().into(Student.class);
		System.out.println("==========根据条件查询============");
		for (Student student : list2) {
			System.out.println(student.toString());
		}
		//查询后年龄排序 orederBy分组和写sql没区别
		List<Student> list3=context.select().from(Tables.STUDENT)
        .orderBy(Tables.STUDENT.AGE.desc()).fetch().into(Student.class);
		System.out.println("==========查询后排序============");
		for (Student student : list3) {
			System.out.println(student.toString());
		}
		//查询后分组 groupBy分组
		List<Student> list5=context.select().from(Tables.STUDENT)
        .groupBy(Tables.STUDENT.AGE).fetch().into(Student.class);
		System.out.println("==========查询后分组============");
		for (Student student : list5) {
			System.out.println(student.toString());
		}
		//分页查询 limit分页
		List<Student> list6=context.select().from(Tables.STUDENT)
        .limit(0, 3).fetch().into(Student.class);
		System.out.println("==========分页查询============");
		for (Student student : list6) {
			System.out.println(student.toString());
		}
		//连接查询 VO对象,顺序一致
		List<StudentVo> list4=context
    .select(Tables.STUDENT.ID,Tables.STUDENT.NAME,Tables.STUDENT.AGE,Tables.S_CLASS.CLASSNAME)
    .from(Tables.S_CLASS)
    .innerJoin(Tables.STUDENT).on(Tables.S_CLASS.ID.eq(Tables.STUDENT.ID)).fetch().into(StudentVo.class);
		System.out.println("==========内联查询============");
		for (StudentVo student : list4) {
			System.out.println(student.toString());
		}
		//条件查询对象 .fetchOne()返回单条结果
		Student list7=context.select().from(Tables.STUDENT)
        .where(Tables.STUDENT.ID.eq(3)).fetchOne().into(Student.class);
		System.out.println("==========条件查询对象============");
		System.out.println(list7.toString());

		}
	
}

结果:

查询总数两种方式

		//查询总数方法1
		int count=context.selectCount().from(Tables.STUDENT).fetchOne().into(Integer.class);
		System.out.println("==========条件总数方法1============");
		System.out.println(count);

		//查询总数方法2
		int count2=context.fetchCount(Tables.STUDENT);
		System.out.println("==========条件总数方法2============");
		System.out.println(count2);

 动态条件查询

Condition进行条件拼接

	public void query(String name,String age) {
		Condition condition=DSL.trueCondition();//真实条件
		if(name==null) {
			condition=condition.and(Tables.STUDENT.NAME.eq(name));
		}
		if(age==null) {
			condition=condition.and(Tables.STUDENT.AGE.eq(Integer.parseInt(age)));
		}
		List<Student> list=context.select().from(Tables.STUDENT).where(condition).fetch().into(Student.class);
	}

 删除用法

public void del(Integer id) {
		int del=context.delete(Tables.STUDENT).where(Tables.STUDENT.ID.eq(3)).execute();
	}

 修改的两种用法

	public void up(Student student) {
        //两种方式:1.StudentRecord传值 2.Map<?,?>传值
		StudentRecord studentRecord=new StudentRecord();
		studentRecord.setName(student.getName());
		student.setAge(student.getAge());
    int up=context.update(Tables.STUDENT).set(studentRecord).where(Tables.STUDENT.ID.eq(3)).execute();
	}

 新增的两种用法

	public void insert() {
		//1.当字段比较少的时候可以使用列对值的这种方式
         context.insertInto(Tables.STUDENT).
         columns(Tables.STUDENT.NAME,Tables.STUDENT.AGE).values("小七", 16).execute();//成功返回1,失败返回0

		//2.当字段多时,推荐使用第二种方式,通过set()将字段对应值进行了新增
        StudentRecored studentRecored=new StudentRecored();
        studentRecored.setName("小七")
        studentRecored.setAge(16)
        context.insertInto(Tables.STUDENT).set(StudentRecored).execute();//成功返回1,失败返回0

	}

以上就差不多对Jooq的一些常用方法进行了讲解,看完本篇以后,大家是不是发现这东西一点也不难,只要有sql语法基础的都基本上会写。

猜你喜欢

转载自blog.csdn.net/qq_39940674/article/details/93383177