第一篇:1、Mybatis-Plus 创建SpringBoot项目
第二篇:2、Mybatis-Plus 测试增、删、改、查
第三篇:3、Mybatis-Plus 自定义sql语句
第四篇:4、Mybatis-Plus 通用service的操作
第五篇:5、Mybatis-Plus 常用注解
文章目录
- 1、wapper的介绍
- 2、QueryWrapper(组装查询条件)
- 3、QueryWrapper(组装排序条件)
- 4、QueryWrapper(组装删除条件)
- 5、QueryWrapper(信息的修改)
- 6、QueryWrapper(条件的优先级)
- 7、QueryWrapper(组装select子句)
- 8、QueryWrapper(实现子查询)
- 8、QueryWrapper(UpdateWrapper)
1、wapper的介绍
介绍:
- Wrapper : 条件构造抽象类,最顶端父类
- AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
- QueryWrapper : 查询条件封装
- UpdateWrapper : Update 条件封装
- AbstractLambdaWrapper : 使用Lambda 语法
- LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper
- LambdaUpdateWrapper : Lambda 更新封装Wrapper
2、QueryWrapper(组装查询条件)
//条件构造器
@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","三")
.between("age",20,25)
.isNotNull("email");
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
3、QueryWrapper(组装排序条件)
@Test
public void test02(){
/**
* //按年龄降序查询用户,如果年龄相同则按id升序排列
* //SELECT id,username AS 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> users = userMapper.selectList(queryWrapper);
users.forEach(System.out::println);
}
4、QueryWrapper(组装删除条件)
@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 rs = userMapper.delete(queryWrapper);
System.out.println(rs);
}
5、QueryWrapper(信息的修改)
@Test
public void test04(){
/**使用queryWrapper实现用户信息的修改
* 将(年龄大于20并且用户名中包含有a)或邮箱为null的用户信息修改
* UPDATE t_user SET user_name=?, age=? WHERE is_deleted=0 AND (age > ? AND user_name LIKE ? OR email IS NULL)
*/
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.gt("age",20)
.like("user_name","a")
.or()
.isNull("email");
User user = new User();
user.setName("孙悟空");
user.setAge(32);
int rs = userMapper.update(user,queryWrapper);
System.out.println(rs);
}
6、QueryWrapper(条件的优先级)
@Test
public void test05(){
/**
* 条件的优先级
* 将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改
* lambda中的条件优先执行
* UPDATE t_user SET user_name=?, age=?, email=? WHERE is_deleted=0 AND (user_name LIKE ? AND (age > ? OR email IS NULL))
*/
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("user_name","a")
.and(i->i.gt("age",20).or().isNull("email"));
User user = new User();
user.setName("猪八戒");
user.setAge(32);
user.setEmail("zbj@qq.com");
int rs = userMapper.update(user,queryWrapper);
System.out.println(rs);
}
7、QueryWrapper(组装select子句)
@Test
public void test06(){
/**
* 查询用户信息的username、age和email字段
* SELECT user_name,age,email FROM t_user WHERE is_deleted=0
*/
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("user_name","age","email");
List<Map<String,Object>> maps = userMapper.selectMaps(queryWrapper);
maps.forEach(System.out::println);
}
8、QueryWrapper(实现子查询)
@Test
public void test07(){
/**
*查询uid小于等于100的用户信息
* 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 <= 100))
*/
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.inSql("uid","select uid from t_user where uid <= 100");
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
8、QueryWrapper(UpdateWrapper)
@Test
public void test08(){
/**
* 将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改
* UPDATE t_user SET user_name=?,email=? WHERE is_deleted=0 AND (user_name LIKE ? AND (age > ? OR email IS NULL))
*/
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.like("user_name","a")
.and(i-> i.gt("age",20).or().isNull("email"));
updateWrapper.set("user_name","唐僧").set("email","tangseng@qq.com");
int rs = userMapper.update(null,updateWrapper);
System.out.println(rs);
}