简体   繁体   中英

Is it possible to use Aggregate functions in MyBatis

I am a newbie to MyBatis. As per my knowledge every column is mapped to a property value while retrieving data from the database. So is it possible to use aggregate functions using MyBatis. To what property we will map the results? I have searched it everywhere But can't find any details about the usage of aggregate functions with MyBatis. If anyone can help, please.

Every column of the result set is mapped to a property. Therefore, it's just a matter of producing a SQL statement that does that.

For example:

<resultMap id="regionProfit" type="app.RegionProfitVO">
  <result property="region" column="region" />
  <result property="cnt" column="cnt" />
  <result property="profit" column="profit" />
</resultMap>

<select id="getRegionProfit" resultMap="regionProfit">
  select
    region,
    count(sales) as cnt,
    sum(revenue) - sum(expenses) as profit 
  from sales
  group by region
</select>

Now, in the Java code you could do something like:

List<RegionProfitVO> rp = sqlSession.selectList("getRegionProfit");

If you don't want to create another POJO, you can always retrieve the result as a list of java.util.Map<String, Object> but use resultType="hashmap" in mapper.xml file for that select query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM