今天分享常用API:
1、排序:
// 组装查询条件
QueryWrapper<User> wrapper = new QueryWrapper<>();
// 按 id, age 字段进行升序
wrapper.orderBy(true, true, "id", "age");
// 实际执行 SQL : SELECT id,name,age,gender FROM user ORDER BY id ASC,age ASC
List<User> users = userMapper.selectList(wrapper);
一个参数排序,倒序:
LambdaQueryWrapper<Info> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByDesc(true,Info::getUpdateTime);
2、不等于 <>
相关的方法如下
// 组装查询条件
QueryWrapper<User> wrapper = new QueryWrapper<>();
// 不等于 <> 语句
wrapper.ne("name", "犬小哈");
// 实际执行 SQL : SELECT id,name,age,gender FROM user WHERE (name <> '犬小哈')
List<User> users = userMapper.selectList(wrapper);
3、 使用ge(大于等于 >=)和le(小于等于 <=)
LambdaQueryWrapper<Info> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.ge(Info::getProcessTime, query.getStartTime());
queryWrapper.le(Info::getProcessTime, query.getEndTime());
配合日期格式:
@DateTimeFormat(pattern = "yyyy-MM-dd")
@ApiModelProperty(value = "开始时间")
private LocalDateTime startTime;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@ApiModelProperty(value = "结束时间")
private LocalDateTime endTime;
4、忽略映射字段时可以在实体类属性上加注解:
@TableField(exist = false)//表示该属性不为数据库表字段,有需要使用
private Int age;
5、聚合查询:sum() 函数
QueryWrapper<Statistics> queryWrapper = new QueryWrapper<>();
queryWrapper.select(" product_year,sum(output)as output");
queryWrapper.eq(StringUtils.isNotEmpty(query.getCity()),"city", query.getCity());
queryWrapper.eq("del_flag", DelFlagEnum.False.getCode());
queryWrapper.in("product_year",yearList);
queryWrapper.groupBy("product_year");
List<Statistics> statisticsList = statisticsMapper.selectList(queryWrapper);
6、看到一篇文章也挺详细的,我就不在重复造轮子了: