目录
①. 条件构造器与常用的接口
②. QueryWrapper
③. UpdateWrapper
④. Condition——简化代码开发
⑤. LambdaQueryWrapper
⑥. LambdaUpdateWrapper
条件构造器(Wrapper)的作用:
MyBatis-Plus提供的通用Mapper中,删改查(没有insert)的方法通常会需要我们传入一个条件构造器,用来生成最后执行的SQL语句。
比如List selectList( Wrapper wrapper)方法,当我们传入null时,默认生成的语句就是select * from xxx,这条语句没有查询条件,排序,子查询,模糊查询并且固定查询所有的字段,如果我们需要一些复杂的查询条件,就可以使用条件构造器来指定我们的SQL需要拼接那些查询条件。
查询用户名包含a,年龄在20到30直接,邮箱信息不为空的信息
(注释中的sql语句就是mybatis-plus为我们生成的sql语句,is_deleted字段为逻辑删除字段,在对数据库进行操作时都要筛选出未被逻辑删除的字段再对其进行CRUD操作)
@DisplayName("条件构造器组装查询条件")
@Test
public void test01(){
// SELECT id,name,age,email,is_deleted FROM user WHERE is_deleted=0 AND (name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
QueryWrapper wrapper=new QueryWrapper<>();
wrapper.like("name","a").
between("age",20,30).
isNotNull("email");
List list = userMapper.selectList(wrapper);
list.forEach(System.out::println);
}
查询用户信息,按照年龄降序排序,若年龄相同,则按id升序排序
@DisplayName("条件构造器组装排序条件")
@Test
public void test02(){
//SELECT id,name,age,email,is_deleted FROM user WHERE is_deleted=0 ORDER BY age DESC,id ASC
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.orderByDesc("age").orderByAsc("id");
List list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
删除邮箱地址为null的数据
@DisplayName("条件构造器删除功能")
@Test
public void test03(){
//UPDATE user SET is_deleted=1 WHERE is_deleted=0 AND (email IS NULL)
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.isNull("email");
int delete = userMapper.delete(queryWrapper);
System.out.println(delete);
}
@DisplayName("使用queryWrapper来实现修改功能")
@Test
public void test04(){
//1.先查询出要修改的数据
//将(年龄大于20并且用户名中包含a)或者邮箱为Null的用户进行修改
//UPDATE user SET name=? WHERE is_deleted=0 AND (age > ? AND name LIKE ? OR email IS NULL)
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.gt("age",20)
.like("name","a")
.or()
.isNull("email");
//2.再对其进行修改
User user=new User();
user.setName("桥桥");
int update = userMapper.update(user, queryWrapper);
System.out.println(update);
}
@DisplayName("使用queryWrapper来实现修改功能(查询的优先级)")
@Test
public void test05(){
//将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改
//UPDATE user SET name=? WHERE is_deleted=0 AND (name LIKE ? AND (age > ? OR email IS NULL))
//lambda中的条件优先执行
//.and(i就是条件构造器的意思)
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.like("name","a")
.and(i->i.gt("age",20).or().isNull("email"));
User user=new User();
user.setName("桥桥");
int update = userMapper.update(user, queryWrapper);
System.out.println(update);
}
查询用户的用户名、年龄
@DisplayName("查询部分字段")
@Test
public void test06(){
//SELECT name,age FROM user WHERE is_deleted=0
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.select("name","age");
List
查询id<=100的用户信息
@DisplayName("条件构造器实现子查询")
@Test
public void test07(){
// SELECT id,name,age,email,is_deleted FROM user WHERE is_deleted=0 AND (id IN (select id from user where id <=6))
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.inSql("id","select id from user where id <=6");
queryWrapper.like("name","桥");
List list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改
@DisplayName("updateWrapper设置修改条件")
@Test
public void test08(){
//UPDATE user SET name=? WHERE is_deleted=0 AND (name LIKE ? AND (age > ? OR email IS NULL))
UpdateWrapper updateWrapper=new UpdateWrapper<>();
updateWrapper.like("name","a")
.and(i->i.gt("age",20).or().isNull("email"))
.set("name","旋旋").set("email","big");
int update = userMapper.update(null, updateWrapper);
System.out.println(update);
}
test09测试模拟了根据前端传过来的姓名、最大最小年龄来进行查询,查询前要先判断传过来的数据是否存在,若不存在则不作为查询条件。
@Test
public void test09(){
String name="";
Integer ageBegin=20;
Integer ageEnd=30;
QueryWrapper queryWrapper=new QueryWrapper<>();
//SELECT id,name,age,email,is_deleted FROM user WHERE is_deleted=0 AND (age >= ? AND age <= ?)
if (StringUtils.isNotBlank(name)){
//isNotBlank判断某个字符串是否不为空,不为Null,不为空白符
queryWrapper.like("name",name);
}
if(ageBegin!=null){
queryWrapper.ge("age",ageBegin);
}
if (ageEnd!=null){
queryWrapper.le("age",ageEnd);
}
List list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
@Test
public void test10(){
//与test09一致,简化,把判断条件放到构造语句中
//SELECT id,name,age,email,is_deleted FROM user WHERE is_deleted=0 AND (age >= ? AND age <= ?)
String name=" ";
Integer ageBegin=20;
Integer ageEnd=30;
QueryWrapper queryWrapper=new QueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(name),"name","a")
.ge(ageBegin!=null,"age",20)
.le(ageEnd!=null,"age",30);
List list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
为了防止我们写错字段名,我们使用lambda函数式接口来访问属性对应的字段名称
@DisplayName("lambdaQueryWrapper测试")
@Test
public void test11(){
//SELECT id,name,age,email,is_deleted FROM user WHERE is_deleted=0 AND (name LIKE ? AND age >= ? AND age <= ?)
String name="a";
Integer ageBegin=20;
Integer ageEnd=30;
LambdaQueryWrapper lambdaQueryWrapper=new LambdaQueryWrapper<>();
lambdaQueryWrapper.like(StringUtils.isNotBlank(name),User::getName,name)
.ge(ageBegin!=null,User::getAge,ageBegin)
.le(ageEnd!=null,User::getAge,ageEnd);
List list = userMapper.selectList(lambdaQueryWrapper);
list.forEach(System.out::println);
}
将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改
@DisplayName("lambda设置修改条件")
@Test
public void test12(){
//UPDATE user SET name=? WHERE is_deleted=0 AND (name LIKE ? AND (age > ? OR email IS NULL))
LambdaUpdateWrapper updateWrapper=new LambdaUpdateWrapper<>();
updateWrapper.like(User::getName,"a")
.and(i->i.gt(User::getAge,20).or().isNull(User::getEmail))
.set(User::getName,"旋旋").set(User::getEmail,"big");
int update = userMapper.update(null, updateWrapper);
System.out.println(update);
}