条件构造器
1、Wrapper介绍
我们先查看以下Wrapper抽象类的继承体系 标出了我们常用的子类
- Wrapper:条件构造类,是父类
- AbstractWrapper : 用于查询条件的封装,生成sql的where条件
- QueryWrapper : 查询条件封装
- UpdateWrapper : Update条件的封装
- AbstractLambdaWrapper : 使用Lambda语法
- LambdaQueryWrapper:用于Lambda语法使用的查询Wrapper
- LambdaUpdateWrapper: 更新封装的Wrapper(Lambda)
- AbstractWrapper : 用于查询条件的封装,生成sql的where条件
2、QueryWrapper使用
SpringBoot测试
@SpringBootTest public class MybatisPlusWrapperTest { @Autowired private UserMapper userMapper; }
①组装查询条件
@Test
public void test01(){
//查询用户名包含a 年龄18-25之间 邮箱信息不为Null的用户信息
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.like("user_name","a")
.between("age",18,25)
.isNotNull("email");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
②组装排序条件
@Test
public void test02(){
//将age字段按照降序排序 id字段升序排序
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.orderByDesc("age").orderByAsc("id");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
③组装删除条件
@Test
public void test03(){
//将email为null的字段进行删除
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.isNull("email");
//UPDATE t_user SET ready_delete=1 WHERE ready_delete=0 AND (email IS NULL)
//因为我们有逻辑删除 所以执行删除操作时 实际上执行的是修改
int result = userMapper.delete(queryWrapper);
System.out.println("受影响的行数: " + result);
}
④条件的优先级
@Test
public void test04(){
//将年龄大于18并且用户名中含有a 或 邮箱为Null的用户进行修改
//UPDATE t_user SET age=?, email=?
//WHERE ready_delete=0 AND (user_name LIKE ? AND age > ? OR email IS NULL)
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
queryWrapper.like("user_name","a")
.gt("age",18).or().isNull("email");
User user = new User();
user.setAge(18);
user.setEmail("[email protected]");
int update = userMapper.update(user, queryWrapper);
System.out.println("受影响的行数:" + update);
}
@Test
public void test05(){
//UPDATE t_user SET age=?, email=?
// WHERE ready_delete=0 AND (user_name LIKE ? AND (age > ?) OR email IS NULL)
QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
//lambda表达式内的逻辑 优先运算
queryWrapper.like("user_name","a").and(i -> i.gt("age",18))
.or().isNull("email");
User user = new User();
user.setAge(18);
user.setEmail("[email protected]");
int update2 = userMapper.update(user, queryWrapper);
System.out.println("受影响的行数:" + update2);
}
⑤组装select子句
@Test
public void test06(){
//查询用户信息username,age的字段
//SELECT user_name,age FROM t_user WHERE ready_delete=0
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("user_name","age");
//selectMaps() 返回map集合 通常配合select()使用
//这样做的目的是避免没有被查询到的列值为null
List<Map<String, Object>> lists = userMapper.selectMaps(queryWrapper);
lists.forEach(System.out::println);
}
⑥子查询
@Test
public void test07(){
//查询id小于等于5的用户 信息 (子查询实现)
//SELECT uid AS id,user_name AS name,age,email,ready_delete FROM t_user
// WHERE ready_delete=0 AND (uid IN (select uid from t_user where uid <=5))
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.inSql("uid","select uid from t_user where uid <=5");
List<User> lists = userMapper.selectList(queryWrapper);
lists.forEach(System.out::println);
}
3、UpdateWrapper使用
@Test
public void test08(){
//将年龄大于15或邮箱为null 并且 用户名中含有a的用户信息修改
// UPDATE t_user SET age=?,email=?
// WHERE ready_delete=0 AND (user_name LIKE ? AND (age > ? OR email IS NULL))
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.set("age",18).set("email","[email protected]").like("user_name","a")
.and(i -> i.gt("age",15).or().isNull("email"));
int result = userMapper.update(null, updateWrapper);
System.out.println("result:" +result);
}
4、condition
在开发中,组装条件是常见的,这些数据来源于用户的输入,是可选的,因此在组装这些条件时,必须先判断用户是否选择了这些条件,若选择则需要组装该条件,反之,以免影响sql执行的效率问题。
@Test
public void test09(){
//模拟开发场景
String username = "";
Integer ageBegin = 20;
Integer ageEnd = 30;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
if (StringUtils.isNotBlank(username)){
//判断某个字符是否不为空字符串 不为null,不为空字符串
queryWrapper.like("user_name",username);
}
if (ageBegin != null){
queryWrapper.ge("age",ageBegin);
}
if (ageEnd != null){
queryWrapper.le("age",ageEnd);
}
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
简化开发
@Test
public void test10(){
String username = "a";
Integer ageBegin = 20;
Integer ageEnd = 30;
//使用condition条件进行组装
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(username),"user_name",username)
.ge(ageBegin != null,"age",ageBegin)
.le(ageEnd != null,"age",ageEnd);
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
5、LambdaQueryWrapper
@Test
public void test11(){
//定义查询条件 可能为null 用户未输入
String username = "a";
Integer ageBegin = 10;
Integer ageEnd = 24;
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
//避免使用字符串表示字段 防止运行时异常
queryWrapper.like(StringUtils.isNotBlank(username),User::getName,username)
.ge(ageBegin != null, User::getAge,ageBegin)
.le(ageEnd != null,User::getAge,ageEnd);
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
6、LambdaUpdateWrapper
@Test
public void test12(){
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(User::getAge,18)
.set(User::getEmail,"[email protected]")
.like(User::getName,"a")
.and(i -> i.lt(User::getAge,22).or().isNull(User::getEmail));
//lambda表达式优先运算
User user = new User();
int update = userMapper.update(user, updateWrapper);
System.out.println(update);
}