為了聚合查詢的條件,增加了幾個資料。
我們試著取得最小的年齡。
方法實作
@Override public Integer getAgeMin() { Map<String, Object> result = testFluentMybatisMapper .findOneMap(new TestFluentMybatisQuery().select.min.age("minAge").end()) .orElse(null); return result != null ? Convert.toInt(result.get("minAge"), 0) : 0; }
控制層程式碼
@ApiOperation(value = "获取最小年龄", notes = "获取最小年龄") @RequestMapping(value = "/getAgeMin", method = RequestMethod.GET) @ResponseBody public Result<Integer> getAgeMin() { try { return Result.ok(aggregateService.getAgeMin()); } catch (Exception exception) { return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null); } }
偵錯程式碼
##程式碼說明:1、age("minAge")為什麼要加一個字串進去呢?不加可以嗎?答案是可以,不過你看到的結果回傳時是這樣的。 沒錯,括號內的是聚合查詢結果別名,不傳的話結果比較尷尬,建議還是傳一下。 MAX在做max聚合函數的時候,我來搞複雜一點,加上group by。 定義回傳實體。import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; /** @Author huyi @Date 2021/10/26 14:15 @Description: 聚合最大年龄返回体 */ @Data @AllArgsConstructor @NoArgsConstructor @Builder public class AggregateMaxAgeRsp { private String name; private Integer maxAge; }
@Override public List<AggregateMaxAgeRsp> getAgeMaxByName() { List<Map<String, Object>> result = testFluentMybatisMapper.listMaps( new TestFluentMybatisQuery() .select .name() .max .age("maxAge") .end() .groupBy .name() .end()); if (result != null && result.size() != 0) { List<AggregateMaxAgeRsp> list = new ArrayList<>(); result.forEach( x -> list.add(BeanUtil.fillBeanWithMapIgnoreCase(x, new AggregateMaxAgeRsp(), false))); return list; } else { return null; } }
@ApiOperation(value = "根据年龄分组并获取最大年龄", notes = "根据年龄分组并获取最大年龄") @RequestMapping(value = "/getAgeMaxByName", method = RequestMethod.GET) @ResponseBody public Result<List<AggregateMaxAgeRsp>> getAgeMaxByName() { try { return Result.ok(aggregateService.getAgeMaxByName()); } catch (Exception exception) { return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null); } }
import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; /** @Author huyi @Date 2021/10/26 14:50 @Description: 聚合平均总和返回体 */ @Data @AllArgsConstructor @NoArgsConstructor @Builder public class AggregateAgeSumAvgAndCountRsp { private String name; private Integer sum; private Integer avg; private Integer count; }
@Override public List<AggregateAgeSumAvgAndCountRsp> getAgeSumAvgCountByName() { List<Map<String, Object>> result = testFluentMybatisMapper.listMaps( new TestFluentMybatisQuery() .select .name() .sum .age("sum") .avg .age("avg") .count("count") .end() .groupBy .name() .end()); if (result != null && result.size() != 0) { List<AggregateAgeSumAvgAndCountRsp> list = new ArrayList<>(); result.forEach( x -> list.add( BeanUtil.fillBeanWithMapIgnoreCase( x, new AggregateAgeSumAvgAndCountRsp(), false))); return list; } else { return null; } }
@ApiOperation(value = "根据年龄分组并获取年龄和、平均年龄、数量", notes = "根据年龄分组并获取年龄和、平均年龄、数量") @RequestMapping(value = "/getAgeSumAvgCountByName", method = RequestMethod.GET) @ResponseBody public Result<List<AggregateAgeSumAvgAndCountRsp>> getAgeSumAvgCountByName() { try { return Result.ok(aggregateService.getAgeSumAvgCountByName()); } catch (Exception exception) { return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null); } }
import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; /** @Author huyi @Date 2021/10/26 15:10 @Description: 聚合应用返回体 */ @Data @AllArgsConstructor @NoArgsConstructor @Builder public class AggregateApplyRsp { private String name; private Date createTime; private Integer minAge; private Date maxTime; }
@Override public List<AggregateApplyRsp> getApply() { List<Map<String, Object>> result = testFluentMybatisMapper.listMaps( new TestFluentMybatisQuery() .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 = new ArrayList<>(); result.forEach( x -> list.add(BeanUtil.fillBeanWithMapIgnoreCase(x, new AggregateApplyRsp(), false))); return list; } else { return null; } }
@ApiOperation(value = "根据名字获取最小年龄,使用语句", notes = "根据名字获取最小年龄,使用语句") @RequestMapping(value = "/getApply", method = RequestMethod.GET) @ResponseBody public Result<List<AggregateApplyRsp>> getApply() { try { return Result.ok(aggregateService.getApply()); } catch (Exception exception) { return Result.error(ErrorCode.BASE_ERROR_CODE.getCode(), exception.getMessage(), null); } }
以上是Java Fluent Mybatis聚合查詢與apply方法流程的範例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!