Mybatis Plus之QueryWrapper、LambdaQueryWrapper以及LambdaQueryChainWrapper用法

一、QueryWrapper

.like("email", "24252") //email对应数据库中列名
/**
     * 条件构造器 查询操作
     */
    @Test
    void TestQueryWrapperSelect() {
        //1、条件用法
        List userList = userMapper.selectList(new QueryWrapper()
                .like("email", "24252")
                .between("age", 20, 22)
                .or()
                .eq("name", "zcx")
        );
        System.out.println("userList:" + userList);

        //2、排序用法
        List users = userMapper.selectList(new QueryWrapper()
                .eq("nick_name", "xx")
                .orderByAsc("age")  //升序
//                .orderByDesc("age") //降序
                .last("limit 0,3") //last用法:在sql末尾添加sql语句,有sql注入风险
        );
        System.out.println("users:"+users);

    }

二、LambdaQueryWrapper

queryWrapper.eq(User::getName,"liangd1"); //可以通过Lambda获取数据库列名
@Test
    void TestLambdaQueryWrapper() {
        //1、查询单条
        LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName,"liangd1");
        User selectOne = userMapper.selectOne(queryWrapper);
        System.out.println(selectOne);

        //2、查询list以及统计条数
        queryWrapper.eq(User::getName, "zcx");
        List userList = userMapper.selectList(queryWrapper);
        System.out.println("userList:" + userList);
        Integer result = userMapper.selectCount(queryWrapper);
        System.out.println("result:" + result);
    }

三、LambdaQueryChainWrapper

//链式查询方式
User one = new LambdaQueryChainWrapper<>(userMapper)
                  .eq(User::getName, "liangd1")
                  .one();
@Test
    void TestLambdaQueryChainWrapper() {
        //1、eq查询单条
        User one = new LambdaQueryChainWrapper<>(userMapper)
                .eq(User::getName, "liangd1")
                .one();
        System.out.println("UserOne:" + one);

        //2、查询list
        List users = new LambdaQueryChainWrapper<>(userMapper)
                .eq(User::getName, "zcx")
                .list();
        System.out.println("UserList:" + users);

        //3、模糊查询
        List LikeList = new LambdaQueryChainWrapper<>(userMapper)
                .like(User::getEmail, "test")
                .list();
        System.out.println("LikeUser:" + LikeList);
    }

你可能感兴趣的:(Mybatis Plus之QueryWrapper、LambdaQueryWrapper以及LambdaQueryChainWrapper用法)