MyBatisPlus条件构造器

主要包括以下几种:

  • QueryWrapper
  • UpdateWrapper
  • LambdaQueryWrapper
  • LambdaUpdateWrapper

用法示例如下:

QueryWrapper

1 查询示例

    /**
     * ==>  Preparing: SELECT id,user_name AS name,age,email,is_deleted FROM user WHERE is_deleted=0 AND (user_name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
     * ==> Parameters: %雷%(String), 10(Integer), 30(Integer)
     * <==    Columns: id, name, age, email, is_deleted
     * <==        Row: 1734579107187970050, 李雷, 12, [email protected], 0
     * <==      Total: 1
     * 用户列表信息为:[User(id=1734579107187970050, name=李雷, age=12, [email protected], isDeleted=0)]
     */
    @Test
    void testQueryWrapper01(){
        // 查询用户名包含“雷”字,且年龄在20到30之间,邮箱不为null的用户信息
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("user_name", "雷")
                .between("age", 10, 30)
                .isNotNull("email");

        List<User> userList = userMapper.selectList(queryWrapper);
        log.info("用户列表信息为:{}", userList);
    }
    /**
     * SELECT id,user_name AS name,age,email,is_deleted FROM user WHERE is_deleted=0 ORDER BY age DESC,id ASC
     */
    @Test
    void testOrderBy02(){
        // 查询用户列表,按照年龄降序,按照ID升序排序
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("age")
                .orderByAsc("id");
        List<User> userList = userMapper.selectList(queryWrapper);
        log.info("用户列表信息为:{}", userList);
    }
  • 组装select子句
    /**
     * 组装select子句
     */
    @Test
    void testCompSubSql(){
        // 只查询部分字段
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("user_name", "age", "email");
        List<Map<String, Object>> maps = userMapper.selectMaps(queryWrapper);
        log.info("查询部分字段:{}", maps);
    }
  • 子查询

    /**
     * 子查询
     * ==>  Preparing: SELECT id,user_name AS name,age,email,is_deleted FROM user WHERE is_deleted=0 AND (id IN (select id from t_user where age > 10))
     * ==> Parameters:
     * <==    Columns: id, name, age, email, is_deleted
     * <==        Row: 1, Jone, 18, [email protected], 0
     * <==        Row: 2, 窃听风云, 23, [email protected], 0
     * <==        Row: 3, Tom, 28, [email protected], 0
     * <==      Total: 3
     * 查询结果为:[User(id=1, name=Jone, age=18, [email protected], isDeleted=0), User(id=2, name=窃听风云, age=23, [email protected], isDeleted=0), User(id=3, name=Tom, age=28, [email protected], isDeleted=0)]
     */
    @Test
    void testCompSubSql2(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("id", "select id from t_user where age > 10");
        List<User> userList = userMapper.selectList(queryWrapper);
        log.info("查询结果为:{}", userList);
    }

2 删除示例

    /**
     * ==>  Preparing: UPDATE user SET is_deleted=1 WHERE is_deleted=0 AND (user_name = ? AND age = ?)
     * ==> Parameters: 李雷(String), 12(Integer)
     * <==    Updates: 1
     */
    @Test
    void testDelete03(){
        // 删除功能测试,此时为逻辑删除,实际上执行的是更新语句
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", "李雷")
                .eq("age", 12);
        int delete = userMapper.delete(queryWrapper);
        log.info("用户列表信息为:{}", delete);
    }

3 更新示例

    /**
     * ==>  Preparing: UPDATE user SET user_name=?, age=?, email=? WHERE is_deleted=0 AND (user_name = ? AND age = ? OR email IS NULL)
     * ==> Parameters: 韩梅梅(String), 23(Integer), [email protected](String), 李雷(String), 12(Integer)
     * <==    Updates: 1
     */
    @Test
    void testUpdate01(){
        // 更新姓名为”李雷“且年龄为12岁,或邮箱为空的数据
        // case 1: 使用QueryWrapper
        // 设定更新条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", "李雷")
                .eq("age", 12)
                .or()
                .isNull("email");
        // 设定更新数据
        User updUser = new User();
        updUser.setEmail("[email protected]");
        updUser.setName("韩梅梅");
        updUser.setAge(23);
        int update = userMapper.update(updUser, queryWrapper);
        log.info("更新条数为:{}", update);
    }
  • MybatisPlus条件优先级:lambda表达式内的逻辑优先运算
    /**
     * ==>  Preparing: UPDATE user SET user_name=?, age=?, email=? WHERE is_deleted=0 AND (user_name LIKE ? AND (age > ? OR email IS NULL))
     * ==> Parameters: 窃听风云(String), 23(Integer), [email protected](String), %J%(String), 18(Integer)
     * <==    Updates: 1
     */
    @Test
    void testUpdate02(){
        // 将用户名中包含有J并且(年龄大于18或邮箱为null)的用户信息修改
        // case 2: lambda表达式内的逻辑优先运算
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("user_name", "J")
                .and(
                        wrapper -> wrapper.gt("age", 18)
                                .or()
                                .isNull("email")
                );
        // 设定更新数据
        User updUser = new User();
        updUser.setEmail("[email protected]");
        updUser.setName("窃听风云");
        updUser.setAge(23);
        int update = userMapper.update(updUser, queryWrapper);
        log.info("更新条数为:{}", update);
    }
}

