• Mybatis关联关系映射


    一、一对多

    1、首先使用Mybatis逆向工程生成t_hibernate_ordert_hibernate_order_item两张表的代码

    model层和Mapper层

    2、建立vo类,vo类能够定义t_hibernate_order表里所没有的属性

    vo类不仅拿到当前表的所有信息,还能拿到相关联表的所有信息

    两个表之间的关系:

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

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

    OrderVo:

    1. package com.zjy.model;
    2. import java.util.List;
    3. /**
    4. * @author小朱
    5. * @site Bi8boYin
    6. * @company xxx公司
    7. * @create  2022-08-15 19:27
    8. */
    9. //vo类不仅拿到当前表的所有信息,还能拿到相关联表的所有信息
    10. public class OrderVo extends Order {
    11. //通过订单号查询订单的详细信息以及所有订单项的信息
    12. private List orderItems;
    13. public List getOrderItems() {
    14. return orderItems;
    15. }
    16. public void setOrderItems(List orderItems) {
    17. this.orderItems = orderItems;
    18. }
    19. }

    OrderItemVo: 

    1. package com.zjy.model;
    2. /**
    3. * @author小朱
    4. * @site Bi8boYin
    5. * @company xxx公司
    6. * @create  2022-08-15 19:35
    7. */
    8. public class OrderItemVo extends OrderItem{
    9. private Order order;
    10. public Order getOrder() {
    11. return order;
    12. }
    13. public void setOrder(Order order) {
    14. this.order = order;
    15. }
    16. }

    3、在OderMapper.xml中映射实体类resultMap以及对应的方法

    1. <resultMap id="OrderVoMap" type="com.zjy.model.OrderVo" >
    2. <result property="orderId" column="order_id">result>
    3. <result property="orderNo" column="order_no">result>
    4. <collection property="orderItems" ofType="com.zjy.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>

    OrderItemMapper.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>

     

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

     

    注意:该方法的的返回值一定是vo类,因为我们在xml中resultMap节点里包含了数据库表里不包含的字段

     

    OrderMapper.java

    OrderVo queryOrderItemVoByOrderItemId(Integer orderItemId);

    OrderItemsMapper.java 

    OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId);

    5、建立业务类和业务实体类

    OrderBiz

    1. package com.zjy.biz;
    2. import com.zjy.model.Order;
    3. import org.apache.ibatis.annotations.Param;
    4. /**
    5. * @author小朱
    6. * @site Bi8boYin
    7. * @company xxx公司
    8. * @create  2022-08-15 19:58
    9. */
    10. public interface OrderBiz {
    11. int deleteByPrimaryKey(Integer orderId);
    12. int insert(Order record);
    13. int insertSelective(Order record);
    14. Order selectByPrimaryKey(Integer orderId);
    15. Order queryOrderVoByOrderId(@Param("orderId") Integer orderId);
    16. int updateByPrimaryKeySelective(Order record);
    17. int updateByPrimaryKey(Order record);
    18. }

    OrderBizImpl

    1. package com.zjy.biz.impl;
    2. import com.zjy.biz.OrderBiz;
    3. import com.zjy.mapper.OrderMapper;
    4. import com.zjy.model.Order;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. /**
    8. * @author小朱
    9. * @site Bi8boYin
    10. * @company xxx公司
    11. * @create  2022-08-15 20:00
    12. */
    13. @Service
    14. public class OrderBizImpl implements OrderBiz {
    15. @Autowired
    16. private OrderMapper orderMapper;
    17. @Override
    18. public int deleteByPrimaryKey(Integer orderId) {
    19. return 0;
    20. }
    21. @Override
    22. public int insert(Order record) {
    23. return 0;
    24. }
    25. @Override
    26. public int insertSelective(Order record) {
    27. return 0;
    28. }
    29. @Override
    30. public Order selectByPrimaryKey(Integer orderId) {
    31. return null;
    32. }
    33. @Override
    34. public Order queryOrderVoByOrderId(Integer orderId) {
    35. return orderMapper.queryOrderVoByOrderId(orderId);
    36. }
    37. @Override
    38. public int updateByPrimaryKeySelective(Order record) {
    39. return 0;
    40. }
    41. @Override
    42. public int updateByPrimaryKey(Order record) {
    43. return 0;
    44. }
    45. }

    6、编写测试类

    1. package com.zjy.biz.impl;
    2. import com.zjy.biz.OrderBiz;
    3. import org.junit.Test;
    4. import org.junit.runner.RunWith;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.test.context.ContextConfiguration;
    7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    8. /**
    9. * @author小朱
    10. * @site Bi8boYin
    11. * @company xxx公司
    12. * @create  2022-08-15 20:09
    13. */
    14. @RunWith(SpringJUnit4ClassRunner.class)
    15. @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
    16. public class OrderBizImplTest {
    17. @Autowired
    18. private OrderBiz orderBiz;
    19. @Test
    20. public void queryOrderVoByOrderId() {
    21. orderBiz.queryOrderVoByOrderId(9);
    22. }
    23. }

    两张表所对应的关系:通过订单id能够查询出订单详情,通过订单详情也能找到该条订单的id

     

     

     

    二、多对多

    1、多对多关系就是和一对多的关系的大同小异,只是多对多的关系可以看成两个一对多关系。

               2、多对多关系的配置的步骤和一对多关系配置是一样的:(以书籍表、类别表以及书籍类别表为例)

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

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

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

    1. <resultMap id="HbookVoMap" type="com.zjy.vo.HBookVo">
    2. <result property="bookId" column="book_id">result>
    3. <result property="bookName" column="book_name">result>
    4. <collection property="categories" ofType="com.zj.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.zjy.vo.HCateVo">
    10. <result property="categoryId" column="category_id">result>
    11. <result property="categoryName" column="category_name">result>
    12. <collection property="hbooks" ofType="com.zj.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>

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

    1. package com.zjy.mapper;
    2. import com.zjy.model.HBookC;
    3. import com.zjy.vo.HBookVo;
    4. import com.zjy.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. }

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

    Many2ManyService:
    1. package com.zjy.www.service;
    2. import com.zjy.www.vo.HBookVo;
    3. import com.zjy.www.vo.HCateVo;
    4. public interface Many2ManyService {
    5. HBookVo queryByBookId(Integer bookId);
    6. HCateVo queryByCid(Integer cid);
    7. }
    Many2ManyServiceImpl:
    1. package com.zjy.www.service.impl;
    2. import com.zjy.www.mapper.HBookCMapper;
    3. import com.zjy.www.service.Many2ManyService;
    4. import com.zjy.www.vo.HBookVo;
    5. import com.zjy.www.vo.HCateVo;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. @Service
    9. public class Many2ManyServiceImpl implements Many2ManyService {
    10. @Autowired
    11. private HBookCMapper hBookCMapper;
    12. @Override
    13. public HBookVo queryByBookId(Integer bookId) {
    14. return hBookCMapper.queryByBookId(bookId);
    15. }
    16. @Override
    17. public HCateVo queryByCid(Integer cid) {
    18. return hBookCMapper.queryByCid(cid);
    19. }
    20. }

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

    HBookVo:

    1. package com.zjy.www.vo;
    2. import com.zjy.www.model.HBook;
    3. import com.zjy.www.model.HBookC;
    4. import com.zjy.www.model.HCate;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. public class HBookVo extends HBook {
    8. private List<HCate> categories = new ArrayList<>();
    9. public List<HCate> getCategories() {
    10. return categories;
    11. }
    12. public void setCategories(List<HCate> categories) {
    13. this.categories = categories;
    14. }
    15. }

    HBookC:

    1. package com.zjy.www.vo;
    2. import com.zjy.www.model.HBook;
    3. import com.zjy.www.model.HCate;
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. public class HCateVo extends HCate {
    7. private List hbooks = new ArrayList<>();
    8. public List getHbooks() {
    9. return hbooks;
    10. }
    11. public void setHbooks(List hbooks) {
    12. this.hbooks = hbooks;
    13. }
    14. @Override
    15. public String toString() {
    16. return "HCateVo{" +
    17. "hbooks=" + hbooks +
    18. '}';
    19. }
    20. }

     2.6:编写测试类:

    1. package com.zjy.www.service.impl;
    2. import com.zjy.www.model.HBook;
    3. import com.zjy.www.model.HCate;
    4. import com.zjy.www.service.Many2ManyService;
    5. import com.zjy.www.vo.HBookVo;
    6. import com.zjy.www.vo.HCateVo;
    7. import junit.framework.TestCase;
    8. import org.junit.Test;
    9. import org.junit.runner.RunWith;
    10. import org.springframework.beans.factory.annotation.Autowired;
    11. import org.springframework.test.context.ContextConfiguration;
    12. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    13. @RunWith(SpringJUnit4ClassRunner.class)
    14. @ContextConfiguration(locations={"classpath:applicationContext.xml"})
    15. public class Many2ManyServiceImplTest extends TestCase {
    16. @Autowired
    17. private Many2ManyService many2ManyService;
    18. @Test
    19. public void queryByBookId() {
    20. HBookVo hbookVo = many2ManyService.queryByBookId(8);
    21. System.out.println(hbookVo);
    22. for (HCate category : hbookVo.getCategories()) {
    23. System.out.println(category);
    24. }
    25. }
    26. @Test
    27. public void queryByCid() {
    28. HCateVo categoryVo = many2ManyService.queryByCid(8);
    29. System.out.println(categoryVo);
    30. for (HBook hbook : categoryVo.getHbooks()) {
    31. System.out.println(hbook);
    32. }
    33. }
    34. }

  • 相关阅读:
    排队分数 10
    数据预处理和特征工程2-缺失值处理、分类型特征:编码与哑变量
    java计算机毕业设计基于springboo+vue的房产出租销售门户网站(房屋租赁系统源代码+数据库+Lw文档)
    电商技术揭秘三十一:智能风控与反欺诈技术
    pytest-html测试报告
    初阶数据结构学习记录——일곱 队列
    ubuntu20.04升级到22.04
    【idea windows打包 】 tar包
    十 动手学深度学习v2 ——卷积神经网络之NiN + GoogLeNet
    读取位置时发生内存访问冲突
  • 原文地址:https://blog.csdn.net/m0_67477525/article/details/126354478