MyBatisPlus ---- 条件构造器和常用接口
- 1. wapper介绍
- 2. QueryWrapper
- a>例1:组装查询条件
- b>例2:组装排序条件
- c>例3:组装删除条件
- d>例4:条件的优先级
- e>例5:组装select子句
- f>例6:实现子查询
- 3. UpdateWrapper
- 4. condition
- 思路一
- 思路二
- 5. LambdaQueryWrapper
- 6. LambdaUpdateWrapper
1. wapper介绍
- Wrapper:条件构造抽象类,最顶端父类
- AbstractWrapper:用于查询条件封装,生成 sql 的 where 条件
- QueryWrapper:查询条件封装
- UpdateWrapper:Update 条件封装
- AbstractLambdaWrapper:使用 Lambda 语法
- LambdaQueryWrapper:用于 Lambda 语法使用的查询 Wrapper
- LambdaUpdateWrapper:Lambda 更新封装 Wrapper
- AbstractWrapper:用于查询条件封装,生成 sql 的 where 条件
2. QueryWrapper
a>例1:组装查询条件
@Test
public void test01(){
//查询用户名包含a,年龄在20到30之间,并且邮箱不为null的用户信息
//SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (user_name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("user_name", "a").between("age", 20, 30).isNotNull("email");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(user -> System.out.println(user));
}
由于前面设置了 is_deleted 逻辑删除,所以这里查询到的是,为经过逻辑删除的信息
b>例2:组装排序条件
@Test
public void test02(){
//按照年龄升序查询用户,如果年龄相同则按id 降序排列
//SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user WHERE is_deleted=0 ORDER BY age ASC,id DESC
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByAsc("age").orderByDesc("id");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(user -> System.out.println(user));
}
c>例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);
}
由于前面设置了逻辑删除,所以这里的删除,就仅是逻辑删除,而不是物理删除
d>例4:条件的优先级
@Test
public void test04(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//将(年龄大于20并且用户名中包含有a)或者邮箱为null的用户信息修改
//UPDATE t_user SET age=?, email=? WHERE is_deleted=0 AND (user_name LIKE ? AND age > ? OR email IS NULL)
queryWrapper.like("user_name", "a").gt("age", 20).or().isNull("email");
User user = new User();
user.setAge(18);
user.setEmail("user@fickler.com");
int result = userMapper.update(user, queryWrapper);
System.out.println(result);
}
querywrapper 不仅可以用于查询,也可以用于更改
@Test
public void test05(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改
//UPDATE t_user SET age=?, email=? WHERE is_deleted=0 AND (user_name LIKE ? AND (age > ? OR email IS NULL))
//lambda表达式内的逻辑优先运算
queryWrapper.like("user_name", "a").and(i -> i.gt("age", 20).or().isNull("email"));
User user = new User();
user.setAge(18);
user.setEmail("user@fickler.com");
int result = userMapper.update(user, queryWrapper);
System.out.println(result);
}
e>例5:组装select子句
@Test
public void test06(){
//查询用户信息的username和age字段
//SELECT user_name,age FROM t_user WHERE is_deleted=0
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("user_name", "age");
//selectMaps()返回Map集合列表,通常配合select()使用,避免User对象中没有被查询到列值为null
List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
maps.forEach(map -> System.out.println(map));
}
f>例6:实现子查询
@Test
public void test07(){
//查询id小于等于3的用户信息
//SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (uid IN (select uid from t_user where uid <= 3))
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.inSql("uid", "select uid from t_user where uid <= 3");
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(user -> System.out.println(user));
}
3. UpdateWrapper
@Test
public void test08(){
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
//将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改
//UPDATE t_user SET age=?,email=? WHERE is_deleted=0 AND (user_name LIKE ? AND (age > ? AND email IS NOT NULL))
//lambda表达式内的逻辑优先运算
updateWrapper.set("age", 30).set("email", "ceshi@qq.com").like("user_name", "a").and(i -> i.gt("age", 18).isNotNull("email"));
int result = userMapper.update(null, updateWrapper);
System.out.println(result);
}
4. condition
在真正开发的过程中,组装条件是常见的功能,而这些条件数据来源与用户输入,是可选的,因此我们在组装这些条件时,必须先判断用户是否选择了这些条件,若选择则需要组装该条件,若没有选择则一定不能组装,以避免影响 SQL 执行的结果
思路一
@Test
public void test09() {
//定义查询条件,有可能为null(用户为输入)
String username = null;
Integer ageBegin = 10;
Integer ageEnd = 24;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//StringUtils.isNotBlank()判断字符串是否不为空且长度不为0且不由空白符(whitespace)构成
if (StringUtils.isNotBlank(username)) {
queryWrapper.like("username", "a");
}
if (ageBegin != null) {
queryWrapper.ge("age", ageBegin);
}
if (ageEnd != null) {
queryWrapper.le("age", ageEnd);
}
//SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (age >= ? AND age <= ?)
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(user -> System.out.println(user));
}
思路二
上面的实现方案没有问题,但是代码比较复杂,我们可以使用带 condition 参数的重载方法构建查询条件,简化代码的编写
@Test
public void test10() {
//定义查询条件,有可能为null(用户为输入)
String username = null;
Integer ageBegin = 10;
Integer ageEnd = 24;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//StringUtils.isNotBlank()判断字符串是否不为空且长度不为0且不由空白符(whitespace)构成
queryWrapper.like(StringUtils.isNotBlank(username), "user_name", "a").ge(ageBegin != null, "age", ageBegin).le(ageEnd != null, "age", ageEnd);
//SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (age >= ? AND age <= ?)
List<User> users = userMapper.selectList(queryWrapper);
users.forEach(user -> System.out.println(user));
}
5. LambdaQueryWrapper
@Test
public void test11() {
//定义查询条件,有可能为null(用户为输入)
String username = null;
Integer ageBegin = 10;
Integer ageEnd = 24;
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//StringUtils.isNotBlank()判断字符串是否不为空且长度不为0且不由空白符(whitespace)构成
lambdaQueryWrapper.like(StringUtils.isNotBlank(username), User::getName, "a").ge(ageBegin != null, User::getAge, ageBegin).le(ageEnd != null, User::getAge, ageEnd);
//SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (age >= ? AND age <= ?)
List<User> users = userMapper.selectList(lambdaQueryWrapper);
users.forEach(user -> System.out.println(user));
}
使用
User::getName
的方式来确定属性名,一定程度上避免了实体类中属性名和数据库中的属性名不同的问题
6. LambdaUpdateWrapper
@Test
public void test12(){
//组装set子句
LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//lambda表达式内的逻辑优先运算
lambdaUpdateWrapper.set(User::getAge, 18).set(User::getEmail, "ceshi@qq.com").like(User::getName, "a").and(i -> i.lt(User::getAge, 24).or().isNull(User::getEmail));
//UPDATE t_user SET age=?,email=? WHERE is_deleted=0 AND (user_name LIKE ? AND (age < ? OR email IS NULL))
int result = userMapper.update(null, lambdaUpdateWrapper);
System.out.println(result);
}