1.一对多关联查询:
示例:person与order
java实体类中:private List<Orders> orderList;
sql映射文件中:需重新定义一个resultMap
<resultMap type="person" id="selectPersonAndOrderByIdRM" extends="BaseResultMap">
<collection property="orderList" ofType="com.rl.model.Orders">
<id column="ORDER_ID" property="orderId" jdbcType="INTEGER" />
<result column="PERSON_ID" property="personId" jdbcType="INTEGER" />
<result column="TOTAL_SUM" property="totalSum" jdbcType="REAL" />
<result column="REC_ADDR" property="recAddr" jdbcType="VARCHAR" />
</collection>
</resultMap>
说明:
1.resultMap之间是可以继承的。
2.一对多collection标签介绍:
collection:处理一对多从一的一端看的关系
property:多的一端在一的一端的实体类中的集合的属性名
ofType:集合中的泛型
2.collection标签还可以嵌套:用于解决一对多,多中还有多的问题
<resultMap type="person" id="selectPersonAndOrderAndDetailByIdRM" extends="BaseResultMap">
<collection property="orderList" ofType="com.rl.model.Orders">
<id column="ORDER_ID" property="orderId" jdbcType="INTEGER" />
<result column="PERSON_ID" property="personId" jdbcType="INTEGER" />
<result column="TOTAL_SUM" property="totalSum" jdbcType="REAL" />
<result column="REC_ADDR" property="recAddr" jdbcType="VARCHAR" />
<collection property="detailList" ofType="com.rl.model.OrderDetail">
<id column="DETAIL_ID" property="detailId" jdbcType="INTEGER" />
<result column="ORDER_ID" property="orderId" jdbcType="INTEGER" />
<result column="ITEM_NAME" property="itemName" jdbcType="VARCHAR" />
<result column="PRICE" property="price" jdbcType="REAL" />
<result column="ITEM_NUM" property="itemNum" jdbcType="INTEGER" />
</collection>
</collection>
</resultMap>
3.多对一关联查询:
java代码中:
private Person person;
sql映射中需重新定义一个resultMap
<resultMap type="com.model.Orders" id="selectPersonByOrderIdRM" extends="BaseResultMap">
<association property="person" javaType="person">
<id column="person_id" property="personId"/>
<result column="name" property="name"/>
<result column="gender" property="gender"/>
<result column="person_addr" property="personAddr"/>
<result column="birthday" property="birthday"/>
</association>
</resultMap>
说明:
association标签:处理多对一的关系
property:一的一端的属性名
javaType:一的一端数据类型,区分于ofType(一对多Collection时用的)
4.如果一对多和多对一同时存在:
association要放在collection上面
<!-- 如果存在一对多和多对一的时候, association要放在collection上面-->
<resultMap type="com.model.Orders" id="selectPersonAndDetaiByOrderIdRM" extends="BaseResultMap">
<association property="person" javaType="person">
<id column="person_id" property="personId"/>
<result column="name" property="name"/>
<result column="gender" property="gender"/>
<result column="person_addr" property="personAddr"/>
<result column="birthday" property="birthday"/>
</association>
<collection property="detailList" ofType="com.rl.model.OrderDetail">
<id column="DETAIL_ID" property="detailId" jdbcType="INTEGER" />
<result column="ORDER_ID" property="orderId" jdbcType="INTEGER" />
<result column="ITEM_NAME" property="itemName" jdbcType="VARCHAR" />
<result column="PRICE" property="price" jdbcType="REAL" />
<result column="ITEM_NUM" property="itemNum" jdbcType="INTEGER" />
</collection>
</resultMap>
5.一对一关联查询:
一对一的关系和多对一一模一样,还是使用 association