总结:
无论关联嵌套还是集合嵌套,无非就是使用了不同的标签,associate和collection,来表示他们属于哪一类,而在配置的时候由主要分为直接在总的映射中进行映射和再次使用外部的映射,无论怎样变,都是将查询结果的列名和对应的类的属性名对应起来.
注意:集合嵌套中方法三使用了分块查询的,略微不同.
数据库表对应的包装类中包含其他包装类
<select id="queryPaper2" parameterType="int" resultMap="paperResultMap" >
select * from paper left join user on paper.userId = user.user_id where paper_id = #{id}
select>
<resultMap id="paperResultMap" type="com.ssii.pojo.Paper">
<id column="paper_id" property="paper_id"/>
<result column="name" property="name"/>
<result column="number" property="number"/>
<result column="detail" property="detail"/>
<association property="user" javaType="com.ssii.pojo.User">
<id column="user_id" property="user_id"/>
<result column="username" property="username"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
association>
resultMap>
数据库表对应的包装类中包含其他包装类
<resultMap id="paperResultMap" type="com.ssii.pojo.Paper">
<id column="paper_id" property="paper_id"/>
<result column="name" property="name"/>
<result column="number" property="number"/>
<result column="detail" property="detail"/>
<association column="userId" property="user" javaType="com.ssii.pojo.User" resultMap="userResultMap"/>
resultMap>
<resultMap id="userResultMap" type="com.ssii.pojo.User">
<id column="user_id" property="user_id"/>
<result column="username" property="username"/>
<result column="gender" property="gender"/>
<result column="email" property="email"/>
resultMap>
直接在整体的结果映射中对包装类的中的集合进行映射
<select id="queryBatchAndBatchDetail" resultMap="batchAndDetailResultMap">
SELECT batch.*,batchdetail.product_id,batchdetail.product_num
from batch
left join batchdetail on batch.batch_id=batchdetail.batch_id;
select>
<resultMap id="batchAndDetailResultMap" type="com.ssii.pojo.BatchAndDetail">
<id property="batch_id" column="batch_id"/>
<result property="cus_id" column="cus_id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
<collection property="batchDetails" ofType="com.ssii.pojo.BatchDetail">
<id property="id" column="id"/>
<result property="batch_id" column="batch_id"/>
<result property="product_id" column="product_id"/>
<result property="product_num" column="product_num"/>
collection>
resultMap>
在整体映射中又使用了外部映射
<select id="queryBatchAndBatchDetail" resultMap="batchAndDetailResultMap">
SELECT batch.*,batchdetail.product_id,batchdetail.product_num
from batch
left join batchdetail on batch.batch_id=batchdetail.batch_id;
select>
<resultMap id="batchAndDetailResultMap" type="com.ssii.pojo.BatchAndDetail">
<id property="batch_id" column="batch_id"/>
<result property="cus_id" column="cus_id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
<collection property="batchDetails" ofType="com.ssii.pojo.BatchDetail"
resultMap="batchDetailMap">
collection>
resultMap>
<resultMap id="batchDetailMap" type="com.ssii.pojo.BatchDetail">
<id property="id" column="id"/>
<result property="batch_id" column="batch_id"/>
<result property="product_id" column="product_id"/>
<result property="product_num" column="product_num"/>
resultMap>
此方法和前两种稍有不同:它的思想是分块查询,例如下面的片段就涉及到两个查询(select) ,一个查询批次的信息,一个查询批次的详细信息,而批次详细信息的查找范围则由批次来决定(因为存在外键),
<select id="queryBatchAndBatchDetail" resultMap="batchAndDetailResultMap">
select * from batch;
select>
<select id="findBatchDetail" parameterType="int"
resultType="com.ssii.pojo.BatchDetail">
select batchdetail.product_id,batchdetail.product_num
from batchdetail
where batchdetail.batch_id = #{value};
select>
<resultMap id="batchAndDetailResultMap" type="com.ssii.pojo.BatchAndDetail">
<id property="batch_id" column="batch_id"/>
<result property="cus_id" column="cus_id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
<collection property="batchDetails" ofType="com.ssii.pojo.BatchDetail"
select="findBatchDetail" column="batch_id">
collection>
resultMap>