Mybatis的关联关系映射

关联映射
关联关系是面向对象分析、面向对象设计最终的思想,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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值