【Mybatis】【ResultMap-Collection】使用记录

如需在查询 baseSelect 中返回统计数 total,则可在 ResultMap 中用 Collection 进行处理。
但具体使用方法没有参考到网上具体例子,于是试了下错,得出结果如下:

<select id="baseSelect" resultMap="MyResultMap">
select p_id,p_value from p_detail where p_id = #{pId}
</select>

<resultMap id="MyResultMap" type="com.test.service.demo.model.P" >
<id column="p_id" property="pId" jdbcType="INTEGER" />
<result column="p_value" property="pValue" jdbcType="DECIMAL" />
<collection property="total" model.P 的 total
column="{pId = p_id}" baseSelect返回的p_id 用作childSelect的查询参数pId
select="chilrdSelect" 该字段需要调用的子查询
javaType="java.math.BigDecimal" /> model.P中 total的类型
</resultMap>

<select id="chilrdSelect" resultType="java.math.BigDecimal">
select sum(dt.value) total
from p_detail dt
where dt.p_id = #{pId}
</select>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容