文章目录
- 五、条件构造器和常用接口
- 1、wapper介绍
- 2、QueryWrapper
- 2.1 例1:组装查询条件
- 2.2 例2:组装排序条件
- 2.3 例3:组装删除条件
- 2.4 例4:条件的优先级
- 2.5 例5:组装select子句
- 2.6 例6:实现子查询
- 3、UpdateWrapper
- 4、condition
- 4.1 思路一
- 4.2 思路二:
- 5、LambdaQueryWrapper
- 6、LambdaUpdateWrapper
【尚硅谷】MyBatisPlus教程-讲师:杨博超
失败,是正因你在距成功一步之遥的时候停住了脚步。
五、条件构造器和常用接口
1、wapper介绍
- Wrapper : 条件构造抽象类,最顶端父类
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
- QueryWrapper : 查询条件封装
- UpdateWrapper : Update 条件封装
- AbstractLambdaWrapper : 使用Lambda 语法
- LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper
- LambdaUpdateWrapper : Lambda 更新封装Wrapper
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
2、QueryWrapper
2.1 例1:组装查询条件
@Test
public void test01() {
// 查询用户名包含a,年龄在20到30之间,并且邮箱不为null的用户信息
// SELECT id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name", "a")
.between("age", 20, 30)
.isNotNull("email");
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
2.2 例2:组装排序条件
@Test
public void test02() {
// 按年龄降序查询用户,如果年龄相同则按id升序排列
// SELECT id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 ORDER BY age DESC,id ASC
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("age")
.orderByAsc("id");
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
2.3 例3:组装删除条件
@Test
public void test03() {
// 删除email为空的用户
// UPDATE t_user SET is_deleted=1 WHERE is_deleted=0 AND (email IS NULL)
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.isNull("email");
//条件构造器也可以构建删除语句的条件
int result = userMapper.delete(queryWrapper);
System.out.println("result:" + result);
}
2.4 例4:条件的优先级
@Test
public void test04() {
//将(年龄大于20并且用户名中包含有a)或邮箱为null的用户信息修改
// UPDATE t_user SET email=? WHERE is_deleted=0 AND (name LIKE ? AND age > ? OR email IS NULL)
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
User user = new User();
user.setEmail("123@qq.com");
queryWrapper.like("name", "a")
.gt("age", 20)
.or()
.isNull("email");
int result = userMapper.update(user, queryWrapper);
System.out.println("result:" + result);
}
@Test
public void test05() {
// 将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改
// UPDATE t_user SET email=? WHERE is_deleted=0 AND (name LIKE ? AND (age > ? OR email IS NULL))
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name", "a")
.and(i -> i.gt("age", 20).or().isNull("email"));
User user = new User();
user.setEmail("zzzz@qq.com");
int result = userMapper.update(user, queryWrapper);
System.out.println("result" + result);
}
2.5 例5:组装select子句
@Test
public void test06() {
// 查询用户信息的username和age字段
// SELECT name,age FROM t_user WHERE is_deleted=0
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("name", "age");
// selectMaps()返回Map集合列表,通常配合select()使用,避免User对象中没有被查询到的列值为null
List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
maps.forEach(System.out::println);
}
2.6 例6:实现子查询
@Test
public void test07() {
// 查询id小于等于3的用户信息
// SELECT id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (id IN (select id from t_user where id <= 3 and is_deleted = 0))
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.inSql("id", "select id from t_user where id <= 3 and is_deleted = 0");
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
3、UpdateWrapper
@Test
public void test08() {
// 将(年龄大于20或邮箱为null)并且用户名中包含有a的用户信息修改
// 组装set子句以及修改条件
// UPDATE t_user SET age=?,email=? WHERE is_deleted=0 AND (name LIKE ? AND (age > ? OR email IS NULL))
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
// lambda表达式内的逻辑优先运算
updateWrapper.set("age", 18)
.set("email", "user@qq.com")
.like("name", "a")
.and(i -> i.gt("age", 20).or().isNull("email"));
int result = userMapper.update(null, updateWrapper);
System.out.println("result" + result);
}
4、condition
在真正开发的过程中,组装条件是常见的功能,而这些条件数据来源于用户输入,是可选的,因此我们在组装这些条件时,必须先判断用户是否选择了这些条件,若选择则需要组装该条件,若没有选择则一定不能组装,以免影响SQL执行的结果
4.1 思路一
@Test
public void test09() {
// //定义查询条件,有可能为null(用户未输入或未选择)
String name = "";
Integer ageBegin = 10;
Integer ageEnd = 24;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// StringUtils.isNotBlank()判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成
// SELECT id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (age >= ? AND age <= ?)
if (StringUtils.isNotBlank(name)) {
queryWrapper.like("name", name);
}
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);
}
4.2 思路二:
上面的实现方案没有问题,但是代码比较复杂,我们可以使用带condition参数的重载方法构建查询条件,简化代码的编写
@Test
public void test10() {
// //定义查询条件,有可能为null(用户未输入或未选择)
String name = "";
Integer ageBegin = 10;
Integer ageEnd = 24;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// StringUtils.isNotBlank()判断某字符串是否不为空且长度不为0且不由空白符(whitespace)构成
// SELECT id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (age >= ? AND age <= ?)
queryWrapper.like(StringUtils.isNotBlank(name), "name", name)
.ge(ageBegin != null, "age", ageBegin)
.le(ageEnd != null, "age", ageEnd);
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
5、LambdaQueryWrapper
@Test
public void test11() {
// 定义查询条件,有可能为null(用户未输入)
String name = "a";
Integer ageBegin = 10;
Integer ageEnd = 24;
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
// 避免使用字符串表示字段,防止运行时错误
// SELECT id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (name LIKE ? AND age >= ? AND age <= ?)
queryWrapper.like(StringUtils.isNotBlank(name), User::getName, name)
.ge(ageBegin != null, User::getAge, ageBegin)
.le(ageEnd != null, User::getAge, ageEnd);
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
6、LambdaUpdateWrapper
@Test
public void test12() {
// 组装set子句
// 修改名字包含a并且年龄小于24或者邮箱为null的用户
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.set(User::getAge, 18)
.set(User::getEmail, "zhangsan@qq.com")
.like(User::getName, "a")
.and(i -> i.lt(User::getAge, 24).or().isNull(User::getEmail));
// lambda表达式内的逻辑优先运算
// UPDATE t_user SET age=?,email=? WHERE is_deleted=0 AND (name LIKE ? AND (age < ? OR email IS NULL))
int result = userMapper.update(null, updateWrapper);
System.out.println("result" + result);
}