MybatisPlus之QueryWrapper、LambdaQueryWrapper以及LambdaQueryChainWrapper用法

MybatisPlus3.4.1版本自定义条件构造器

Wrapper之QueryWrapper、LambdaQueryWrapper以及LambdaQueryChainWrapper

1、QueryWrapper

 .like("email", "24252") //email对应数据库中列名
 1   /**
 2      * 条件构造器 查询操作
 3      */
 4     @Test
 5     void TestQueryWrapperSelect() {
 6         //1、条件用法
 7         List<User> userList = userMapper.selectList(new QueryWrapper<User>()
 8                 .like("email", "24252")
 9                 .between("age", 20, 22)
10                 .or()
11                 .eq("name", "zcx")
12         );
13         System.out.println("userList:" + userList);
14 
15         //2、排序用法
16         List<User> users = userMapper.selectList(new QueryWrapper<User>()
17                 .eq("nick_name", "xx")
18                 .orderByAsc("age")  //升序
19 //                .orderByDesc("age") //降序
20                 .last("limit 0,3") //last用法:在sql末尾添加sql语句,有sql注入风险
21         );
22         System.out.println("users:"+users);
23 
24     }

 

2、LambdaQueryWrapper

queryWrapper.eq(User::getName,"liangd1"); //可以通过Lambda获取数据库列名
 1     @Test
 2     void TestLambdaQueryWrapper() {
 3         //1、查询单条
 4         LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
 5         queryWrapper.eq(User::getName,"liangd1");
 6         User selectOne = userMapper.selectOne(queryWrapper);
 7         System.out.println(selectOne);
 8 
 9         //2、查询list以及统计条数
10         queryWrapper.eq(User::getName, "zcx");
11         List<User> userList = userMapper.selectList(queryWrapper);
12         System.out.println("userList:" + userList);
13         Integer result = userMapper.selectCount(queryWrapper);
14         System.out.println("result:" + result);
15     }

 

3、LambdaQueryChainWrapper

//链式查询方式
User one = new LambdaQueryChainWrapper<>(userMapper)
                 .eq(User::getName, "liangd1")
                 .one();
 1     @Test
 2     void TestLambdaQueryChainWrapper() {
 3         //1、eq查询单条
 4         User one = new LambdaQueryChainWrapper<>(userMapper)
 5                 .eq(User::getName, "liangd1")
 6                 .one();
 7         System.out.println("UserOne:" + one);
 8 
 9         //2、查询list
10         List<User> users = new LambdaQueryChainWrapper<>(userMapper)
11                 .eq(User::getName, "zcx")
12                 .list();
13         System.out.println("UserList:" + users);
14 
15         //3、模糊查询
16         List<User> LikeList = new LambdaQueryChainWrapper<>(userMapper)
17                 .like(User::getEmail, "test")
18                 .list();
19         System.out.println("LikeUser:" + LikeList);
20     }
上一篇:Java代码生成随机十六进制颜色代码


下一篇:Mybatis-plus之QueryWrapper、QueryChainWrapper、LambdaQueryWrapper以及LambdaQueryChainWrapper用法