繁体   English   中英

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

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

我是 MyBatis 的新手。 据我所知,在从数据库中检索数据时,每一列都映射到一个属性值。 那么是否可以使用MyBatis来使用聚合函数。 我们将 map 结果赋予什么属性? 我到处都搜索过但是找不到任何关于聚合函数在 MyBatis 中的用法的细节。 如果有人可以提供帮助,请。

结果集的每一列都映射到一个属性。 因此,只需生成一个 SQL 语句即可。

例如:

<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>

现在,在 Java 代码中,您可以执行以下操作:

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

如果您不想创建另一个 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