总结:
无论关联嵌套还是集合嵌套,无非就是使用了不同的标签,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>
<!--查询结果的映射关系 -->
<!--结果映射,type表示用于封装查询结果的实体类,column表示查询结果集中的列名,property表示实体类中的属性-->
<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"/>
<!--关联的嵌套结果(写法二 其中的关联类使用了resultMap)
其中的userId是paper表中与user表关联的列名
-->
<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 batch.*,batchdetail.product_id,batchdetail.product_num
from batch
left join batchdetail on batch.batch_id=batchdetail.batch_id;
</select>-->
<!--方法三的查询语句 分块查询1 这里先查批次的信息 -->
<select id="queryBatchAndBatchDetail" resultMap="batchAndDetailResultMap">
select * from batch;
</select>
<!--分块查询2 这里查批次明细的信息-->
<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">
<!--这里的column的值是批次表中的,不是批次明细中的,这个值应该也就决定了select="findBacthDetail" 中的查询条件的值 -->
</collection>
</resultMap>