Mybatis返回统计字段

这篇博客介绍了如何在Mybatis中通过resultMap来实现对表的分组统计查询,返回业务主键和统计信息。示例展示了在Mapper接口和Mapper XML文件中的配置,包括定义resultMap、设置查询条件和分组统计。
摘要由CSDN通过智能技术生成

Mybatis返回统计字段

1. 需求描述

查询一张表分组统计信息,需要返回两个字段,业务主键与统计信息。

2. 案例实现

这里主要是定义resultMap里的返回字段。
注意: 返回我使用的是resultMap 而不是 resultType。
LotteryRulePO实体里面就定义了两个参数:activityCode与codeCount。

实体对象

@Data
public class LotteryRulePO implements Serializable {
    private static final long serialVersionUID = -4316547278556004345L;
    private String activityCode;
    private Integer codeCount;
}

定义的mapper接口

List<LotteryRulePO> queryListByActivityCode(@Param("tenantId") String tenantID, @Param("lotteryRuleList")List<LotteryRule> lotteryRuleList);

mapper文件

    <resultMap id="LotteryResultMap" type="com.enn.sale.marketing.activity.settings.dal.po.ext.LotteryRulePO">
        <result column="activity_code" jdbcType="VARCHAR" property="activityCode" />
        <result column="codeCount" javaType="INTEGER" property="codeCount"/>
    </resultMap>

    <select id="queryListByActivityCode" resultMap="LotteryResultMap">
        select activity_code,count(activity_code) as codeCount
        from lottery_rule where is_deleted = 0
        and tenant_id = #{tenantId,jdbcType=VARCHAR}
        <foreach collection="lotteryRuleList" open="and activity_code in (" close=")" item="item" separator=",">
            #{item.activityCode,jdbcType=VARCHAR}
        </foreach>
        GROUP BY activity_code
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值