嵌套映射

当关联查询非常复杂时,可以用嵌套的select,其原理是在映射复杂数据时执行另一个select来完成

<resultMap id="order_resultMap2" type="Order" autoMapping="true"> <id column="id" property="id"/> <!-- 指定嵌套查询 column是传给内层查询的参数 --> <association property="user" column="user_id" select="selectUserByUid" javaType="user"/> </resultMap> <!-- 外层查询--> <select id="selectOrderByID2" parameterType="int" resultMap="order_resultMap2"> select * from orders where id = #{id} </select> <!-- 嵌套查询--> <select id="selectUserByUid" parameterType="int" resultType="user"> select *from kuser where id = #{id} </select>

这种方式同样适用于一对多的关联关系

<!--自定义映射--> <resultMap id="user_resultMap2" type="user" autoMapping="true"> <result column="username" property="name"/> <collection property="orders" ofType="order" select="selectOrderByUserID" column="id"> <id column="id" property="id"/> </collection> </resultMap> <select id="selectUserByID2" parameterType="int" resultMap="user_resultMap2"> select * from kuser where id = #{uid} </select> <select id="selectOrderByUserID" resultType="order" parameterType="int"> select *from orders where user_id = #{uid} </select>

1|0

原文地址:https://www.cnblogs.com/huaobin/p/14162741.html