MyBatis-Plus——使用查询构造器Wrapper & 简单分页操作

简介: MyBatis-Plus——使用查询构造器Wrapper & 简单分页操作

1.查询构造器:Wrapper


QueryWrapper(LambdaQueryWrapper)UpdateWrapper(LambdaUpdateWrapper) 的父类用于生成 sql where 条件, entity 属性也用于生成 sql where
件。 MP3.x开始支持lambda表达式,LambdaQueryWrapperLambdaUpdateWrapper支持 lambda 表达式的构造查询条件。


我们这里主要使用的是QueryWrapper这个类,它的父类AbstractWrapper实现了一个接口Compare<This, R>,在这个接口中,有大量用来拼接where子句中相关条件的方法。

1.    allEq:基于map的相等

2.    eq 等于 =

3.    ne 不等于 <>

4.    gt 大于 >

5.    ge 大于等于 >=

6.    lt 小于 <

7.    le 小于等于 <=

8.    between BETWEEN 1 AND 2

9.    notBetween NOT BETWEEN 1 AND 2

10.like LIKE '%%'

11.notLike NOT LIKE '%%'

12.likeLeft LIKE '%'

13.likeRight LIKE '%'

14.isNull 字段 IS NULL

15.isNotNull 字段 IS NOT NULL

16.in 字段 IN (value1, value2, ...)

17.notIn 字段 NOT IN (value1, value2, ...)

18.inSql 字段 IN ( sql 语句 )
: inSql("age", "1,2,3")--->age in (1,2,3,4,5,6)
: inSql("id", "select id from table where id < 3")--->id
in (select id from table where id < 3)

19.notInSql 字段 NOT IN ( sql 语句 )

20.groupBy GROUP BY 字段

21.orderByAsc 升序 ORDER BY 字段, ... ASC

22.orderByDesc 降序 ORDER BY 字段, ... DESC

23.orderBy 自定义字段排序 orderBy(true, true, "id", "name")--->order by id ASC,name ASC

24.having 条件分组

25.or OR 语句,拼接 + OR 字段=

26.and AND 语句,拼接 + AND 字段=

27.apply 拼接 sql

28.last sql 语句后拼接自定义条件

29.exists 拼接 EXISTS ( sql 语句 )
: exists("select id from table where age =
1")--->exists (select id from table where age = 1)

30.notExists 拼接 NOT EXISTS ( sql 语句 )

31.nested 正常嵌套不带 AND 或者 OR

2.案例详解


这篇文章中所用到的数据库表、实体类,mapper接口均和上一篇文章(链接:https://blog.csdn.net/weixin_43823808/article/details/118412431)相同,所以这里不再给出代码了,只是这篇文章中,不再使用xml映射文件了。

我将使用Wrapper这个查询构造器来实现查询操作。


2.1 测试方法

    @Test
    public void testAllEq() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //将查询条件封装到Map集合中,and
        Map<String,Object> map=new HashMap<>();
        map.put("name","张三");
        map.put("age",22);
        qw.allEq(map);
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testAllEq2() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //将查询条件封装到Map集合中
        Map<String,Object> map=new HashMap<>();
        map.put("name","张三");
        map.put("age",null);
        /**
         * 第二个参数为 true:处理null值,where条件中加入 字段 IS NULL
         *         为 false:直接忽略null值,不添加到where子句中
         */
        qw.allEq(map,true);
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

qw.allEq(map,true)的执行结果


qw.allEq(map,false)的执行结果 


    @Test
    public void testEq() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //eq等于
        qw.eq("name","李四");
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }


    @Test
    public void testNe() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //ne不等于
        qw.ne("name","李四");
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testGt() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //gt大于,ge大于等于
        //lt小于,le小于等于
        qw.gt("age",25);
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testBetween() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //between在两个值的范围之间(闭区间)
        //notBetween不在两个值范围之间(对应上面集合的补集)
        qw.between("age",30,36);
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testLike() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //like 匹配值 "%值%"
        //notLike 不匹配 "%值%"
        qw.like("name","周");
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testLikeLeft() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //likeLeft 匹配 like "%值"
        //likeRight 匹配 like "值%"
        qw.likeLeft("name","三");
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testIsNull() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //isNull 判断字段值为 null
        //isNotNull 字段值不为 null
        qw.isNull("age");
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testIn() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //in 在这个值列表中,在列表中的都是符合条件的。
        //notIn 不在列表中的
//        List<String> list=new ArrayList<>();
//        list.add("张三");
//        list.add("李四");
//        qw.in("name",list);
        //下面这行等价于上面注释掉的四行
        qw.in("name","张三","李四");
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testInSql() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //inSql 常用来做子查询 类似 in()
        //notInSql 类似 notIn()
        qw.inSql("age","select age from student where id=2");
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testGroupBy() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        qw.select("status,count(*)");
        //groupBy 基于多个字段分组
        qw.groupBy("status");
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testOrderByAsc() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //orderByAsc 按字段升序
        //orderByDesc 按字段降序
        //orderBy 每个字段指定排序方向
        qw.orderByAsc("age");
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testOr() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //or 连接条件用 or,默认是 and
        //and 连接条件用 and
        qw.eq("name","张三").or().eq("age",24);
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testLast() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //last 拼接 sql 语句
        qw.eq("name","张三").or().eq("age",24).last("limit 1");
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }

    @Test
    public void testExists() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        //exists 拼接 exists ( sql 语句 )
        //notExists 是 exists 的相反操作
        qw.exists("select id,name from student where age>36");
        List<Student> students=mapper.selectList(qw);
        students.forEach( student -> System.out.println(student));
    }


