In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the sample analysis of Java Fluent Mybatis aggregation query and apply method process. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Data preparation
Several pieces of data have been added to aggregate the conditions of the query.
MIN
We try to get the youngest age.
Method realization
@ Override public Integer getAgeMin () {Map result = testFluentMybatisMapper .findOneMap (new TestFluentMybatisQuery () .select.min.age ("minAge") .end ()) .orElse (null); return result! = null? Convert.toInt (result.get ("minAge"), 0): 0;}
Control layer code
@ ApiOperation (value = "get the minimum age", notes = "get the minimum age") @ RequestMapping (value = "/ getAgeMin", method = RequestMethod.GET) @ ResponseBody public Result getAgeMin () {try {return Result.ok (aggregateService.getAgeMin ());} catch (Exception exception) {return Result.error (ErrorCode.BASE_ERROR_CODE.getCode (), exception.getMessage (), null);}}
Debug code
Code description:
1. Why does age ("minAge") add a string? Is it okay not to add it? The answer is yes, but the result you see returns like this.
Yes, what is in parentheses is the alias of the aggregate query result. If the result is not passed on, it is more awkward. It is recommended to pass it on.
MAX
When I do the max aggregate function, I'll make it a little more complicated, plus group by.
Defines the return entity.
Import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.NoArgsConstructor; / * * @ Author huyi @ Date 14:15 on 2021-10-26 @ Description: maximum aggregate age return * / @ Data@AllArgsConstructor@NoArgsConstructor@Builderpublic class AggregateMaxAgeRsp {private String name; private Integer maxAge;}
Method realization
@ Override public List getAgeMaxByName () {List result = testFluentMybatisMapper.listMaps (new TestFluentMybatisQuery () .select .name () .max .age ("maxAge") .end () .groupBy .name () .end ()) If (result! = null & & result.size ()! = 0) {List list = new ArrayList (); result.forEach (x-> list.add (BeanUtil.fillBeanWithMapIgnoreCase (x, new AggregateMaxAgeRsp (), false)); return list;} else {return null;}}
Control layer code
@ ApiOperation (value = "grouping by age and obtaining the maximum age", notes = "grouping by age and obtaining the maximum age") @ RequestMapping (value = "/ getAgeMaxByName", method = RequestMethod.GET) @ ResponseBody public Result getAgeMaxByName () {try {return Result.ok (aggregateService.getAgeMaxByName ());} catch (Exception exception) {return Result.error (ErrorCode.BASE_ERROR_CODE.getCode (), exception.getMessage (), null);}}
Debug code
OK, there's no problem.
Code description:
1. The Hutools tool BeanUtil is used to populate the value of map into the solid object.
SUM 、 AVG 、 COUNT
Try sum, avg and count together.
Define the return body
Import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.NoArgsConstructor; / * * @ Author huyi @ Date 14:50 on 2021-10-26 @ Description: aggregate average sum return * / @ Data@AllArgsConstructor@NoArgsConstructor@Builderpublic class AggregateAgeSumAvgAndCountRsp {private String name; private Integer sum; private Integer avg; private Integer count;}
Method realization
@ Override public List getAgeSumAvgCountByName () {List 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 list = new ArrayList (); result.forEach (x-> list.add (BeanUtil.fillBeanWithMapIgnoreCase (x, new AggregateAgeSumAvgAndCountRsp (), false)); return list;} else {return null;}}
Control layer code
@ ApiOperation (value = "grouping by age and obtaining age sum, average age, quantity", notes = "grouping by age and obtaining age sum, average age, quantity") @ RequestMapping (value = "/ getAgeSumAvgCountByName", method = RequestMethod.GET) @ ResponseBody public Result getAgeSumAvgCountByName () {try {return Result.ok (aggregateService.getAgeSumAvgCountByName ()) } catch (Exception exception) {return Result.error (ErrorCode.BASE_ERROR_CODE.getCode (), exception.getMessage (), null);}}
Debug code
OK, perfect.
The apply method uses the
Officially provides the ability to display freely specified fields. Apply syntax. Let's test whether it works.
Return body definition
Import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.NoArgsConstructor; import java.util.Date; / * * @ Author huyi @ Date 15:10 on 2021-10-26 @ Description: aggregation App return body * / @ Data@AllArgsConstructor@NoArgsConstructor@Builderpublic class AggregateApplyRsp {private String name; private Date createTime; private Integer minAge; private Date maxTime;}
Method realization
@ Override public List getApply () {List 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 list = new ArrayList (); result.forEach (x-> list.add (BeanUtil.fillBeanWithMapIgnoreCase (x, new AggregateApplyRsp (), false)); return list;} else {return null;}}
Control layer code
@ ApiOperation (value = "get the minimum age by name, use statement", notes = "get minimum age by name, use statement") @ RequestMapping (value = "/ getApply", method = RequestMethod.GET) @ ResponseBody public Result getApply () {try {return Result.ok (aggregateService.getApply ());} catch (Exception exception) {return Result.error (ErrorCode.BASE_ERROR_CODE.getCode (), exception.getMessage (), null) }}
Debug code
OK, perfect.
This is the end of the article on "sample Analysis of Java Fluent Mybatis aggregation query and apply method process". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 287
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.