一对一关联映射
使用中的子元素
property:指定映射到的实体类对象属性,与表字段一一对应。
column:指定表中对应的字段。
javaType:指定映射到实体对象属性的类型。
实体类
public class IdCard{
private Integer id;
private String code;
}
public class Person {
private Integer id;
private String name;
private Integer age;
private String sex;
private IdCard card; //一对一,关联的身份证
}
嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集
<select id="getClass" parameterType="int" resultMap="getClassMap">
select * from class c, teacher t where c.teacher_id = t.t_id and c.teacher_id=#{id}
</select>
<!-- resultMap:映射实体类和字段之间的一一对应的关系 -->
<resultMap type="Classes" id="getClassMap">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" javaType="Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
嵌套查询 : 通过执行另外一个SQL映射语句来返回预期的复杂类型
<select id="getClass1" parameterType="int" resultMap="getClassMap1">
select * from class where c_id=#{id}
</select>
<resultMap type="Classes" id="getClassMap1">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" select="getTeacher"/>
</resultMap>
<select id="getTeacher" parameterType="int" resultType="Teacher">
select t_id id,t_name name from teacher where t_id =#{id}
</select>
一对多关联映射
使用中的子元素
以用户和订单为案例,一个用户可以拥有多个订单(一对多),同样多个订单也可以同属于一个用户多对一
实体类
public class Orders{
private Integer id; //订单id
private String number; //订单编号
}
public class User{
private Integer id; //用户编号
pirvate String username; //用户姓名
private String address; //用户地址
private List<Orders> ordersList; //用户关联订单
}
映射文件
<select id="findUserWithOrders" parameterType="Integer" resultType="UserWithOrdersResult">
SELECT
u.*,
o.id AS orders_id,
o.number
FROM
tb_user u,
tb_order o
WHERE
u.id = o.user_id
AND u.id = #{id}
</select>
<resultMap type="User" id="UserWithOrdersResult">
<id property="id" column="id"/>
<result property="username" column="username">
<result property="address" column="address">
<!--一对多关系映射:collection
property="ordersList" 表示User实体类中ordersList对象名称
ofType:表示属性集合中元素的类型
-->
<collection property="ordersList" ofType="Orders">
<id property="id" column="orders_id">
<result property="number" column="number">
</collection>
</resultMap>
多对多关联映射
案例:以订单和商品为例,一个订单可以包含多个商品,而一种商品又可以属于多个订单,因此订单和商品就是多对多的关系
实体类
public class Product{
private Integer id; //商品编号
private String name; //商品名称
private Integer price; //商品价格
private List<Order> ordersList; //与订单的关联属性
}
public class Orders{
private Integer id; //订单id
private String number; //订单编号
private List<Product> productList; //与商品的关联属性
}
映射文件
<select id="findOrdersWithProduct" parameterType="Integer" resultType="OrdersWithProductResult">
SELECT
o.*,
p.id AS pid,
p.NAME,
p.price
FROM
tb_order o,
tb_product p,
tb_ordersitem oi
WHERE
oi.orders_id =o.id
AND oi.product_id=p.id
AND o.id = #{id}
</select>
<resultMap type="User" id="OrdersWithProductResult">
<id property="id" column="id"/>
<result property="number" column="number">
<collection property="productList" ofType="Product">
<id property="id" column="pid">
<result property="name" column="name">
<result property="price" column="price">
</collection>
</resultMap>