mybatis高级映射(一对一,一对多)
mybatis高级映射
一对一关联映射
需求:查询订单信息,关联查询用户信息(一个订单对应一个用户)
(1)通过resultType实现
sql语句:
select orders.* , USER.username, USER. sex , USER.address FROM orders , USER WHERE orders.userid = USER .id
创建pojo类
由于orders.java不能全部映射字段,所以要新建pojo
创建一个pojo继承包括查询字段较多的·一个po类
OrdersCustom.java (订单扩展类)
//订单扩展类 public class OrdersCustom extends Orders{ //添加用户属性 private String username; private String sex; private String address; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
mapper.xml文件
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.mapper.OrdersMapperCustom"> <select id="findOrdersUser" parameterType="" resultType="cn.po.OrdersCustom"> select orders.* , USER.username, USER. sex , USER.address FROM orders , USER WHERE orders.userid = USER .id </select> </mapper>
(2)resultMap实现
sql语句
select orders.* , USER.username, USER. sex , USER.address FROM orders , USER WHERE orders.userid = USER .id
在orders类中添加user
//用户信息
private User user;
生成get ,set 方法
mapper.xml
<!-- 定义订单查询关联用户resultMap 将整合查询结果映射到cn.po.Orders--> <resultMap type="cn.po.Orders" id="OrdersUserResultMap"> <!-- 映射订单信息,非关联 --> <!-- id唯一标识,如果有多个配置多个id --> <id column="id" property="id"></id> <result column=user_id" property="userid"></result> <result column=number" property="number"></result> <result column=createtime" property="createtime"></result> <result column="note" property="note"></result> <!-- 配置映射的关联用户信息 --> <!-- association用于映射关联查询单个对象的信息 property 要将关联查询的用户信息映射到Orders的那个属性 --> <association property="user" javaType="cn.po.User"> <!-- id关联用户的唯一标识 colimn 指定唯一标识用户列 javaType映射到user的那个属性 --> <id column="user_id" property="id"/> <result column="username" property="username"/> <result column="sex" property="sex"/> <result column="address" property="address"/> </association> </resultMap> <select id="findOrdersUser" parameterType="" resultMap="OrdersUserResultMap"> select orders.* , USER.username, USER. sex , USER.address FROM orders , USER WHERE orders.userid = USER .id </select>
一对多关联映射
需求:查询订单及订单明细(一个订单里有多个商品明细)
sql语句
select orders.* , orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.orders_id FROM orders ,orderdetail WHERE orderdetail.orders_id = orders.id
在Orders类中添加订单明细(orderdetail.java)的list集合
//订单明细 private List<Orderdetail> orderdetail;
mapper.xml
<resultMap type="cn.po.Orders" id="OrdersOrderdetailResultMap"> <!-- 映射订单信息,非关联 --> <!-- id唯一标识,如果有多个配置多个id --> <id column="id" property="id"></id> <result column=user_id" property="userid"></result> <result column=number" property="number"></result> <result column=createtime" property="createtime"></result> <result column="note" property="note"></result> <!-- 配置映射的关联用户信息 --> <!-- collection用于映射关联查询多个记录映射到集合当中 property 要将关联查询的用户信息映射到Orders的那个属性 ofType指定映射到集合属性中类型 --> <collection property="orderdetail" ofType="cn.po.Orderdetail"> <id column="orderdetail_id" property="id"></id> <result column="items_id" property="itemsid"></result> <result column="items_num" property="itemsnum"></result> <result column="orders_id" property="ordersid"></result> </collection> </resultMap> <select id="findOrdersUser" parameterType="" resultMap="OrdersOrderdetailResultMap"> select orders.* , orderdetail.id orderdetail_id, orderdetail.items_id, orderdetail.items_num, orderdetail.orders_id FROM orders ,orderdetail WHERE orderdetail.orders_id = orders.id </select>