UpdateWrapper

    /**
     * UPDATE user SET user_name=?,email=? WHERE is_deleted=0 AND (user_name = ? AND (age > ? AND email IS NULL))
     */
    @Test
    void testUpdateWrapper(){
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_name", "kitty")
                .and(wrapper -> wrapper.gt("age", 10).isNull("email"));
        updateWrapper.set("user_name", "gis2023").set("email", "[email protected]");
        int update = userMapper.update(updateWrapper);
        log.info("更新条数为:{}", update);
//        int update1 = userMapper.update(null, updateWrapper);
//        log.info("更新条数为:{}", update1);
    }

4 条件组装

示例一
    /**
     * ==>  Preparing: SELECT id,user_name AS name,age,email,is_deleted FROM user WHERE is_deleted=0 AND (user_name LIKE ? AND age >= ? AND age <= ?)
     * ==> Parameters: %张三%(String), 10(Integer), 20(Integer)
     * <==      Total: 0
     */
    @Test
    void testDynamicSqlWrapper(){
        String username = "张三";
        Integer ageBgn = 10;
        Integer ageEnd = 20;
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(username)){
            queryWrapper.like("user_name", username);
        }

        if (ageBgn != null){
            queryWrapper.ge("age", ageBgn);
        }

        if (ageEnd != null){
            queryWrapper.le("age", ageEnd);
        }

        List<User> userList = userMapper.selectList(queryWrapper);
        log.info("查询记录为:{}", userList);
    }
示例二 使用condition
    /**
     * 使用condition组装条件
     * ==>  Preparing: SELECT id,user_name AS name,age,email,is_deleted FROM user WHERE is_deleted=0 AND (user_name LIKE ? AND age >= ? AND age <= ?)
     * ==> Parameters: %张三%(String), 10(Integer), 20(Integer)
     * <==      Total: 0
     */
    @Test
    void testDynamicSqlWrapper02(){
        String username = "张三";
        Integer ageBgn = 10;
        Integer ageEnd = 20;
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(username), "user_name", username)
                .ge(ageBgn != null, "age", ageBgn)
                .le(ageEnd != null, "age", ageEnd);

        List<User> userList = userMapper.selectList(queryWrapper);
        log.info("查询记录为:{}", userList);
    }

LambdaQueryWrapper


    /**
     *
     * ==>  Preparing: SELECT id,user_name AS name,age,email,is_deleted FROM user WHERE is_deleted=0 AND (user_name LIKE ? AND age >= ? AND age <= ?)
     * ==> Parameters: %张三%(String), 10(Integer), 20(Integer)
     * <==      Total: 0
     */
    @Test
    void testDynamicSqlWrapper03(){
        String username = "张三";
        Integer ageBgn = 10;
        Integer ageEnd = 20;
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(username), User::getName, username)
                .ge(ageBgn != null, User::getAge, ageBgn)
                .le(ageEnd != null, User::getAge, ageEnd);

        List<User> userList = userMapper.selectList(lambdaQueryWrapper);
        log.info("查询记录为:{}", userList);
    }

LambdaUpdateWrapper


    /**
     * ==>  Preparing: UPDATE user SET user_name=?,email=? WHERE is_deleted=0 AND (user_name = ? AND (age > ? AND email IS NULL))
     * ==> Parameters: gis2023(String), [email protected](String), kitty(String), 10(Integer)
     * <==    Updates: 0
     */
    @Test
    void testUpdateWrapper02(){
        LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(User::getName, "kitty")
                .and(wrapper -> wrapper.gt(User::getAge, 10).isNull(User::getEmail));
        lambdaUpdateWrapper.set(User::getName, "gis2023").set(User::getEmail, "[email protected]");
        int update = userMapper.update(lambdaUpdateWrapper);
        log.info("更新条数为:{}", update);
//        int update1 = userMapper.update(null, lambdaUpdateWrapper);
//        log.info("更新条数为:{}", update1);
    }

常用

最常用的是LambdaQueryWrapper 和 LambdaUpdateWrapper

你可能感兴趣的:(#,MyBatisPlus,mybatisplus)