• Mybatis04(关联关系映射)


    目录

    一,一对多

    二,多对多


    前言:

    一对多是什么

    例:一个老师教多个班级

    连表查询,可以拿到每个班级的详细信息以及对应的授课老师

    select * from teacher right join class on t.id=c.tid  where c.id=666

    多对多是什么:

    比如一个书籍对应多种类别或者一个类别对应了多本书,而中间必定会有一个书籍类别中间表

     以下就用订单表以及订单项两个表作为案例来给大家讲解以下:

    数据库展示:

    订单表:

    订单项表:

     一,一对多

    用代码生成器生成对应的mapper和model

    generatorConfig.xml: 

    1. "" tableName="t_hibernate_order" domainObjectName="Order"
    2. enableCountByExample="false" enableDeleteByExample="false"
    3. enableSelectByExample="false" enableUpdateByExample="false">
    4. <table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"
    5. enableCountByExample="false" enableDeleteByExample="false"
    6. enableSelectByExample="false" enableUpdateByExample="false">
    7. table>

     成功生成:

    然后我们根据两个需求来完成这一期的展示

    需求一:

    通过订单号查询本次订单的详细信息以及对应的所属订单项信息

    select * from  t_hibernate_order o , t_hibernate_order_item oi

    where o.order_id=oi.oid and o.order_id=9;

    需求二:

    通过订单项的id查询出订单项详细信息及所有订单

    select * from  t_hibernate_order o , t_hibernate_order_item oi

    where o.order_id=oi.oid and oi.order_ item_id=43;

    在对应的orderMpper.xml和orderItemMpper.xml中配置resultMap节点以及对应的方法:

    orderMapper.xml:

    1. <resultMap id="OrderVoMap" type="com.javaxl.ssm.model.vo.OrderVo" >
    2. <result property="orderId" column="order_id">result>
    3. <result property="orderNo" column="order_no">result>
    4. <collection property="orderItems" ofType="com.javaxl.ssm.model.OrderItem">
    5. <result property="orderItemId" column="order_item_id">result>
    6. <result property="oid" column="oid">result>
    7. <result property="productId" column="product_id">result>
    8. <result property="quantity" column="quantity">result>
    9. collection>
    10. resultMap>
    11. <select id="queryOrderVoByOrderId" resultMap="OrderVoMap" parameterType="java.lang.Integer">
    12. select * from t_hibernate_order o,t_hibernate_order_item oi
    13. where o.order_id = oi.oid
    14. and o.order_id = #{orderId}
    15. select>

     orderItemMpper.xml:

    1. <resultMap id="OrderVoMap" type="com.javaxl.ssm.model.vo.OrderItemVo" >
    2. <result property="orderItemId" column="order_item_id">result>
    3. <result property="oid" column="oid">result>
    4. <result property="productId" column="product_id">result>
    5. <result property="quantity" column="quantity">result>
    6. <association property="order" javaType="com.javaxl.ssm.model.Order">
    7. <result property="orderId" column="order_id">result>
    8. <result property="orderNo" column="order_no">result>
    9. association>
    10. resultMap>
    11. <select id="queryOrderItemVoByOrderItemId" resultMap="OrderVoMap" parameterType="java.lang.Integer">
    12. select * from t_hibernate_order o,t_hibernate_order_item oi
    13. where o.order_id = oi.oid
    14. and oi.order_item_id = #{orderItemId}
    15. select>

    在对应的OrderMapper.java和OrderItemsMapper.java中配置出对应的方法:

    OrderMapper.java:

    OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);

    OrderItemsMapper.java:

    OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
     

     之后到service层中接口和实现类中写好方法

    配置两个表之间的关系,建立vo类:

    两个表之间的关系:

    1、一个订单有多个订单详情表

    2、一个订单详情只有一个订单

    OrderVo : 

    1. package com.ljj.vo;
    2. import com.ljj.model.Order;
    3. import com.ljj.model.OrderItem;
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. public class OrderVo extends Order {
    7. private List orderItems = new ArrayList<>();
    8. public List getOrderItems() {
    9. return orderItems;
    10. }
    11. public void setOrderItems(List orderItems) {
    12. this.orderItems = orderItems;
    13. }
    14. @Override
    15. public String toString() {
    16. return "OrderVo{" +
    17. "orderItems=" + orderItems +
    18. '}';
    19. }
    20. }

    OrderItemVo : 

    1. package com.ljj.vo;
    2. import com.ljj.model.Order;
    3. import com.ljj.model.OrderItem;
    4. public class OrderItemVo extends OrderItem {
    5. private Order order;
    6. public Order getOrder() {
    7. return order;
    8. }
    9. public void setOrder(Order order) {
    10. this.order = order;
    11. }
    12. @Override
    13. public String toString() {
    14. return "OrderItemVo{" +
    15. "order=" + order +
    16. '}';
    17. }
    18. }

     效果展示:

    一个订单项

     一笔订单:

    二、多对多

     1、大同小异,可以看成两个一对多关系。
     2、多对多关系的配置的步骤和一对多关系配置是一样的

    首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,这两张表对应的model与mapper

    1. "" tableName="t_hibernate_book" domainObjectName="HBook"
    2. enableCountByExample="false" enableDeleteByExample="false"
    3. enableSelectByExample="false" enableUpdateByExample="false">
    4. <table schema="" tableName="t_hibernate_book_category" domainObjectName="HBookC"
    5. enableCountByExample="false" enableDeleteByExample="false"
    6. enableSelectByExample="false" enableUpdateByExample="false">
    7. table>
    8. <table schema="" tableName="t_hibernate_category" domainObjectName="HCate"
    9. enableCountByExample="false" enableDeleteByExample="false"
    10. enableSelectByExample="false" enableUpdateByExample="false">
    11. table>

        在对应的HbookCategoryMapper.xml配置resultMap节点以及对应的方法:(为什么只在HbookCategoryMapper.xml中进行配置,因为只是一个中间表,可以起到一个连接的动作):

    1. <resultMap id="HbookVoMap" type="com.ljj.www.vo.HBookVo">
    2. <result property="bookId" column="book_id">result>
    3. <result property="bookName" column="book_name">result>
    4. <collection property="categories" ofType="com.ljj.www.model.HCate">
    5. <result property="categoryId" column="category_id">result>
    6. <result property="categoryName" column="category_name">result>
    7. collection>
    8. resultMap>
    9. <resultMap id="CategoryVoMap" type="com.ljj.www.vo.HCateVo">
    10. <result property="categoryId" column="category_id">result>
    11. <result property="categoryName" column="category_name">result>
    12. <collection property="hbooks" ofType="com.ljj.www.model.HBook">
    13. <result property="bookId" column="book_id">result>
    14. <result property="bookName" column="book_name">result>
    15. collection>
    16. resultMap>
    17. <select id="queryByBookId" resultMap="HbookVoMap" parameterType="java.lang.Integer">
    18. select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
    19. where b.book_id = bc.bid and bc.cid = c.category_id and b.book_id = #{bookId}
    20. select>
    21. <select id="queryByCid" resultMap="CategoryVoMap" parameterType="java.lang.Integer">
    22. select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
    23. where b.book_id = bc.bid and bc.cid = c.category_id and c.category_id=#{cid}
    24. select>

    在对应的HbookCategoryMapper.java文件中写好方法:

    1. package com.ljj.mapper;
    2. import com.ljj.model.HBookC;
    3. import com.ljj.vo.HBookVo;
    4. import com.ljj.vo.HCateVo;
    5. import org.apache.ibatis.annotations.Param;
    6. public interface HBookCMapper {
    7. int deleteByPrimaryKey(Integer bcid);
    8. int insert(HBookC record);
    9. int insertSelective(HBookC record);
    10. HBookC selectByPrimaryKey(Integer bcid);
    11. int updateByPrimaryKeySelective(HBookC record);
    12. int updateByPrimaryKey(HBookC record);
    13. HBookVo queryByBookId(@Param("bookId") Integer bookId);
    14. HCateVo queryByCid(@Param("cid") Integer cid);
    15. }

    展示效果:

     

  • 相关阅读:
    Nlog&Prism&WPF
    ORACLE-统计信息收集&&分析表和索引
    ArrayList和linkedList的区别精简概述
    【LeetCode】【Java】有效的数组
    浅刷牛客链表题,逐步深入链表,理解链表
    unity设计模式——代理模式
    centos7.9离线安装docker
    C# 通过自定义控件实现炫酷的时间显示
    Python 3 显示图像的方法
    如何将 ASP.NET Core MVC 项目的视图分离到另一个项目
  • 原文地址:https://blog.csdn.net/weixin_64313980/article/details/126363631