组装查询Wrapper
@Test
public void test01() {
// 查询用户名包含a,年龄在20-30之间,邮箱信息不为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);
}
@Test
public void test02() {
// 查询用户信息,按照年龄的降序排序,若年龄相同,则按照id升序排序
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("age")
.orderByAsc("id");
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
需要注意的点:
1、操作的字段是数据库中的字段名
2、使用了TableLogic之后,只会筛选没有被逻辑删除的数据
组装删除Wrapper(删除使用QueryWrapper,若使用了TableLogic则还是逻辑删除)
@Test
public void test03() {
// 删除邮箱为null的数据
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.isNull("email");
int result = userMapper.delete(queryWrapper);
//实际执行的sql:UPDATE user SET is_deleted=1 WHERE is_deleted=0 AND (email IS NULL)
System.out.println("result = " + result);
}
使用QueryWrapper来实现update修改功能
@Test
public void test04() {
// 使用QueryWrapper来实现update修改功能
// 将(年龄大于20并且用户名中包含有a)或者邮箱为null的用户信息修改
QueryWrapper<User> queryWrapper = new QueryWrapper();
queryWrapper.gt("age", 20)
.like("name", "a")
.or()
.isNull("email");
User user = new User();
user.setName("小明");
user.setEmail("test@qq.com");
int result = userMapper.update(user, queryWrapper);
//UPDATE user SET name=?, email=? WHERE is_deleted=0 AND (age > ? AND name LIKE ? OR email IS NULL)
System.out.println("result = " + result);
}
使用and(或or)方法中的lamda表达式调整查询顺序
@Test
public void test05() {
// 将用户名中包含有a 并且 (年龄大于20 或 邮箱为null) 的用户信息修改
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name", "a")
.and(i->i.gt("age", 20).or().isNull("email"));
User user = new User();
user.setName("小红");
user.setEmail("test@qq.com");
int result = userMapper.update(user, queryWrapper);
//UPDATE user SET name=?, email=? WHERE is_deleted=0 AND (name LIKE ? AND (age > ? OR email IS NULL))
System.out.println("result = " + result);
}
选择指定的查询字段
@Test
public void test06() {
// 查询用户名,年龄,邮箱信息
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("name", "age", "email");
List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
maps.forEach(System.out::println);
}
通过子查询方式进行查询
@Test
public void test07() {
// 查询id 小于等于100的用户信息, 通过子查询方式查询
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.inSql("id", "select id from user where id <= 100");
List<User> list = userMapper.selectList(queryWrapper);
//SELECT id,age,name,email,is_deleted FROM user WHERE is_deleted=0 AND (id IN (select id from user where id <= 100))
list.forEach(System.out::println);
}
组装UpdateWrapper实现修改(可以指定字段修改)
@Test
public void test08() {
// 将用户名中包含a并且 (年龄大于20 或 邮箱为null) 的用户信息修改
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
// 设定筛选条件
updateWrapper.like("name", "a")
.and(i->i.gt("age", 20).or().isNull("email"));
// 设定修改内容
updateWrapper.set("name", "小黑").set("email", "abc@qq.com");
// UPDATE user SET name=?,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);
}
通过condition判断是否需要组装
@Test
public void test10() {
String name = "a";
Integer ageBegin = 20;
Integer ageEnd = 30;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
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);
}
LamdaQueryWrapper(第二个变量处使用了lamda表达式,函数式接口来代替字符串,避免手动写表的属性写错)
@Test
public void test11() {
String name = "a";
Integer ageBegin = 20;
Integer ageEnd = 30;
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.like(StringUtils.isNotBlank(name), User::getName, name)
.ge(ageBegin != null, User::getAge, ageBegin)
.le(ageEnd != null, User::getAge, ageEnd);
List<User> list = userMapper.selectList(lambdaQueryWrapper);
list.forEach(System.out::println);
}
LamdaUpdateWrapper
@Test
public void test12() {
// 将用户名中包含a并且 (年龄大于20 或 邮箱为null) 的用户信息修改
LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
// 设定筛选条件
lambdaUpdateWrapper.like(User::getName, "a")
.and(i->i.gt(User::getAge, 20).or().isNull(User::getEmail));
// 设定修改内容
lambdaUpdateWrapper.set(User::getName, "小黑").set(User::getEmail, "abc@qq.com");
int result = userMapper.update(null, lambdaUpdateWrapper);
System.out.println("result = " + result);
}