2.7 组装子查询字段 根据需求查相关的字段使用 wapper (select)
@Test public void slelectWrappersSql(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //查询uid并对他赋予条件 queryWrapper.inSql("uid","SELECT uid FROM t_user WHERE uid<=100"); //查询用户年龄并降序排列,年龄相同的按照id升序排序 List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
==> Preparing: SELECT uid AS id,user_name AS name,age,email,is_delect FROM t_user WHERE is_delect=0 AND (uid IN (SELECT uid FROM t_user WHERE uid<=100)) ==> Parameters: <== Columns: id, name, age, email, is_delect <== Row: 4, 我是老王, 18, test4@baomidou.com, 0 <== Row: 5, Billie, 10, test5@baomidou.com, 0 <== Row: 6, 老王, 10, 757631644001, 0 <== Row: 7, 老王超1, 10, 757631644111, 0 <== Row: 8, 闫文超2, 10, 757631644221, 0 <== Row: 9, 闫文超3, 10, 757631644331, 0 <== Row: 10, 闫文超4, 14, 757631644441, 0 <== Row: 12, 闫文超6, 16, 757631644661, 0 <== Row: 14, 老闫, 666, 757631644@qq.com, 0 <== Row: 15, 老闫, 666, 757631644@qq.com, 0 <== Total: 10 User(id=4, name=我是老王, age=18, email=test4@baomidou.com, isDelect=0) User(id=5, name=Billie, age=10, email=test5@baomidou.com, isDelect=0) User(id=6, name=老王, age=10, email=757631644001, isDelect=0) User(id=7, name=老王超1, age=10, email=757631644111, isDelect=0) User(id=8, name=闫文超2, age=10, email=757631644221, isDelect=0) User(id=9, name=闫文超3, age=10, email=757631644331, isDelect=0) User(id=10, name=闫文超4, age=14, email=757631644441, isDelect=0) User(id=12, name=闫文超6, age=16, email=757631644661, isDelect=0) User(id=14, name=老闫, age=666, email=757631644@qq.com, isDelect=0) User(id=15, name=老闫, age=666, email=757631644@qq.com, isDelect=0)
2.8 修改中使用 UpdateWrapper(Update)
在这里插入图片描述
@Test public void UpdatetWrapper011(){ //将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改 // 并且用and 包住mybatis中 lambda 优先运行 //将 queryWrapper .like(a) and (年龄大于20或邮箱为null ==> // (i->i.ge("age",20) .or().isNull("email"))的 用户的用户的信息进行修改 UpdateWrapper<User> queryWrapper = new UpdateWrapper<>(); queryWrapper .like("user_name","a") .and(i->i.gt("age",20) .or() .isNull("email")); queryWrapper.set("user_name","我很心烦").set("age",888); //两个参数第一个 修改的用户体对象 第二天是判断的条件 int result = userMapper.update(null,queryWrapper); //三目运算 System.out.println(result>0 ? "修改成功" :"修改失败"); System.out.println("修改条数"+result); }
运行结果为:
==> Preparing: UPDATE t_user SET user_name=?,age=? WHERE is_delect=0 AND (user_name LIKE ? AND (age > ? OR email IS NULL)) ==> Parameters: 我很心烦(String), 888(Integer), %a%(String), 20(Integer) <== Updates: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@60859f5a] 修改成功 修改条数1
2.9 组装条件中使用 QueryWrapper(seelect)
@Test public void selectList(){ String username = "a"; Integer ageBegin = null; Integer ageEnd = 30; QueryWrapper<User> queryWrapper = new QueryWrapper<>(); if(StringUtils.isNotBlank(username)){ //isNotBlank判断某个字符创是否不为空字符串、不为null、不为空白符 queryWrapper.like("user_name", username); } if(ageBegin != null){ //ge 大于等于 queryWrapper.ge("age", ageBegin); } if(ageEnd != null){ //le 小于于等于 queryWrapper.le("age", ageEnd); } List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
运行结果
==> Preparing: SELECT uid AS id,user_name AS name,age,email,is_delect FROM t_user WHERE is_delect=0 AND (user_name LIKE ? AND age <= ?) ==> Parameters: %a%(String), 30(Integer) <== Columns: id, name, age, email, is_delect <== Row: 14, aa, 22, 757631644@qq.com, 0 <== Total: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3ce53f6a] User(id=14, name=aa, age=22, email=757631644@qq.com, isDelect=0) 2022-10-08 23:16:50.583 INFO 16404 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated... 2022-10-08 23:16:50.587 INFO 16404 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed. Process finished with exit code 0
2.9.1 使用condition组装条件 (select)
上面的(2.9)实现方案没有问题,但是代码比较复杂,我们可以使用带condition参数的重载方法构建查询条件,简化代码的编写
@Test public void selectList01(){ String username = "a"; Integer ageBegin = null; Integer ageEnd = 30; QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //插入3个参数第一个:判断条件 2:k的值 3: v的值 queryWrapper.like(StringUtils.isNotBlank(username), "user_name", username) .ge(ageBegin != null, "age", ageBegin) .le(ageEnd != null, "age", ageEnd); List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
运行结果为:
==> Parameters: %a%(String), 30(Integer) <== Columns: id, name, age, email, is_delect <== Row: 14, aa, 22, 757631644@qq.com, 0 <== Total: 1
3.0 使用LambdaQueryWrapper查询条件 (select)
@Test public void LambdaQueryWrapper(){ String username = "a"; Integer ageBegin = null; Integer ageEnd = 30; LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>(); //插入3个参数第一个:判断条件 2:k的值 (为了防止写错他获取的User中getName的实体类的名字) 3: v的值 queryWrapper.like(StringUtils.isNotBlank(username), User::getName, username) .ge(ageBegin != null, User::getAge, ageBegin) .le(ageEnd != null, User::getAge, ageEnd); List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); }
运行结果为:
==> Preparing: SELECT uid AS id,user_name AS name,age,email,is_delect FROM t_user WHERE is_delect=0 AND (user_name LIKE ? AND age <= ?) ==> Parameters: %a%(String), 30(Integer) <== Columns: id, name, age, email, is_delect <== Row: 14, aa, 22, 757631644@qq.com, 0 <== Total: 1
3.1 使用LambdaUpdateWrapper查询条件 (update)
@Test public void LambdaUpdateWrapper(){ //将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改 // 并且用and 包住mybatis中 lambda 优先运行 //将 queryWrapper .like(a) and (年龄大于20或邮箱为null ==> // (i->i.ge("age",20) .or().isNull("email"))的 用户的用户的信息进行修改 LambdaUpdateWrapper<User> queryWrapper = new LambdaUpdateWrapper<>(); queryWrapper .like(User::getName,"a") .and(i->i.gt(User::getAge,20) .or() .isNull(User::getEmail)); queryWrapper.set(User::getName,"我很心烦").set(User::getAge,888); //两个参数第一个 修改的用户体对象 第二天是判断的条件 int result = userMapper.update(null,queryWrapper); //三目运算 System.out.println(result>0 ? "修改成功" :"修改失败"); System.out.println("修改条数"+result); }
运行结果为:
JDBC Connection [HikariProxyConnection@976042249 wrapping com.mysql.cj.jdbc.ConnectionImpl@4fb392c4] will not be managed by Spring ==> Preparing: UPDATE t_user SET user_name=?,age=? WHERE is_delect=0 AND (user_name LIKE ? AND (age > ? OR email IS NULL)) ==> Parameters: 我很心烦(String), 888(Integer), %a%(String), 20(Integer) <== Updates: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@242a209e] 修改成功 修改条数1
4.0 此模块代码集合
package com.example; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.example.mapper.UserMapper; import com.example.pojo.User; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.xml.ws.Action; import java.util.List; import java.util.Map; @SpringBootTest public class MyBatisPlusWraspperTest { @Autowired private UserMapper userMapper; @Test public void slelectWrapper(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //此处指明column 指的是数据库的字段并非实体类对象请大家注意 // like:模糊查询 between 范围 大于10小于30 isnull 字段不为空的数据 queryWrapper.like("user_name","老王") .between("age",10,30) .isNotNull("email"); //查询用户为 老王的 年龄范围在10-30之间的并且邮箱不为空的数据 List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); } @Test public void slelectWrapperAsc(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //orderByDesc 降序 orderByAsc 升序 queryWrapper.orderByDesc("age") .orderByAsc("id"); //查询用户年龄并降序排列,年龄相同的按照id升序排序 List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); } @Test public void delectWrapperAsc(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //orderByDesc 降序 orderByAsc 升序 queryWrapper.eq("email","757631644771"); //删除邮箱为空的数据(因为我们加入了逻辑删除的注解实体类属性我们实现的删除也只是 修改他的字段为假删除 不需要删除即可) int result = userMapper.delete(queryWrapper); System.out.println(result); } @Test public void UpdatetWrapperAsc(){ //将用户名中包含有a并且年龄大于20或邮箱为null的用户信息修改 QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //并且 是and 默认就是and 但是 或的话就需要 手动添加or 条件了 //ge 是大于 queryWrapper.ge("age",20) .like("user_name","a") .or() .isNull("email"); User user = new User(); user.setEmail("757631644@qq.com"); user.setName("老闫"); user.setAge(666); //两个参数第一个 修改的用户体对象 第二天是判断的条件 int result = userMapper.update(user,queryWrapper); //三目运算 System.out.println(result>0 ? "修改成功" :"修改失败"); System.out.println("修改条数"+result); } @Test public void UpdatetWrapper(){ //将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改 // 并且用and 包住mybatis中 lambda 优先运行 //将 queryWrapper .like(a) and (年龄大于20或邮箱为null ==> // (i->i.ge("age",20) .or().isNull("email"))的 用户的用户的信息进行修改 QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper .like("user_name","a") .and(i->i.gt("age",20) .or() .isNull("email")); User user = new User(); user.setEmail("757631644@qq.com"); user.setName("老闫"); user.setAge(666); //两个参数第一个 修改的用户体对象 第二天是判断的条件 int result = userMapper.update(user,queryWrapper); //三目运算 System.out.println(result>0 ? "修改成功" :"修改失败"); System.out.println("修改条数"+result); } @Test public void slelectWrappers(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //根据需求查询部分字段 queryWrapper.select("age","user_name"); //查询用户年龄并降序排列,年龄相同的按照id升序排序 List<Map<String, Object>> list = userMapper.selectMaps(queryWrapper); list.forEach(System.out::println); } @Test public void slelectWrappers01(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //根据需求查询部分字段 queryWrapper.select("age","user_name"); //查询用户年龄并降序排列,年龄相同的按照id升序排序 List<Map<String, Object>> list = userMapper.selectMaps(queryWrapper); list.forEach(System.out::println); } @Test public void slelectWrappersSql(){ QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //查询uid并对他赋予条件 queryWrapper.inSql("uid","SELECT uid FROM t_user WHERE uid<=100"); //查询用户年龄并降序排列,年龄相同的按照id升序排序 List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); } @Test public void UpdatetWrapper011(){ //将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改 // 并且用and 包住mybatis中 lambda 优先运行 //将 queryWrapper .like(a) and (年龄大于20或邮箱为null ==> // (i->i.ge("age",20) .or().isNull("email"))的 用户的用户的信息进行修改 UpdateWrapper<User> queryWrapper = new UpdateWrapper<>(); queryWrapper .like("user_name","a") .and(i->i.gt("age",20) .or() .isNull("email")); queryWrapper.set("user_name","我很心烦").set("age",888); //两个参数第一个 修改的用户体对象 第二天是判断的条件 int result = userMapper.update(null,queryWrapper); //三目运算 System.out.println(result>0 ? "修改成功" :"修改失败"); System.out.println("修改条数"+result); } @Test public void selectList(){ String username = "a"; Integer ageBegin = null; Integer ageEnd = 30; QueryWrapper<User> queryWrapper = new QueryWrapper<>(); if(StringUtils.isNotBlank(username)){ //isNotBlank判断某个字符创是否不为空字符串、不为null、不为空白符 queryWrapper.like("user_name", username); } if(ageBegin != null){ //ge 大于等于 queryWrapper.ge("age", ageBegin); } if(ageEnd != null){ //le 小于于等于 queryWrapper.le("age", ageEnd); } List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); } @Test public void selectList01(){ String username = "a"; Integer ageBegin = null; Integer ageEnd = 30; QueryWrapper<User> queryWrapper = new QueryWrapper<>(); //插入3个参数第一个:判断条件 2:k的值 3: v的值 queryWrapper.like(StringUtils.isNotBlank(username), "user_name", username) .ge(ageBegin != null, "age", ageBegin) .le(ageEnd != null, "age", ageEnd); List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); } @Test public void LambdaQueryWrapper(){ String username = "a"; Integer ageBegin = null; Integer ageEnd = 30; LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>(); //插入3个参数第一个:判断条件 2:k的值 (为了防止写错他获取的User中getName的实体类的名字) 3: v的值 queryWrapper.like(StringUtils.isNotBlank(username), User::getName, username) .ge(ageBegin != null, User::getAge, ageBegin) .le(ageEnd != null, User::getAge, ageEnd); List<User> list = userMapper.selectList(queryWrapper); list.forEach(System.out::println); } @Test public void LambdaUpdateWrapper(){ //将用户名中包含有a并且(年龄大于20或邮箱为null)的用户信息修改 // 并且用and 包住mybatis中 lambda 优先运行 //将 queryWrapper .like(a) and (年龄大于20或邮箱为null ==> // (i->i.ge("age",20) .or().isNull("email"))的 用户的用户的信息进行修改 LambdaUpdateWrapper<User> queryWrapper = new LambdaUpdateWrapper<>(); queryWrapper .like(User::getName,"a") .and(i->i.gt(User::getAge,20) .or() .isNull(User::getEmail)); queryWrapper.set(User::getName,"我很心烦").set(User::getAge,888); //两个参数第一个 修改的用户体对象 第二天是判断的条件 int result = userMapper.update(null,queryWrapper); //三目运算 System.out.println(result>0 ? "修改成功" :"修改失败"); System.out.println("修改条数"+result); } }
到此为止wapper学习结束了;