关联关系映射

本文介绍了Java中的一对多和多对多关联关系映射。在一对多关系中,以老师与班级为例,展示了如何在实体类和MyBatis映射文件中设置关联。而在多对多关系中,通过书籍与类别的例子,详细阐述了配置文件和查询方法的实现。文章提供了完整的代码示例,包括实体类、映射文件和查询接口的实现,以及测试用例的展示。
摘要由CSDN通过智能技术生成

目录

一、一对多关联关系

  一对多--->

二、多对多关联关系


一、一对多关联关系

例如:每个老师对应多个班级;一本书有多个种类

                 如书籍中,各类书籍对应的种类,中间存在一个书籍类别中间表 书籍编号bid和 cid类别编号

1.将数据表导入数据库中

2.通过mybatis-generator插件生成dao、mapper、model

   1)配置mybatis-generator插件生成文件位置
   2)修改generatorConfig.xml配置文件的生成目录(mapper和model)及对应生成关系

3.修改Customer、Order实体类
   1)实现序列化接口
   2)建立实体映射关联关系(一对多、多对一)
   #一对多:一个客户对应多个订单
   private List<Order> orders=new ArrayList<Order>();

   #多对一:多个订单对应一个客户(一个订单对应一个客户)
   private Customer customer;
   

4.配置mybatis关联映射

  一对多--->


   <resultMap id="one2many" type="Customer">
        <id column="customer_id" property="customerId"/>
        <result column="customer_name" property="customerName"/>
        <!-- 一对多的关系 -->
        <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
        <collection property="orders" ofType="Order">
            <id column="order_id" property="orderId"/>
            <result column="order_no" property="orderNo"/>
        </collection>
    </resultMap>'


 
/**
 * 对应t_hibernate_order表
 */
public class Order {
    private Integer orderId;
 
    private String orderNo;
 
    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;
    }
}

 OrderVo类


public class OrderVo extends Order {
    //通过订单号查询本次订单的详情信息以及对应的订单信息
    private List<OrderItem> orderItems = new ArrayList<>();
 
    public List<OrderItem> getOrderItems() {
        return orderItems;
    }
 
    public void setOrderItems(List<OrderItem> orderItems) {
        this.orderItems = orderItems;
    }
 
}

OrderItem类

package com.ssm.model;
 
public class OrderItem {
    private Integer orderItemId;
 
    private Integer productId;
 
    private Integer quantity;
 
    private Integer oid;
 
    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="OrderVoMap" type="com.javaxl.ssm.model.vo.OrderVo" >
  <result property="orderId" column="order_id"></result>
  <result property="orderNo" column="order_no"></result>
  <collection property="orderItems" ofType="com.javaxl.ssm.model.OrderItem">
    <result property="orderItemId" column="order_item_id"></result>
    <result property="oid" column="oid"></result>
    <result property="productId" column="product_id"></result>
    <result property="quantity" column="quantity"></result>
  </collection>
</resultMap>
 
<select id="queryOrderVoByOrderId" resultMap="OrderVoMap" parameterType="java.lang.Integer">
   select * from t_hibernate_order o,t_hibernate_order_item oi
    where o.order_id = oi.oid
    and o.order_id = #{orderId}
</select>

 更新实体类

package com.ssm.model;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 
/**
 * 对应t_hibernate_order表
 */
//相当于添加了set/get方法 同时添加了toSpring方法
//@NoArgsConstructor
//@AllArgsConstructor
@Data
public class Order {
    private Integer orderId;
 
    private String orderNo;
 
}

 一对多:

<resultMap id="OrderVoMap" type="com.javaxl.ssm.model.vo.OrderItemVo" >
  <result property="orderItemId" column="order_item_id"></result>
  <result property="oid" column="oid"></result>
  <result property="productId" column="product_id"></result>
  <result property="quantity" column="quantity"></result>
  <association property="order" javaType="com.javaxl.ssm.model.Order">
    <result property="orderId" column="order_id"></result>
    <result property="orderNo" column="order_no"></result>
  </association>
</resultMap>
 
<select id="queryOrderItemVoByOrderItemId" resultMap="OrderVoMap" parameterType="java.lang.Integer">
   select * from t_hibernate_order o,t_hibernate_order_item oi
    where o.order_id = oi.oid
    and oi.order_item_id = #{orderItemId}
</select>
package com.ssm.model;
 
import lombok.ToString;
 
@ToString
public class OrderItem {
    private Integer orderItemId;
 
    private Integer productId;
 
    private Integer quantity;
 
    private Integer oid;
 
    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;
    }
}

接口类

package com.ssm.mapper;
 
import com.ssm.model.OrderItem;
import com.ssm.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
 
public interface OrderItemMapper {
 
    OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
 
}

实现类‘’

package com.ssm.Biz;
 
import com.ssm.mapper.OrderItemMapper;
import com.ssm.model.OrderItem;
import com.ssm.model.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
 

@Service
public class OrderItemBizImpl implements OrderItemBiz {
    @Autowired
    private OrderItemMapper orderItemMapper;
 
 
    @Override
    public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
        return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
    }
 
 
 
}

 开始测试

package com.ssm.Biz.impl;
 
