mybatis关联关系映射

 

关联关系分类:

一对一:在实际项目中,几乎没有用不到一对一关系映射的,对一对一关系最好使用唯一主外键关联,即两张表使用外键关联关系,同时给外键列增加唯一约束。

 

一对多:实际项目中一对多关联关系也是常见的,数据库中一对多关系通常使用主外键关联,外键列应该在多方,也就是由多方维护关系。

代码示例:(以订单和订单详情为列子)

订单实体类:

package com.zking.ssm.model;

import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

@ToString
public class Order {
    private Integer orderId;

    private String orderNo;

    private List<OrderItem> orderItems =new ArrayList<>();

    public List<OrderItem> getOrderItems() {
        return orderItems;
    }

    public void setOrderItems(List<OrderItem> orderItems) {
        this.orderItems = orderItems;
    }

    public Order(Integer orderId, String orderNo) {
        this.orderId = orderId;
        this.orderNo = orderNo;
    }

    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;
    }
}

订单详情实体类: 

package com.zking.ssm.model;

import lombok.ToString;

@ToString
public class OrderItem {
    private Integer orderItemId;

    private Integer productId;

    private Integer quantity;

    private Integer oid;

    private  Order order;

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public OrderItem(Integer orderItemId, Integer productId, Integer quantity, Integer oid) {
        this.orderItemId = orderItemId;
        this.productId = productId;
        this.quantity = quantity;
        this.oid = oid;
    }

    public OrderItem() {
        super();
    }

    public Integer getOrderItemId() {
        return orderItemId;
    }

    public void setOrderItemId(Integer orderItemId) {
        this.orderItemId = orderItemId;
    }

    public Integer getProductId() {
        return productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public Integer getOid() {
        return oid;
    }

    public void setOid(Integer oid) {
        this.oid = oid;
    }
}

在订单方的配置

<resultMap id="OrederMap" type="com.zking.ssm.model.Order">
      <result property="orderId" column="order_id"></result>
      <result property="orderNo" column="order_no"></result>
  <collection property="orderItems" ofType="com.zking.ssm.model.OrderItem">
    <result property="orderItemId" column="order_item_id"></result>
    <result property="productId" column="product_id"></result>
    <result property="quantity" column="quantity"></result>
    <result property="oid" column="oid"></result>
  </collection>
  </resultMap>

 在订单详情方的配置


  <resultMap id="OrderItemMap" type="com.zking.ssm.model.OrderItem">
      <result property="orderItemId" column="order_item_id"></result>
      <result property="productId" column="product_id"></result>
      <result property="quantity" column="quantity"></result>
      <result property="oid" column="oid"></result>
    <collection property="order" ofType="com.zking.ssm.model.Order">
      <result property="orderId" column="order_id"></result>
      <result property="orderNo" column="order_no"></result>
    </collection>
  </resultMap>

多对多:多对多的关联关系映射也是比较常见的,对于数据库中的多对多关系建议使用一个中间表来维护关系。

代码示例:(以书籍和书籍类别为列)

书籍的实体类

package com.zking.ssm.model;

import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

@ToString
public class Book {
    private Integer bookId;

    private String bookName;

    private Float price;

    private List<Category> categories=new ArrayList<>();

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public Book(Integer bookId, String bookName, Float price) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.price = price;
    }

    public Book() {
        super();
    }

    public Integer getBookId() {
        return bookId;
    }

    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }
}

书籍类型的实体类:

package com.zking.ssm.model;

import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

@ToString
public class Category {
    private Integer categoryId;

    private String categoryName;

    private List<Book> books=new ArrayList<>();


    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    public Category(Integer categoryId, String categoryName) {
        this.categoryId = categoryId;
        this.categoryName = categoryName;
    }

    public Category() {
        super();
    }

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
}

中间表的实体类

package com.zking.ssm.model;

public class BookCategory {
    private Integer bcid;

    private Integer bid;

    private Integer cid;

    public BookCategory(Integer bcid, Integer bid, Integer cid) {
        this.bcid = bcid;
        this.bid = bid;
        this.cid = cid;
    }

    public BookCategory() {
        super();
    }

    public Integer getBcid() {
        return bcid;
    }

    public void setBcid(Integer bcid) {
        this.bcid = bcid;
    }

    public Integer getBid() {
        return bid;
    }

    public void setBid(Integer bid) {
        this.bid = bid;
    }

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }
}

配置文件信息

book类对应的文件

 <resultMap id="BookMap" type="com.zking.ssm.model.Book">
      <result property="bookId" column="book_id"></result>
      <result property="bookName" column="book_name"></result>
      <result property="price" column="price"></result>
    <collection property="categories" ofType="com.zking.ssm.model.Category">
      <result property="categoryId" column="category_id"></result>
      <result property="categoryName" column="category_name"></result>
    </collection>
  </resultMap>

category类对应的文件

<resultMap id="CategoryMap" type="com.zking.ssm.model.Category">
    <result property="categoryId" column="category_id"></result>
    <result property="categoryName" column="category_name"></result>
    <collection property="books" ofType="com.zking.ssm.model.Book">
      <result property="bookId" column="book_id"></result>
      <result property="bookName" column="book_name"></result>
      <result property="price" column="price"></result>
    </collection>
  </resultMap>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值