MybatisPlus之QueryWrapper、LambdaQueryWrapper以及LambdaQueryChainWrapper用法
阿新 • • 發佈:2020-12-09
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 }