.png)
接着上一篇文章:Java Fluent Mybatis 分页查询与sql日志输出详解流程篇
我把分页已经调整好了,现在实验一下官方给出的聚合查询方法。
GitHub代码仓库:GitHub仓库
为了聚合查询的条件,添加了几条数据。

我们试着获取最小的年龄。
方法实现
| 1 2 3 4 5 6 7 8 | @OverridepublicInteger getAgeMin() {  Map<String, Object> result =      testFluentMybatisMapper          .findOneMap(newTestFluentMybatisQuery().select.min.age("minAge").end())          .orElse(null);  returnresult != null? Convert.toInt(result.get("minAge"), 0) : 0;} | 
控制层代码
| 1 2 3 4 5 6 7 8 9 10 | @ApiOperation(value = "获取最小年龄", notes = "获取最小年龄")@RequestMapping(value = "/getAgeMin", method = RequestMethod.GET)@ResponseBodypublicResult<Integer> getAgeMin() {  try{    returnResult.ok(aggregateService.getAgeMin());  } catch(Exception exception) {    returnResult.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);  }} | 
调试代码

代码说明:
1、age("minAge")为什么要加一个字符串进去呢?不加可以吗?答案是可以,不过你看到的结果返回时这样的。

没错,括号内的是聚合查询结果别名,不传的话结果比较尴尬,建议还是传一下。
在做max聚合函数的时候,我来搞复杂一点,加上group by。
定义返回实体。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | importlombok.AllArgsConstructor;importlombok.Builder;importlombok.Data;importlombok.NoArgsConstructor; /** @Author huyi @Date 2021/10/26 14:15 @Description: 聚合最大年龄返回体 */@Data@AllArgsConstructor@NoArgsConstructor@BuilderpublicclassAggregateMaxAgeRsp {  privateString name;  privateInteger maxAge;} | 
方法实现
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | @OverridepublicList<AggregateMaxAgeRsp> getAgeMaxByName() {  List<Map<String, Object>> result =      testFluentMybatisMapper.listMaps(          newTestFluentMybatisQuery()              .select              .name()              .max              .age("maxAge")              .end()              .groupBy              .name()              .end());  if(result != null&& result.size() != 0) {    List<AggregateMaxAgeRsp> list = newArrayList<>();    result.forEach(        x -> list.add(BeanUtil.fillBeanWithMapIgnoreCase(x, newAggregateMaxAgeRsp(), false)));    returnlist;  } else{    returnnull;  }} | 
控制层代码
| 1 2 3 4 5 6 7 8 9 10 | @ApiOperation(value = "根据年龄分组并获取最大年龄", notes = "根据年龄分组并获取最大年龄")@RequestMapping(value = "/getAgeMaxByName", method = RequestMethod.GET)@ResponseBodypublicResult<List<AggregateMaxAgeRsp>> getAgeMaxByName() {  try{    returnResult.ok(aggregateService.getAgeMaxByName());  } catch(Exception exception) {    returnResult.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);  }} | 
调试代码

OK,没什么问题。
代码说明:
1、使用了Hutools工具BeanUtil将map的值填充到实体对象中。
sum、avg、count加一起试试吧。
定义返回体
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | importlombok.AllArgsConstructor;importlombok.Builder;importlombok.Data;importlombok.NoArgsConstructor; /** @Author huyi @Date 2021/10/26 14:50 @Description: 聚合平均总和返回体 */@Data@AllArgsConstructor@NoArgsConstructor@BuilderpublicclassAggregateAgeSumAvgAndCountRsp {  privateString name;  privateInteger sum;  privateInteger avg;  privateInteger count;} | 
方法实现
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | @OverridepublicList<AggregateAgeSumAvgAndCountRsp> getAgeSumAvgCountByName() {  List<Map<String, Object>> result =      testFluentMybatisMapper.listMaps(          newTestFluentMybatisQuery()              .select              .name()              .sum              .age("sum")              .avg              .age("avg")              .count("count")              .end()              .groupBy              .name()              .end());  if(result != null&& result.size() != 0) {    List<AggregateAgeSumAvgAndCountRsp> list = newArrayList<>();    result.forEach(        x ->            list.add(                BeanUtil.fillBeanWithMapIgnoreCase(                    x, newAggregateAgeSumAvgAndCountRsp(), false)));    returnlist;  } else{    returnnull;  }} | 
控制层代码
| 1 2 3 4 5 6 7 8 9 10 | @ApiOperation(value = "根据年龄分组并获取年龄和、平均年龄、数量", notes = "根据年龄分组并获取年龄和、平均年龄、数量")@RequestMapping(value = "/getAgeSumAvgCountByName", method = RequestMethod.GET)@ResponseBodypublicResult<List<AggregateAgeSumAvgAndCountRsp>> getAgeSumAvgCountByName() {  try{    returnResult.ok(aggregateService.getAgeSumAvgCountByName());  } catch(Exception exception) {    returnResult.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);  }} | 
调试代码

OK,完美。
官方提供了显示自由指定字段.apply语法功能。我们测试一下好不好用。
返回体定义
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | importlombok.AllArgsConstructor;importlombok.Builder;importlombok.Data;importlombok.NoArgsConstructor; importjava.util.Date; /** @Author huyi @Date 2021/10/26 15:10 @Description: 聚合应用返回体 */@Data@AllArgsConstructor@NoArgsConstructor@BuilderpublicclassAggregateApplyRsp {  privateString name;  privateDate createTime;  privateInteger minAge;  privateDate maxTime;} | 
方法实现
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @OverridepublicList<AggregateApplyRsp> getApply() {  List<Map<String, Object>> result =      testFluentMybatisMapper.listMaps(          newTestFluentMybatisQuery()              .select              .apply("name")              .createTime("createTime")              .apply("min(age) as minAge", "max(create_time) as maxTime")              .end()              .groupBy              .name()              .createTime()              .end());  if(result != null&& result.size() != 0) {    List<AggregateApplyRsp> list = newArrayList<>();    result.forEach(        x -> list.add(BeanUtil.fillBeanWithMapIgnoreCase(x, newAggregateApplyRsp(), false)));    returnlist;  } else{    returnnull;  }} | 
控制层代码
| 1 2 3 4 5 6 7 8 9 10 | @ApiOperation(value = "根据名字获取最小年龄,使用语句", notes = "根据名字获取最小年龄,使用语句")@RequestMapping(value = "/getApply", method = RequestMethod.GET)@ResponseBodypublicResult<List<AggregateApplyRsp>> getApply() {  try{    returnResult.ok(aggregateService.getApply());  } catch(Exception exception) {    returnResult.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null);  }} | 
调试代码

OK,完美。
在调试完聚合查询代码后给我直观感受是,这和写sql没啥区别,十分好理解。只是需要掌握fm的select语法,特别是end方法的理解,可以追一下源码看一下具体实现过程。
如果文章对你有帮助的话,点个赞吧,点个赞吧,点个赞吧,重要的事情说三遍。

到此这篇关于Java Fluent Mybatis 聚合查询与apply方法详解流程篇的文章就介绍到这了,更多相关Java Fluent Mybatis内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!