//查询所有
userService .list();
//根据查询条件查询一个返回实体类对象
userService.getOne(lambdaQueryWrapper);
//分页查询所有
userService.page(pageInfo,lambdaQueryWrapper);
//查询数量
userService .count();
//根据ID查list集合
userService .listByIds();
//根据ID删除
userService .removeById();
userService .removeByIds();
//修改
userService .update();
userMapper.updateById(实体类);
//新增
userService .save();
userMapper.insert(实体类);
//批量新增
userService .saveBatch(集合);
在jdbc连接url最后加上rewriteBatchedStatements=true,速度会提升巨大,原因详见https://blog.csdn.net/chengpei147/article/details/114969606
User one = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getAge, 11),false);
boolean b = userService.saveOrUpdateBatch(users);
LambdaUpdateWrapper是修改的时候给的条件,例如:
/**
* lambda链式编程
*/
@Test
public void test88(){
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda()
.between(User::getAge,30,60)
.orderByDesc(User::getId);
List<User> list = userService.list(queryWrapper);
list.forEach(System.out::println);
}
LambdaQueryWrapper是查询时候给条件,例如
/**
* 测试链式编程更新操作
*/
@Test
public void test99(){
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.lambda()
.le(User::getAge, 30)
.setSql("email = '[email protected]'");
userService.update(updateWrapper);
}
.eq 就是 equal等于
.ne 就是 not equal不等于
.gt 就是 greater than大于
.lt 就是 less than小于
.ge 就是 greater than or equal 大于等于
.le 就是 less than or equal 小于等于
.in 就是 in 包含(数组)
.isNull 就是 等于null
.between 就是 在2个条件之间(包括边界值)
.like 就是 模糊查询
LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.or((wrapper)->{
wrapper.eq(SysUser::getUserName, idUserPayBalUserName).or();
wrapper.eq(SysUser::getPhonenumber, idUserPayBalUserName);
});
lambdaQueryWrapper.and((wrapper)->{
wrapper.eq(SysUser::getNickName, idUserPayBalName);
}
);
SysUser one = userService.getOne(lambdaQueryWrapper);
sql语句为
SELECT *
FROM sys_user
WHERE ((user_name = '15666666669' OR phonenumber = '15666666669') AND (nick_name = '15666666669'));
也就是说,使用or()包裹的代码执行后是在sql语句的括号内,再次使用or()或者and()则另起一个括号.
LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(SysUser::getUserName, idUserPayBalUserName).or();
lambdaQueryWrapper.eq(SysUser::getPhonenumber, idUserPayBalUserName);
lambdaQueryWrapper.eq(SysUser::getNickName, idUserPayBalName);
SysUser one = userService.getOne(lambdaQueryWrapper);
SELECT *
FROM sys_user
WHERE (user_name = '15666666669' OR phonenumber = '15666666669' AND nick_name = '123');
可以理解为如下这个语句,所以这个sql和上面的例子的sql执行后的结果是完全不一样的.
SELECT *
FROM sys_user
WHERE (user_name = '15666666669' OR (phonenumber = '15666666669' AND nick_name = '123'));
lambdaQueryWrapper.last("OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY");
注意这个自定义慎用,会有sql注入的风险
lambdaQueryWrapper.apply("YEAR(tijiao_time) = {0}",year);
注意这个自定义慎用,会有sql注入的风险
这里用到了hutool的ObjectUtil.isNotEmpty来判非空
//查询时间范围
LambdaQueryWrapper<ProductKhtm> productKhtmLambdaQueryWrapper = new LambdaQueryWrapper<>();
productKhtmLambdaQueryWrapper.between(
ObjectUtil.isNotEmpty(productKhtmhz.getParams().get("endDbilldate")),
ProductKhtm::getDbilldate,
productKhtmhz.getParams().get("beginDbilldate"),
productKhtmhz.getParams().get("endDbilldate")
);
productKhtmService.list(productKhtmLambdaQueryWrapper);
本文部分来源1
本文部分来源2
本文部分来源3