lambda条件构造器的三种创建方式
LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
LambdaQueryWrapper<User> lambdaQuery = Wrappers.<User>lambdaQuery();
使用lambda条件构造器的好处:防止误写,写错后编译都通不过,语句会直接爆红。
lambda条件构造器使用案例一:
// SELECT id,name,age,email FROM user WHERE (name LIKE ? AND age < ?)
@Test
public void selectByWrapperLambda(){
// 创建lambda条件构造器的三种方式
// LambdaQueryWrapper<User> lambda = new QueryWrapper<User>().lambda();
// LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
LambdaQueryWrapper<User> lambdaQuery = Wrappers.<User>lambdaQuery();
lambdaQuery.like(User::getName,"J").lt(User::getAge,40);
List<User> userList = userMapper.selectList(lambdaQuery);
userList.forEach(System.out::println);
}
lambda条件构造器使用案例二:
// SELECT id,name,age,email FROM user WHERE (name LIKE ? AND (age < ? OR email IS NOT NULL))
@Test
public void selectByWrapperLambda2(){
LambdaQueryWrapper<User> lambdaQuery = Wrappers.<User>lambdaQuery();
lambdaQuery.likeRight(User::getName, "J").and(lq->lq.lt(User::getAge,40).or().isNotNull(User::getEmail));
List<User> userList = userMapper.selectList(lambdaQuery);
userList.forEach(System.out::println);
}
lambda条件构造器使用案例三:(写法与上面不同)
// SELECT id,name,age,email FROM user WHERE (name LIKE ? AND age >= ?)
@Test
public void selectByWrapperLambda3(){
List<User> userList = new LambdaQueryChainWrapper<User>(userMapper)
.like(User::getName, "J").ge(User::getAge, 20).list();
userList.forEach(System.out::println);
}