import com.ssm.Biz.OrderItemBiz;
import com.ssm.model.vo.OrderItemVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
import static org.junit.Assert.*;
 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
 
public class OrderItemBizImplTest {
    @Autowired
    private OrderItemBiz orderItemBiz;
 
 
    @Test
    public void queryOrderItemVoByOrderItemId() {
        OrderItemVo orderItemVo = orderItemBiz.queryOrderItemVoByOrderItemId(43);
        System.out.println(orderItemVo);
        System.out.println(orderItemVo.getOrder());
    }
}

最终效果

二、多对多关联关系

 <resultMap id="many2one" type="Order">
        <id column="order_id" property="orderId"/>
        <result column="order_no" property="orderNo"/>
        <result column="cid" property="cid"/>

        <!-- 多对一的关系 -->
        <!-- property: 指的是属性的值, javaType:指的是属性的类型-->
        <association property="customer" javaType="Customer">
            <id column="customer_id" property="customerId"/>
            <result column="customer_name" property="customerName"/>
        </association>
   </resultMap>

需求:

1.根据书籍ID,查询书籍信息及所属所有类别信息

 2.根据类别ID,查询类别信息及所包含书籍信息

配置文件

<resu

ltMap id="HbookVoMap" type="com.dengxiyan.ssm.model.vo.HbookVo">
    <result property="bookId" column="book_id"></result>
    <result property="bookName" column="book_name"></result>
    <collection property="categories" ofType="com.dengxiyan.ssm.model.Category">
      <result property="categoryId" column="category_id"></result>
      <result property="categoryName" column="category_name"></result>
    </collection>
  </resultMap>
 
  <resultMap id="CategoryVoMap" type="com.dengxiyan.ssm.model.vo.CategoryVo">
    <result property="categoryId" column="category_id"></result>
    <result property="categoryName" column="category_name"></result>
    <collection property="hbooks" ofType="com.dengxiyan.ssm.model.Hbook">
      <result property="bookId" column="book_id"></result>
      <result property="bookName" column="book_name"></result>
    </collection>
  </resultMap>
 
  <select id="queryByBookId" resultMap="HbookVoMap" parameterType="java.lang.Integer">
select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
     where b.book_id = bc.bid and bc.cid = c.category_id and b.book_id = #{bookId}
 </select>
  <select id="queryByCid" resultMap="CategoryVoMap" parameterType="java.lang.Integer">
select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
     where b.book_id = bc.bid and bc.cid = c.category_id and c.category_id=#{cid}
   </select>
 

实体类

package com.ssm.model;
 
import lombok.ToString;
 
@ToString
public class Hbook {
    private Integer bookId;
 
    private String bookName;
 
    private Float price;
 
    public Hbook(Integer bookId, String bookName, Float price) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.price = price;
    }
 
    public Hbook() {
        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.ssm.model.vo;
 
import com.ssm.model.Category;
import com.ssm.model.Hbook;
 
import java.util.List;
 

public class HbookVo extends Hbook {
 
    private List<Category> categories;
 
    public List<Category> getCategories() {
        return categories;
    }
 
    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }
}
package com.ssm.model.vo;
 
import com.ssm.model.Hbook;
 
import java.util.List;
 

public class CategoryVo {
    private List<Hbook> hbooks;
 
    public List<Hbook> getHbooks() {
        return hbooks;
    }
 
    public void setHbooks(List<Hbook> hbooks) {
        this.hbooks = hbooks;
    }
}

 接口层



@Service
public class HbookCategoryBizImpl implements HbookCategoryBiz {
   @Autowired
   private HbookCategoryMapper hbookCategoryMapper;
    
 
    public HbookVo queryByBookId(Integer bookId) {
        return hbookCategoryMapper.queryByBookId(bookId);
    }
 
    @Override
    public CategoryVo queryByCid(Integer cid) {
        return hbookCategoryMapper.queryByCid(cid);
    }
}


public interface HbookCategoryBiz {
    
 
    HbookVo queryByBookId(Integer bookId);
 
    CategoryVo queryByCid(Integer cid);
 
}

实现类


@Service
public class HbookCategoryBizImpl implements HbookCategoryBiz {
   @Autowired
 
   private HbookCategoryMapper hbookCategoryMapper;
   
 
 
    public HbookVo queryByBookId(Integer bookId) {
        return hbookCategoryMapper.queryByBookId(bookId);
    }
 
    @Override
    public CategoryVo queryByCid(Integer cid) {
        return hbookCategoryMapper.queryByCid(cid);
    }
}

 开始测试


 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
 
public class HbookCategoryBizImplTest {
   @Autowired
   private HbookCategoryBiz hbookCategoryBiz;
 
    @Test
    public void queryByBookId() {
        HbookVo hbookVo = hbookCategoryBiz.queryByBookId(8);
        System.out.println(hbookVo);
        for (Category category : hbookVo.getCategories()) {
            System.out.println(category);
        }
    }
 
    @Test
    public void queryByCid() {
        CategoryVo categoryVo = hbookCategoryBiz.queryByCid(8);
        System.out.println(categoryVo);
        for (Hbook hbook : categoryVo.getHbooks()) {
            System.out.println(hbook);
        }
    }
}

效果如图 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值