3

Mybatisplus条件构造器Wrapper

MyBatis-Plus官方介绍:

MyBatis-Plus:是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

mybatis是一个用Java编写的持久层框架,使用ORM实现了结果集的封装。既然和数据层打交道,自然需要大量的CURD,条件构造器作为MyBatis-Plus的一大特色,可以极大提高我们开发效率。

条件构造器继承关系:

Wrapper:最顶级抽象类、条件构造抽象类
--AbstractWrapper:继承自Wrapper的抽象类、用于查询条件封装,生成 sql 的 where 条件
----QueryWrapper:继承自AbstractWrapper的类、不支持lambda语法、用于查询
----UpdateWrapper :继承自AbstractWrapper的类、不支持lambda语法、用于修改
----AbstractLambdaWrapper:继承自AbstractWrapper的抽象类、使用Lambda 语法统一处理解析 column
------LambdaQueryWrapper:继承自AbstractLambdaWrapper的类、支持lambda语法、用于查询
------LambdaUpdateWrapper:继承自AbstractLambdaWrapper的类、支持lambda语法、用于修改

常用方法:

方法          描述            等价             例子
eq            等于            =               eq("name","吕布")name="吕布"
ne            不等于          <>              ne("name","吕布")含义:name<>"吕布"
gt            大于            >               gt("age","10")含义:age>10
lt            小于            <               lt("age","10")含义:age<10
ge            大于等于        >=              ge("age","10")含义:age>=10
le            小于等于        <=              le("age","10")含义:age<=10
between       在值1值2之间    between and     between("age","10","18")含义:10<=age<=18
notBetween    不在值1值2之间 not bteween and  notBetween("age","10","18")含义:18<age或age<10
like          模糊查询        like            like("name","吕")含义:name like "%吕%"
notLike       模糊查询        not Like        notLike("name","吕")含义:name not like "%吕%"
likLeft       模糊查询                        likLeft("name","吕")含义:name like "%吕"
likeRight     模糊查询                        likeRight("name","吕")含义:name like "吕%"
isNull        是否为空
isNotNull     字段为不空
in            in             in               age in (10,15,20)含义:age在10,15,20之间
notIn         not  in        not  in          age not in (10,15,20)含义:age不在10,15,20之间
inSql         in(sql)                         inSql("id","select id from table")含义:id in (select id from table)
notInSql      not in(sql)                     notInSql("id","select id from table")含义:id not in (select id from table)
groupBy       分组           group by         groupBy(id,name)含义:group by id,name
orderByAsc    升序                            orderByAsc(id,name)含义:order by id asc,name asc
orderByDesc   降序                            orderByDesc(id,name)含义:order by id desc,name desc
orderBy       排序           order by         orderBy(true,true,"id","name")含义:order by id asc,name asc
having        having
or            或             or               eq("id","1") or eq("id","2")含义:id='1' or id='2'
and           且             and              eq("id","1") and eq("id","2")含义:id='1' and id='2'
apply         拼接sql
last          无视优化规则直接拼接sql最后
exists        拼接
notExists     拼接
nested        正常嵌套,不带and或者or


案例:

test表:

id     name   sex  age
1001   张三   男   18
1002   李四   男   18
1003   王五   女   16
1004   赵六   女   16

test实体类:

public class Test{
    private String id;
    private String name;
    private String sex;
    private String age;
}

查询sex为男的test数据

  • 使用QueryWrapper构造器形式:

      QueryWrapper<Test> queryWrapper = new QueryWrapper();
      queryWrapper.eq("sex","男");
      List<Test> resultList = testService.list(queryWrapper);

    或者

      List<Test> resultList = testService.list(Wrappers.<Test>query().eq("sex","男"));
    
  • 使用LambdaQueryWrapper构造器形式:

      LambdaQueryWrapper<Test> lambdaQueryWrapper = new LambdaQueryWrapper();
      lambdaQueryWrapper.eq(Test::getSex,"男");
      List<Test> resultList = testService.list(lambdaQueryWrapper);

    或者

      List<Test> resultList = testService.list(Wrappers.<Test>lambdaQuery().eq(Test::getSex,"男"));
    

修改id为'1001'的数据的name为'小张三'

  • 使用UpdateWrapper构造器形式:

      UpdateWrapper<Test> queryWrapper = new UpdateWrapper();
      queryWrapper.eq("id","1001");
      queryWrapper.set("name","小张三");
      testService.update(queryWrapper);

    或者

      testService.update(Wrappers.<Test>update().eq("id","1001").set("name","小张三"));
    
  • 使用LambdaUpdateWrapper构造器形式:

      LambdaUpdateWrapper<Test> lambdaUpdateWrapper = new LambdaUpdateWrapper();
      lambdaUpdateWrapper.eq(Test::getId,"1001");
      lambdaUpdateWrapper.set(Test::getName,"小张三");
      testService.update(lambdaUpdateWrapper);

    或者

       testService.update(Wrappers.<Test>lambdaUpdate().eq(Test::getId,"1001").set(Test::getName,"小张三"));

    删除'sex'为'男'的数据

  • 使用QueryWrapper构造器形式:

      QueryWrapper<Test> queryWrapper = new QueryWrapper();
      queryWrapper.eq("sex","男");
      testService.remove(queryWrapper);

    或者

      testService.remove(Wrappers.<Test>query().eq("sex","男"));
  • 使用LambdaQueryWrapper构造器形式:

      LambdaQueryWrapper<Test> lambdaQueryWrapper = new LambdaQueryWrapper();
      lambdaQueryWrapper.eq(Test::getSex,"男");
      testService.remove(lambdaQueryWrapper);

    或者

      testService.remove(Wrappers.<Test>lambdaQuery().eq(Test::getSex,"男"));

wasdquiop
7 声望4 粉丝