MyBatis-Plus 条件构造器
作者:互联网
MyBatis-Plus 条件构造器
MyBatis-Plus 条件构造器用于构造sql条件
- Wrapper : 条件构造抽象类,最顶端父类
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
- QueryWrapper : 查询条件封装
- UpdateWrapper : Update 条件封装
- AbstractLambdaWrapper : 使用Lambda 语法
- LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper
- LambdaUpdateWrapper : Lambda 更新封装Wrapper
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
代码实战
查询
/**
* 查询name包含j,age在16到27之间,email不为空的数据
*/
@Test
public void testQueryWrapper(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name","j")
.between("age",16,27)
.isNotNull("email");
// SELECT id,name,age,email FROM user WHERE (name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
List<User> userList = mapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
/**
* 将查询结果按照年龄降序id升序排列
*/
@Test
public void testOrder(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.orderByDesc("age").orderByAsc("id");
// SELECT id,name,age,email FROM user ORDER BY age DESC,id ASC
List<User> userList = mapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
/**
* 查询指定字段
*/
@Test
public void testSelect() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("name", "age");
// SELECT name,age FROM user
List<Map<String, Object>> userMap = mapper.selectMaps(queryWrapper);
userMap.forEach(System.out::println);
}
/**
* 子查询
*/
@Test
public void testSelect(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.inSql("id","select id from user where id<4");
// SELECT id,name,age,email FROM user WHERE (id IN (select id from user where id<4))
List<User> userList = mapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
/**
* 动态查询
*/
@Test
public void testContidition(){
String name="";
Integer age=17;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// 动态组装条件
queryWrapper.like(!StringUtils.isEmpty(name),"name",name);
queryWrapper.ge(!ObjectUtils.isEmpty(age),"age",age);
// SELECT id,name,age,email FROM user WHERE (age >= ?)
List<User> userList = mapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
/**
* 动态查询
*/
@Test
public void testCondition(){
String name="";
Integer age=17;
// LambdaQueryWrapper可以自动获取属性对应字段名
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
// 动态组装条件
queryWrapper.like(!StringUtils.isEmpty(name),User::getName,name);
queryWrapper.ge(!ObjectUtils.isEmpty(age),User::getAge,age);
// SELECT id,name,age,email FROM user WHERE (age >= ?)
List<User> userList = mapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
删除
/**
* 删除email为空的数据
*/
@Test
public void testDelete(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.isNull("email");
// DELETE FROM user WHERE (email IS NULL)
mapper.delete(queryWrapper);
}
修改
/**
* 将age>20,name包含a或者email为空的用户名修改为zandra,邮箱修改为123@qq.com
*/
@Test
public void testUpdate(){
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.gt("age",20)
.like("name","a")
.or()
.isNull("email");
User user = new User();
user.setName("zandra");
user.setEmail("123@qq.com");
// UPDATE user SET name=?, email=? WHERE (age > ? AND name LIKE ? OR email IS NULL)
mapper.update(user,updateWrapper);
}
/**
* 将name包含a并且(age>20或者email为空)的用户名修改为zandra,邮箱修改为123@qq.com
*/
@Test
public void testUpdate(){
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.like("name","a")
// 通过lambda表达式将条件优先级设置为一样(条件包含在同一对小括号中)
.and(wrapper->{
wrapper.gt("age",20)
.or()
.isNull("email");
});
// 将修改内容封装在实体对象中
User user = new User();
user.setName("zandra");
user.setEmail("123@qq.com");
// UPDATE user SET name=?, email=? WHERE (name LIKE ? AND (age > ? OR email IS NULL))
mapper.update(user,updateWrapper);
}
/**
* 将name包含a并且(age>20或者email为空)的用户名修改为zandra,邮箱修改为123@qq.com
*/
@Test
public void testUpdate() {
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.like("name", "a")
.and(wrapper -> {
wrapper.gt("age", 20)
.or()
.isNull("email");
});
// 设置修改内容
updateWrapper.set("name","zandra").set("email","123@qq.com");
// UPDATE user SET name=?,email=? WHERE (name LIKE ? AND (age > ? OR email IS NULL))
mapper.update(null, updateWrapper);
}
/**
* LambdaUpdateWrapper可以自动获取属性对应字段名
*/
@Test
public void testUpdate() {
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.like(User::getName, "a")
.and(wrapper -> {
wrapper.gt(User::getAge, 20)
.or()
.isNull(User::getEmail);
});
// 设置修改内容
updateWrapper.set(User::getName,"zandra").set(User::getEmail,"123@qq.com");
// UPDATE user SET name=?,email=? WHERE (name LIKE ? AND (age > ? OR email IS NULL))
mapper.update(null, updateWrapper);
}
标签:queryWrapper,name,age,构造,Plus,user,MyBatis,email,updateWrapper 来源: https://www.cnblogs.com/Y-wee/p/16274893.html