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>