简体   繁体   English

是否可以在 MyBatis 中使用聚合函数

[英]Is it possible to use Aggregate functions in MyBatis

I am a newbie to MyBatis.我是 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.那么是否可以使用MyBatis来使用聚合函数。 To what property we will map the results?我们将 map 结果赋予什么属性? I have searched it everywhere But can't find any details about the usage of aggregate functions with MyBatis.我到处都搜索过但是找不到任何关于聚合函数在 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.因此,只需生成一个 SQL 语句即可。

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:现在,在 Java 代码中,您可以执行以下操作:

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如果您不想创建另一个 POJO,您始终可以将结果检索为 java.util.Map<String, Object> 的列表,但在 select 查询的 mapper.xml 文件中使用resultType="hashmap"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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