第一种用法,一个sql查询出所有需要的数据。
<resultMap type="A类路径" id="item">
<id column="id" property="id"/>
<result column="value" property="value"/>
<collection column="id" property="与A类路径字段名一致" ofType="B类路径">
<result column="id" property="b_id"/>
<result column="value" property="b_value"/>
</collection>
</resultMap>
注意:
1.如果字段名一致,需要取别名作为区分,比如上面的b_id和b_value。
2.property="与A类路径字段名一致" ----对应的是A类路径的字段名称,为了一一对应。
3.如果关联的表很多,此种方法速度会很慢。
第二中用法,多个sql查询出需要的数据。主要用于关联多表查询,提升查询速度
<resultMap type="A类路径" id="itemNew">
<id column="id" property="a_id"/>
<result column="value" property="a_value"/>
<collection column="{aId=a_id}" property="与A类字段名一致" ofType="B类路径" select="getList"> </collection>
</resultMap>
<select id="getListNew(与dao中方法名保持一致)" parameterType="类路径" resultMap="itemNew">
select a.id a_id ,a.value a_value from table_a a
</select>
<resultMap type="类路径" id="itemList">
<result column="id" property="b_id"/>
<result column="value" property="b_value"/>
</resultMap>
<select id="getList(与上面select标签值保持一致)" resultMap="itemList">
select b.id b_id , b.value b_value from table_b b where b.aid = #{aId}
</select>
说明:
1.column="{aId=a_id}" 这个字段用于传值给子collection,如果需要传多个参数,用逗号隔开,eg;column="{aId=a_id,e=e}"
2.如果有问题,请留言,看到会回复