关联映射
关联关系是面向对象分析、面向对象设计最终的思想,Mybatis完全可以理解这种关联关系,如果关系得当,Mybatis的关联映射将可以大大简化持久层数据的访问。关联关系大致可以分为以下情况:
1、一对一,一个人只能有一张身份证,而一张身份证只能属于一个人;
2、一对多,一个客户对应多个订单
3、多对多,一篇新闻对应多种类型,一种类型对应多篇新闻
当然,还有更复杂的关系,同样在购物系统中,一个用户可以有多个订单,而一个订单只能属于一个用户,再加上商品的关系就是一对多夹杂多对多的关系,但是万变都不离其中。
一对多、多对一
1、按照上举例的客户和订单的关系创建如下两张表t_customer(客户)和t_order(订单)
t_customer:
t_order:
实体类对象
/**
* 一对多
*/
public class Customer {
private Integer customerId;
private String customerName;
private List<Order> orders;
public List<Order> getOrders() {
return orders;
}
public void setOrders(List<Order> orders) {
this.orders = orders;
}
public Customer(Integer customerId, String customerName) {
this.customerId = customerId;
this.customerName = customerName;
}
public Customer() {
super();
}
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
@Override
public String toString() {
return "Customer{" +
"customerId=" + customerId +
", customerName='" + customerName + '\'' +
", orders=" + orders +
'}';
}
}
/**
* 多对一
*/
public class Order {
private Integer orderId;
private String orderNo;
private Integer cid;
private Customer customer;
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Order(Integer orderId, String orderNo, Integer cid) {
this.orderId = orderId;
this.orderNo = orderNo;
this.cid = cid;
}
public Order() {
super();
}
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
@Override
public String toString() {
return "Order{" +
"orderId=" + orderId +
", orderNo='" + orderNo + '\'' +
", cid=" + cid +
", customer=" + customer +
'}';
}
}
Mapper层接口:
CustomerMapper
@Repository
public interface CustomerMapper {
List<Customer> queryOrders();
}
OrderMapper
@Repository
public interface OrderMapper {
Order queryOrderId(Integer OrderId);
}
Mapper文件
一对多
CustomerMapper.xml
<resultMap id="oneToMany" type="com.zking.ssm.book.model.Customer">
<id property="customerId" javaType="java.lang.Integer" column="customer_Id" />
<result property="customerName" javaType="java.lang.String" column="customer_Name"/>
<collection property="orders" ofType="com.zking.ssm.book.model.Order">
<id property="orderId" javaType="java.lang.Integer" column="order_Id" />
<result property="orderNo" javaType="java.lang.String" column="order_No"/>
<result property="orderId" javaType="java.lang.Integer" column="order_Id"/>
</collection>
</resultMap>
<select id="queryOrders" resultMap="oneToMany">
select * from t_customer c left join t_order o on c.customer_id=o.cid
</select>
多对一
OrderMapper.xml
<resultMap id="ManyToOne" type="com.zking.ssm.book.model.Order">
<id property="orderId" javaType="java.lang.Integer" column="order_Id" />
<result property="orderNo" javaType="java.lang.String" column="order_No"/>
<result property="orderId" javaType="java.lang.Integer" column="order_Id"/>
<association property="customer" javaType="com.zking.ssm.book.model.Customer">
<id property="customerId" javaType="java.lang.Integer" column="customer_Id" />
<result property="customerName" javaType="java.lang.String" column="customer_Name"/>
</association>
</resultMap>
<select id="queryOrderId" resultMap="ManyToOne">
select * from t_customer c inner join t_order o on c.customer_id=o.cid
where o.order_id=#{value}
</select>