1.2 分页操作

首先添加分页插件,在原先的SSM中,我们需要在xml文件中使用<bean>标签进行声明配置;现在升级为了SpringBoot,就告别xml了,我们直接定义一个config包,在其中创建一个类,使用@Configuration注解标记该类为配置类,在其中使用@Bean注解就可以了。

package com.szh.mybatisplus.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 *
 */
@Configuration
public class MyConfig {
    //分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

下面是测试方法。

    @Test
    public void testPage() {
        QueryWrapper<Student> qw=new QueryWrapper<>();
        qw.gt("age",20);
        qw.orderByAsc("id");
        //设置分页的数据
        IPage<Student> page=new Page<>();
        page.setCurrent(1);//第1页
        page.setSize(3);//每页3条记录
        IPage<Student> result = mapper.selectPage(page, qw);
        List<Student> students = result.getRecords();
        System.out.println("总页数:" + result.getPages());
        System.out.println("总记录数:" + result.getTotal());
        System.out.println("当前页码:" + result.getCurrent());
        System.out.println("每页大小:" + result.getSize());
        students.forEach( student -> System.out.println(student));
    }

相关文章
|
1月前
|
SQL XML Java
8、Mybatis-Plus 分页插件、自定义分页
这篇文章介绍了Mybatis-Plus的分页功能,包括如何配置分页插件、使用Mybatis-Plus提供的Page对象进行分页查询,以及如何在XML中自定义分页SQL。文章通过具体的代码示例和测试结果,展示了分页插件的使用和自定义分页的方法。
8、Mybatis-Plus 分页插件、自定义分页
|
2月前
|
SQL
条件构造器,MybatisPlus支持各种复杂的where条件,其实就是Wrapper,eq是等于的意思,相当于等于那个数值,ne就是不等于,gt大于的意思,ge大于等于,QueryWrapper是做
条件构造器,MybatisPlus支持各种复杂的where条件,其实就是Wrapper,eq是等于的意思,相当于等于那个数值,ne就是不等于,gt大于的意思,ge大于等于,QueryWrapper是做
|
1月前
|
Java 数据库 Spring
MyBatisPlus分页插件在SpringBoot中的使用
这篇文章介绍了如何在Spring Boot项目中配置和使用MyBatis-Plus的分页插件,包括创建配置类以注册分页拦截器,编写测试类来演示如何进行分页查询,并展示了测试结果和数据库表结构。
MyBatisPlus分页插件在SpringBoot中的使用
|
18天前
|
SQL Java 关系型数据库
MyBatis-Plus 分页魅力绽放!紧跟技术热点,带你领略数据分页的高效与便捷
【8月更文挑战第29天】在 Java 开发中,数据处理至关重要,尤其在大量数据查询与展示时,分页功能尤为重要。MyBatis-Plus 作为一款强大的持久层框架,提供了便捷高效的分页解决方案。通过封装数据库分页查询语句,开发者能轻松实现分页功能。在实际应用中,只需创建 `Page` 对象并设置页码和每页条数,再通过 `QueryWrapper` 构建查询条件,调用 `selectPage` 方法即可完成分页查询。MyBatis-Plus 不仅生成分页 SQL 语句,还自动处理参数合法性检查,并支持条件查询和排序等功能,极大地提升了系统性能和稳定性。
32 0
|
2月前
|
Java 数据库连接 数据库
mybatis plus 中增删改查及Wrapper的使用
mybatis plus 中增删改查及Wrapper的使用
53 3
|
2月前
|
SQL 关系型数据库 Java
mybatis-分页
1. MyBatis RowBounds分页:先查询所有结果,再进行内存分页。 2. PageHelper插件:自动识别数据库类型并添加对应分页关键字,分两步执行:添加分页查询,然后查询总数。 3. SQL分页:直接在SQL中使用`LIMIT`或`ROWNUM`等进行分页。 4. 数组分页:DAO层查询所有数据,Service层通过`subList`方法实现分页。 5. 拦截器分页:自定义拦截器对特定方法进行拦截,并在SQL语句中添加分页参数。 6. 总结:逻辑分页适合小数据量,物理分页适合大数据量避免内存溢出。物理分页优于逻辑分页。
|
3月前
|
缓存 Java 数据库连接
我们后端程序员不是操作MyBatis的CRUD Boy
大家好,我是南哥。一个对Java程序员进阶成长颇有研究的人,今天我们接着新的一篇Java进阶指南。为啥都戏称后端是CRUD Boy?难道就因为天天怼着数据库CRUD吗?要我说,是这个岗位的位置要的就是你CRUD,你不得不CRUD。哪有公司天天能给你搭建高并发、高可用、大数据框架的活呢,一条业务线总要成长吧,慢慢成熟了就要装修工来缝缝补补、美化美化,也就是CRUD的活。不能妄自菲薄CRUD Boy,我们是后端工程师。今天来指南下操作数据库之MyBatis框架。
我们后端程序员不是操作MyBatis的CRUD Boy
|
1月前
|
SQL Java 数据库连接
后端框架的学习----mybatis框架(5、分页)
这篇文章介绍了如何在MyBatis框架中实现分页功能,包括使用SQL的`limit`语句进行分页和利用MyBatis的`RowBounds`对象进行分页的方法。
|
2月前
|
Java 数据库连接 mybatis
Mybatis查询传递单个参数和传递多个参数用法
Mybatis查询传递单个参数和传递多个参数用法
46 11
|
2月前
|
SQL 缓存 Java
使用MyBatis优化Java持久层操作
使用MyBatis优化Java持久层操作