• Mybatisplus条件构造器


    一、Wrapper介绍

    Wrapper : 条件构造抽象类,最顶端父类

    • AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件
      • QueryWrapper : 查询条件封装
      • UpdateWrapper : Update 条件封装
      • AbstractLambdaWrapper : 使用Lambda 语法
        • LambdaQueryWrapper :用于Lambda语法使用的查询Wrapper
        • LambdaUpdateWrapper : Lambda 更新封装Wrapper

    条件构造器是一个用于构建SQL查询条件的工具,它可以帮助你通过链式调用来拼装查询条件,进一步简化了查询条件的编写。使用条件构造器,你可以灵活地构建各种条件,包括等于、不等于、大于、小于、模糊查询等。

    二、QueryWrapper

    a>例1:组装查询条件

    1. /** 查询用户包含a,年龄在2030之间,邮箱信息不为null的用户信息 **/
    2. @Test
    3. public void test01() {
    4. /*
    5. SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user
    6. WHERE is_deleted=0 AND (user_name LIKE ?
    7. AND age BETWEEN ? AND ? AND email IS NOT NULL)
    8. */
    9. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    10. queryWrapper.like("user_name", "a")
    11. .between("age", 20, 30)
    12. .isNotNull("email");
    13. List<User> users = userMapper.selectList(queryWrapper);
    14. users.forEach(System.out::println);
    15. }

    b>例2:组装排序条件

    1. /** 组装排序条件 查询用户信息,按照年龄的降序排序,若年龄相同,则按照id升序排序 **/
    2. @Test
    3. public void test02() {
    4. /*
    5. SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user
    6. WHERE is_deleted=0 ORDER BY age DESC,uid ASC
    7. */
    8. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    9. queryWrapper.orderByDesc("age")
    10. .orderByAsc("uid");
    11. List<User> users = userMapper.selectList(queryWrapper);
    12. users.forEach(System.out::println);
    13. }

    c>例3:组装删除条件

    1. /** 组装删除条件 删除邮箱地址为null的用户信息 **/
    2. @Test
    3. public void test03() {
    4. /*
    5. UPDATE t_user SET is_deleted=1 WHERE is_deleted=0 AND (email IS NOT NULL)
    6. */
    7. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    8. queryWrapper.isNull("email");
    9. int result = userMapper.delete(queryWrapper);
    10. System.out.println("result = " + result);
    11. }

    d>例4:条件的优先级

    1. /** 组装修改条件 将(年龄大于20并且用户名中包含有a)或邮箱为null的用户信息修改 **/
    2. @Test
    3. public void test04() {
    4. /*
    5. UPDATE t_user SET user_name=?, email=?
    6. WHERE is_deleted=0 AND (age > ? AND user_name LIKE ? OR email IS NOT NULL)
    7. */
    8. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    9. queryWrapper.gt("age", 20)
    10. .like("user_name", "a")
    11. .or()
    12. .isNotNull("email");
    13. User user = new User();
    14. user.setName("小明");
    15. user.setEmail("test@atguigu.com");
    16. int result = userMapper.update(user, queryWrapper);
    17. System.out.println("result = " + result);
    18. }
    19. /** 条件优先级 将用户名中包含a并且(年龄大于20或邮箱为null)的用户信息修改 **/
    20. @Test
    21. public void test05() {
    22. // lambda中条件优先级
    23. /*
    24. UPDATE t_user SET user_name=?, email=?
    25. WHERE is_deleted=0 AND (user_name LIKE ? AND (age > ? OR email IS NULL))
    26. */
    27. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    28. queryWrapper.like("user_name", "a")
    29. .and(i -> i.gt("age", 20).or().isNull("email"));
    30. User user = new User();
    31. user.setName("小红");
    32. user.setEmail("test@atguigu.com");
    33. int result = userMapper.update(user, queryWrapper);
    34. System.out.println("result = " + result);
    35. }

    e>例5:组装select子句(查询部分字段)

    1. /** 组装select字句 查询用户名的用户名、年龄、邮箱信息 **/
    2. @Test
    3. public void test06() {
    4. /*
    5. SELECT user_name,age,email FROM t_user WHERE is_deleted=0
    6. */
    7. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    8. queryWrapper.select("user_name", "age", "email");
    9. List<Map<String, Object>> users = userMapper.selectMaps(queryWrapper);
    10. users.forEach(System.out::println);
    11. }

    f>例6:实现子查询

    1. /** 组装子查询 查询id小于100的用户信息**/
    2. @Test
    3. public void test07() {
    4. /*
    5. SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user
    6. WHERE is_deleted=0 AND (uid IN (select uid from t_user where uid <= 100))
    7. */
    8. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    9. queryWrapper.inSql("uid", "select uid from t_user where uid <= 100");
    10. List<User> list = userMapper.selectList(queryWrapper);
    11. list.forEach(System.out::println);
    12. }

    三、UpdateWrapper

    1. /** 使用UpdateWrapper实现修改功能 将用户名中包含a并且(年龄大于20或邮箱为null)的用户信息修改 **/
    2. @Test
    3. public void test08() {
    4. /*
    5. UPDATE t_user SET user_name=?,email=?
    6. WHERE is_deleted=0 AND (user_name LIKE ? AND (age > ? OR email IS NULL))
    7. */
    8. UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
    9. updateWrapper.like("user_name", "a")
    10. .and(i -> i.gt("age", 20).or().isNull("email"));
    11. updateWrapper.set("user_name", "小黑").set("email", "abc@atguigu.com");
    12. int result = userMapper.update(null, updateWrapper);
    13. System.out.println("result = " + result);
    14. }

    四、使用condition组装条件

    在真正开发的过程中,组装条件是常见的功能,而这些条件数据来源于用户输入,是可选的,因此我们在组装这些条件时,必须先判断用户是否选择了这些条件,若选择则需要组装该条件,若没有选择则一定不能组装,以免影响SQL执行的结果

    思路一:

    使用if判断语句

    1. /** 模拟开发中组装条件的情况 **/
    2. @Test
    3. public void test09() {
    4. /*
    5. SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user
    6. WHERE is_deleted=0 AND (user_name LIKE ? AND age <= ?)
    7. */
    8. String username = "a";
    9. Integer ageBegin = null;
    10. Integer ageEnd = 30;
    11. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    12. if (StringUtils.isNotBlank(username)) {
    13. // isNotBlank判断某个字符串是否不为空字符串、不为null、不为空白符
    14. queryWrapper.like("user_name", username);
    15. }
    16. if (ageBegin != null) {
    17. queryWrapper.gt("age", ageBegin);
    18. }
    19. if (ageEnd != null) {
    20. queryWrapper.le("age", ageEnd);
    21. }
    22. List<User> list = userMapper.selectList(queryWrapper);
    23. list.forEach(System.out::println);
    24. }

    思路二:

    使用带condition参数的重载方法构建查询条件简化代码的编写

    以like举例,condition用于做判断

    1. /** 使用condition组装条件 **/
    2. @Test
    3. public void test10() {
    4. /*
    5. SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user
    6. WHERE is_deleted=0 AND (user_name LIKE ? AND age <= ?)
    7. */
    8. String username = "a";
    9. Integer ageBegin = null;
    10. Integer ageEnd = 30;
    11. QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    12. queryWrapper.like(StringUtils.isNotBlank(username), "user_name", username)
    13. .gt(ageBegin != null, "age", ageBegin)
    14. .le(ageEnd != null, "age", ageEnd);
    15. List<User> list = userMapper.selectList(queryWrapper);
    16. list.forEach(System.out::println);
    17. }

    五、LambdaQueryWrapper

    功能等同于QueryWrapper,提供了Lambda表达式的语法可以避免填错列名。

    1. /** LambdaQueryWrapper **/
    2. @Test
    3. public void test11() {
    4. /*
    5. SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user
    6. WHERE is_deleted=0 AND (user_name LIKE ? AND age <= ?)
    7. */
    8. String username = "a";
    9. Integer ageBegin = null;
    10. Integer ageEnd = 30;
    11. //组装set子句
    12. LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
    13. //避免使用字符串表示字段,防止运行时错误
    14. queryWrapper.like(StringUtils.isNotBlank(username), User::getName, username)
    15. .gt(ageBegin != null, User::getAge, ageBegin)
    16. .le(ageEnd != null, User::getAge, ageEnd);
    17. List<User> list = userMapper.selectList(queryWrapper);
    18. list.forEach(System.out::println);
    19. }

    六、LambdaUpdateWrapper

    1. /** LambdaUpdateWrapper **/
    2. @Test
    3. public void test12() {
    4. /*
    5. UPDATE t_user SET user_name=?,email=?
    6. WHERE is_deleted=0 AND (user_name LIKE ? AND (age > ? OR email IS NULL))
    7. */
    8. LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
    9. updateWrapper.like(User::getName, "a")
    10. //lambda表达式内的逻辑优先运算
    11. .and(i -> i.gt(User::getAge, 20).or().isNull(User::getEmail));
    12. updateWrapper.set(User::getName, "小黑").set(User::getEmail, "abc@atguigu.com");
    13. int result = userMapper.update(null, updateWrapper);
    14. System.out.println("result = " + result);
    15. }

  • 相关阅读:
    Shell脚本案例:Git规范commit-msg
    webrtc用clang编译支持h264,支持msvc调用库
    中国地名信息库
    AI工具合集
    Mybatis 动态语言 - mybatis-thymeleaf
    Servlet的部署与安全
    一次 MDIO 配置 switch 的调试过程,88e1512 switch mv88e6xxx
    Linux学习(1) ——Linux基本介绍
    薪资25k,从华为外包测试“跳”入字节,说说我转行做测试的这5年...
    【云备份】
  • 原文地址:https://blog.csdn.net/weixin_55772633/article/details/133176667