需求查询返回分页list且每个list中的对象有一个list对象不想在代码上做for循环重复查询因此使用resultMap对实体做映射和子查询
直接贴xml
<!-- aaaa 实体内包含了bbbb 的List集合-->
<resultMap type="com.xxxx.aaaa" id="xxxxMap">
<id column="id" property = "id"/>
<id column="state" property = "state"/>
<!-- property bbbList 是aaaa实体的字段 select 子查询名称 column 主表的主键对应子查询的入参-->
<collection property="bbbList" ofType="com.xxxx.bbbb"
javaType="ArrayList" select="selectbbbbList" column="id">
<id column="id" property = "id"/>
<id column="a_id" property = "a_id"/>
</collection>
</resultMap>
<sql id="a_sql">
id,state
</sql>
<sql id="b_sql">
id,a_id
</sql>
<sql id="where_query">
<where>
1 = 1
</where>
</sql>
<select id="getAt" parameterType="com.xxxx.aaaa" resultMap="xxxxMap">
select <include refid="a_sql"/> from aa
<include refid="where_query"/>
order by r.iddesc
offset #{start} rows fetch next #{pageSize} rows only
</select>
<select id = "selectbbbbList" resultType = "com.xxxx.bbbb">
select <include refid="b_sql"/> from bb h where h.a_id= #{id}
order by h.delivery_date desc
</select>
<select id="getACount" resultType="java.lang.Integer">
select count(1) from aa r
<include refid="rehabilitationTreatment_query"/>
</select>
大概就是这样,之前没用子查询会导致说查出来sql10条,但是因为关联查询了b表会出现count 20条的情况。这样查询子查询就可以避免这个问题。