在输出映射中,如果一个需要映射的类有成员变量不是基本数据类型,而是自定义的类或者是数据结构,当需要把结果集映射到这种类的时候,就需要用到关联映射。
一对一关联映射
有两个类,Order类和User类,对应数据库里的tb_orders和tb_user表。
现在需要知道每个订单是哪个用户的,在tb_orders表里只有user_id,没有用户的具体信息,需要连接两个表查询结果,用association把结果集中的用户信息映射到user对象里。
User.java
public class User {
private int id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址
}
Order.java
public class Order {
private int id;//主键
private int userId;//下单用户id
private String number;//订单号
private Date createTime;//创建订单时间
private String note;//备注
private User user;//该订单对应的用户
}
OrderMapper.xml
<resultMap id="orderUserResultMap" type="Order">
<id column="id" property="id" />
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createTime"/>
<result column="note" property="note"/>
<!--一对一关联-->
<association property="user" javaType="User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="birthday" property="birthday"/>
<result column="address" property="address"/>
</association>
</resultMap>
<select id="findOrderAndUser" resultMap="orderUserResultMap">
select tb_orders.id, tb_orders.number, tb_orders.createtime, tb_orders.user_id, tb_orders.note, tb_user.*
from tb_orders, tb_user
where tb_orders.user_id=tb_user.id
</select>
OrderMapper.java 接口
public interface OrderMapper {
public List<Order> findOrderAndUser();
}
测试代码
@Test
public void findOrderAndUser() {
SqlSession sqlSession = sqlSessionFactory.openSession();
OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
List<Order> list = orderMapper.findOrderAndUser();
for (int i = 0; i < list.size(); i++)
System.out.println(list.get(i));
}
结果
Order{id=3, userId=1, number='1000010', createTime=Wed Feb 04 13:22:35 CST 2015, note='null', user=User{id=3, username='王五', sex='2', birthday=null, address='null', itemList=null}, details=null}
Order{id=4, userId=1, number='1000011', createTime=Tue Feb 03 13:22:41 CST 2015, note='null', user=User{id=4, username='王五', sex='2', birthday=null, address='null', itemList=null}, details=null}
Order{id=5, userId=10, number='1000012', createTime=Thu Feb 12 16:13:23 CST 2015, note='null', user=User{id=5, username='张三', sex='1', birthday=Thu Jul 10 00:00:00 CST 2014, address='北京市', itemList=null}, details=null}
一对多关联映射
一个订单有多个订单明细,用collection将结果映射到List集合里。
OrderDetail.java
public class OrderDetail {
private Integer id;
private Integer ordersId;
private Integer itemsId;
private Integer itemsNum;
private List<Item> items;
}
Order.java
public class Order {
private int id;//主键
private int userId;//下单用户id
private String number;//订单号
private Date createTime;//创建订单时间
private String note;//备注
private User user;
private List<OrderDetail> details;
}
OrderMapper.xml
<resultMap id="orderAndOrderDetail" type="Order">
<id column="tb_orders_id" property="id" />
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createTime"/>
<result column="note" property="note"/>
<!--一对多关联-->
<collection property="details" ofType="OrderDetail">
<id column="orderdetail_id" property="id"/>
<result column="orders_id" property="ordersId"/>
<result column="items_id" property="itemsId"/>
<result column="items_num" property="itemsNum"/>
</collection>
</resultMap>
<select id="findOrderAndOrderDetail" resultMap="orderAndOrderDetail">
select tb_orders.id tb_orders_id, user_id, number, createtime, note, tb_orderdetail.id orderdetail_id, orders_id, items_id,items_num from tb_orders, tb_orderdetail
where tb_orders.id=tb_orderdetail.orders_id
</select>
OrderMapper.java
public interface OrderMapper {
public List<Order> findOrderAndUser();
public List<Order> findOrderAndOrderDetail();
}
测试代码
@Test
public void findOrderAndOrderDetail() {
SqlSession sqlSession = sqlSessionFactory.openSession();
OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
List<Order> list = orderMapper.findOrderAndOrderDetail();
for (int i = 0; i < list.size(); i++)
System.out.println(list.get(i));
}
结果
Order{id=3, userId=1, number='1000010', createTime=Wed Feb 04 13:22:35 CST 2015, note='null', user=null, details=[OrderDetail{id=1, ordersId=3, itemsId=1, itemsNum=1, items=null}, OrderDetail{id=2, ordersId=3, itemsId=2, itemsNum=3, items=null}]}
Order{id=4, userId=1, number='1000011', createTime=Tue Feb 03 13:22:41 CST 2015, note='null', user=null, details=[OrderDetail{id=3, ordersId=4, itemsId=3, itemsNum=4, items=null}, OrderDetail{id=4, ordersId=4, itemsId=2, itemsNum=3, items=null}]}
id为3的订单有两个订单明细,id为4的订单也有两个订单明细。
注意:需要对查询结果的字段中重复的进行重命名,如查询结果的tb_orderdetail的id和tb_orders的id重名,如果不进行重命名会造成多个订单明细只记录到一个。
也可以定义复杂的映射关系,如一个用户有多个订单,一个订单有多个订单明细,一个订单明细对应一个商品项,可以在resultMap里用collection标签、association标签依次把映射关系写出来。