• Mybatis的三种映射关系以及联表查询


    目录

    一、概念

    二、一对一

    1、配置generatorConfig.xml

    2、Vo包的编写

    3、xml的sql编写

    4、编写对应接口及实现类

    5、测试

    三、一对多

    1、Vo包类的编写

    2、xml的sql编写

    3、编写对应接口及实现类

    4、测试

    四、多对多

    1、Vo类

    2、xml的sql配置

    3、接口及接口实现类

    4、测试


    一、概念


    1、MyBatis中表之间的关系是如何映射的处理的?
    resultType:使用多表查询我们经常会resultType="java.utils.Map" ,我们不推荐这样写,但是这样写对自己比较有利。

    好处:resultType 是直接将查询结果映射到 Java 对象,可以使用简单的类型(如 int、String)或复杂的自定义类型。它的好处是简单直观,易于使用。
    弊端:对于复杂的关系映射,resultType 可能会变得冗长,并且无法处理一对多或多对多的关系映射。
    resultMap:resultMap 允许我们定义复杂的映射规则,将结果集中的多个字段映射到一个对象中。

    好处:可以处理复杂的关系映射,支持一对多或多对多的关系映射。我们可以在 resultMap 中定义映射规则,指定字段与属性间的映射关系,并通过嵌套 resultMap 处理表之间的关系。
    弊端:相对于 resultType,resultMap 的配置较为繁琐。

    二、一对一

    1、配置generatorConfig.xml

    在我们的配置文件里面配置我们需要的几个表,自动生成所需文件

    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="HBookCategory"
    6. enableCountByExample="false" enableDeleteByExample="false"
    7. enableSelectByExample="false" enableUpdateByExample="false">
    8. table>
    9. <table schema="" tableName="t_hibernate_category" domainObjectName="HCategory"
    10. enableCountByExample="false" enableDeleteByExample="false"
    11. enableSelectByExample="false" enableUpdateByExample="false">
    12. table>
    13. <table schema="" tableName="t_hibernate_order" domainObjectName="HOrder"
    14. enableCountByExample="false" enableDeleteByExample="false"
    15. enableSelectByExample="false" enableUpdateByExample="false">
    16. table>
    17. <table schema="" tableName="t_hibernate_order_item" domainObjectName="HOrderItem"
    18. enableCountByExample="false" enableDeleteByExample="false"
    19. enableSelectByExample="false" enableUpdateByExample="false">
    20. table>

    然后生成我们想要的model和xml映射文件

    2、Vo包的编写

    当然我们要先建立这个包里面的类才能更好的下一步。

    我们现在示例的是一对一的,所以根据前面以此类推我们建立一个HOrderItemVo类

    1. package com.liwen.vo;
    2. import com.liwen.model.HOrder;
    3. import com.liwen.model.HOrderItem;
    4. /**
    5. * @软件包名 com.liwen.vo
    6. * @用户 liwen
    7. * @create 2023-08-26 下午4:37
    8. * @注释说明:
    9. */
    10. public class HOrderItemVo extends HOrderItem {
    11. private HOrder hOrder;
    12. public HOrder gethOrder() {
    13. return hOrder;
    14. }
    15. public void sethOrder(HOrder hOrder) {
    16. this.hOrder = hOrder;
    17. }
    18. }

    3、xml的sql编写

    在我们的里面添加一个sql的方法编写

    1. <resultMap id="HOrderItemVoMap" type="com.liwen.vo.HOrderItemVo">
    2. <result column="order_itemId" property="orderItemId"/>
    3. <result column="product_id" property="productId"/>
    4. <result column="quantity" property="quantity"/>
    5. <result column="oid" property="oid"/>
    6. <association property="hOrder" javaType="com.liwen.model.HOrder">
    7. <result column="order_id" property="orderId"/>
    8. <result column="order_no" property="orderNo"/>
    9. association>
    10. resultMap>
    11. <select id="selectByHOrderId" resultMap="HOrderItemVoMap" parameterType="java.lang.Integer">
    12. select *
    13. from t_hibernate_order o,
    14. t_hibernate_order_item oi
    15. where o.order_id = oi.oid
    16. and oi.order_item_id = #{oiid}
    17. select>

    4、编写对应接口及实现类

    在上面我们已经写好了sql,我们生成对应的接口及接口实现方法。

    在我们生成的HOrderItemMapper 接口里面编写

    1. package com.liwen.mapper;
    2. import com.liwen.model.HOrderItem;
    3. import com.liwen.vo.HOrderItemVo;
    4. import org.apache.ibatis.annotations.Param;
    5. import org.springframework.stereotype.Repository;
    6. @Repository
    7. public interface HOrderItemMapper {
    8. int deleteByPrimaryKey(Integer orderItemId);
    9. int insert(HOrderItem record);
    10. int insertSelective(HOrderItem record);
    11. HOrderItem selectByPrimaryKey(Integer orderItemId);
    12. int updateByPrimaryKeySelective(HOrderItem record);
    13. int updateByPrimaryKey(HOrderItem record);
    14. HOrderItemVo selectByHOrderId(@Param("oiid") Integer oiid);
    15. }

    创建一个biz的包,里面编写一个HOrderItemBiz接口类并且编写接口方法

    1. package com.liwen.biz;
    2. import com.liwen.vo.HOrderItemVo;
    3. /**
    4. * @软件包名 com.liwen.biz
    5. * @用户 liwen
    6. * @create 2023-08-26 下午4:48
    7. * @注释说明:
    8. */
    9. public interface HOrderItemBiz {
    10. HOrderItemVo selectByHOrderId(Integer oiid);
    11. }

    在这个biz里面新建一个impl包,里面创建一个HOrderItemBizImpl 接口实现类,继承HOrderItemBiz

    1. package com.liwen.biz.impl;
    2. import com.liwen.biz.HOrderItemBiz;
    3. import com.liwen.mapper.HOrderItemMapper;
    4. import com.liwen.vo.HOrderItemVo;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. /**
    8. * @软件包名 com.liwen.biz.impl
    9. * @用户 liwen
    10. * @create 2023-08-26 下午4:48
    11. * @注释说明:
    12. */
    13. @Service
    14. public class HOrderItemBizImpl implements HOrderItemBiz {
    15. @Autowired
    16. private HOrderItemMapper hOrderItemMapper;
    17. @Override
    18. public HOrderItemVo selectByHOrderId(Integer oiid) {
    19. return hOrderItemMapper.selectByHOrderId(oiid);
    20. }
    21. }

    5、测试

    1. package com.liwen.biz.impl;
    2. import com.liwen.biz.HOrderItemBiz;
    3. import com.liwen.vo.HOrderItemVo;
    4. import org.junit.Test;
    5. import org.junit.runner.RunWith;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.test.context.ContextConfiguration;
    8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    9. /**
    10. * @软件包名 com.liwen.biz.impl
    11. * @用户 liwen
    12. * @create 2023-08-26 下午4:58
    13. * @注释说明:
    14. */
    15. @RunWith(SpringJUnit4ClassRunner.class)
    16. @ContextConfiguration(locations = {"classpath:spring-context.xml"})
    17. public class HOrderItemBizImplTest {
    18. @Autowired
    19. private HOrderItemBiz hOrderItemBiz;
    20. @Test
    21. public void selectByHOrderId() {
    22. HOrderItemVo hOrderItemVo = hOrderItemBiz.selectByHOrderId(27);
    23. System.out.println(hOrderItemVo);
    24. }
    25. }

    三、一对多

    1、Vo包类的编写

    因为我们是一对多的所以我们再编写vo类的时候,里面是使用list集合

    1. package com.liwen.vo;
    2. import com.liwen.model.HOrder;
    3. import com.liwen.model.HOrderItem;
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. /**
    7. * @软件包名 com.liwen.vo
    8. * @用户 liwen
    9. * @create 2023-08-26 下午3:55
    10. * @注释说明:
    11. */
    12. public class HOrderVo extends HOrder {
    13. // 一个订单存在多个订单项
    14. private List hOrderItems = new ArrayList<>();
    15. public List gethOrderItems() {
    16. return hOrderItems;
    17. }
    18. public void sethOrderItems(List hOrderItems) {
    19. this.hOrderItems = hOrderItems;
    20. }
    21. }

    2、xml的sql编写

    在原本的基础的sql上我们增加一个一对多的sql

    1. "HOrderVoMap" type="com.liwen.vo.HOrderVo">
    2. "order_id" property="orderId"/>
    3. "order_no" property="orderNo"/>
    4. "hOrderItems" ofType="com.liwen.model.HOrderItem">
    5. "order_itemId" property="orderItemId"/>
    6. "product_id" property="productId"/>
    7. "quantity" property="quantity"/>
    8. "oid" property="oid"/>

    3、编写对应接口及实现类

    根据sql生成的对应的HOrderMapper 类里面生成已经编写好的sql方法

    1. package com.liwen.mapper;
    2. import com.liwen.model.HOrder;
    3. import com.liwen.vo.HOrderVo;
    4. import org.apache.ibatis.annotations.Param;
    5. import org.springframework.stereotype.Repository;
    6. @Repository
    7. public interface HOrderMapper {
    8. int deleteByPrimaryKey(Integer orderId);
    9. int insert(HOrder record);
    10. int insertSelective(HOrder record);
    11. HOrder selectByPrimaryKey(Integer orderId);
    12. int updateByPrimaryKeySelective(HOrder record);
    13. int updateByPrimaryKey(HOrder record);
    14. //============================================
    15. HOrderVo byOid(@Param("oid") Integer oid);
    16. }

    在biz包里面新建一个接口HOrderBiz 

    1. package com.liwen.biz;
    2. import com.liwen.vo.HOrderVo;
    3. /**
    4. * @软件包名 com.liwen.biz
    5. * @用户 liwen
    6. * @create 2023-08-26 下午4:15
    7. * @注释说明:
    8. */
    9. public interface HOrderBiz {
    10. HOrderVo byOid(Integer oid);
    11. }

    在biz包里面的impl里面新建一个Java类实现HOrderBiz 接口

    1. package com.liwen.biz.impl;
    2. import com.liwen.biz.HOrderBiz;
    3. import com.liwen.mapper.HOrderMapper;
    4. import com.liwen.vo.HOrderVo;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. /**
    8. * @软件包名 com.liwen.biz.impl
    9. * @用户 liwen
    10. * @create 2023-08-26 下午4:16
    11. * @注释说明:
    12. */
    13. @Service
    14. public class HOrderBizImpl implements HOrderBiz {
    15. @Autowired
    16. private HOrderMapper hOrderMapper;
    17. @Override
    18. public HOrderVo byOid(Integer oid) {
    19. return hOrderMapper.byOid(oid);
    20. }
    21. }

    4、测试

    1. package com.liwen.biz.impl;
    2. import com.liwen.biz.HOrderBiz;
    3. import com.liwen.vo.HOrderVo;
    4. import org.junit.After;
    5. import org.junit.Before;
    6. import org.junit.Test;
    7. import org.junit.runner.RunWith;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.test.context.ContextConfiguration;
    10. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    11. import static org.junit.Assert.*;
    12. /**
    13. * @软件包名 com.liwen.biz.impl
    14. * @用户 liwen
    15. * @create 2023-08-26 下午4:22
    16. * @注释说明:
    17. */
    18. @RunWith(SpringJUnit4ClassRunner.class)
    19. @ContextConfiguration(locations = {"classpath:spring-context.xml"})
    20. public class HOrderBizImplTest {
    21. @Autowired
    22. private HOrderBiz hOrderBiz;
    23. @Test
    24. public void byOid() {
    25. HOrderVo hOrderVo = hOrderBiz.byOid(7);
    26. System.out.println(hOrderVo);
    27. }
    28. }

    四、多对多

    1、Vo类

    1. package com.liwen.vo;
    2. import com.liwen.model.HBook;
    3. import com.liwen.model.HCategory;
    4. import java.util.List;
    5. /**
    6. * @软件包名 com.liwen.vo
    7. * @用户 liwen
    8. * @create 2023-08-27 下午10:29
    9. * @注释说明:
    10. */
    11. public class HBookVo extends HBook {
    12. private List hCategoryList;
    13. public List gethCategoryList() {
    14. return hCategoryList;
    15. }
    16. public void sethCategoryList(List hCategoryList) {
    17. this.hCategoryList = hCategoryList;
    18. }
    19. }
    1. package com.liwen.vo;
    2. import com.liwen.model.HBook;
    3. import com.liwen.model.HCategory;
    4. import java.util.ArrayList;
    5. import java.util.List;
    6. /**
    7. * @软件包名 com.liwen.vo
    8. * @用户 liwen
    9. * @create 2023-08-27 下午11:03
    10. * @注释说明:
    11. */
    12. public class HCategoryVo extends HCategory {
    13. private List hBooks = new ArrayList<>();
    14. public List gethBooks() {
    15. return hBooks;
    16. }
    17. public void sethBooks(List hBooks) {
    18. this.hBooks = hBooks;
    19. }
    20. }

    2、xml的sql配置

    分别在不同的xml配置文件里面配置

    1. <resultMap id="HBookVo" type="com.liwen.vo.HBookVo">
    2. <result column="book_id" property="bookId"/>
    3. <result column="book_name" property="bookName"/>
    4. <result column="price" property="price"/>
    5. <collection property="hCategoryList" ofType="com.liwen.model.HCategory">
    6. <result column="category_id" property="categoryId"/>
    7. <result column="category_name" property="categoryName"/>
    8. collection>
    9. resultMap>
    10. <select id="selectByBId" resultMap="HBookVo" parameterType="java.lang.Integer">
    11. select *
    12. from t_hibernate_book b,
    13. t_hibernate_book_category bc,
    14. t_hibernate_category c
    15. where b.book_id = bc.bid
    16. and bc.cid = c.category_id
    17. and b.book_id = #{bid}
    18. select>
    1. <resultMap id="HCategoryVo" type="com.liwen.vo.HCategoryVo">
    2. <result column="category_id" property="categoryId"/>
    3. <result column="category_name" property="categoryName"/>
    4. <collection property="hBooks" ofType="com.liwen.model.HBook">
    5. <result column="book_id" property="bookId"/>
    6. <result column="book_name" property="bookName"/>
    7. <result column="price" property="price"/>
    8. collection>
    9. resultMap>
    10. <select id="selectByCId" resultMap="HCategoryVo" parameterType="java.lang.Integer">
    11. select *
    12. from t_hibernate_book b,
    13. t_hibernate_book_category bc,
    14. t_hibernate_category c
    15. where b.book_id = bc.bid
    16. and bc.cid = c.category_id
    17. and c.category_id = #{cid}
    18. select>

    3、接口及接口实现类

    在生成的接口类里面编写对应的接口方法

    1. package com.liwen.mapper;
    2. import com.liwen.model.HBook;
    3. import com.liwen.vo.HBookVo;
    4. import org.apache.ibatis.annotations.Param;
    5. public interface HBookMapper {
    6. int deleteByPrimaryKey(Integer bookId);
    7. int insert(HBook record);
    8. int insertSelective(HBook record);
    9. HBook selectByPrimaryKey(Integer bookId);
    10. int updateByPrimaryKeySelective(HBook record);
    11. int updateByPrimaryKey(HBook record);
    12. HBookVo selectByBId(@Param("bid") Integer bid);
    13. }
    1. package com.liwen.mapper;
    2. import com.liwen.model.HCategory;
    3. import com.liwen.vo.HCategoryVo;
    4. import org.apache.ibatis.annotations.Param;
    5. import org.springframework.stereotype.Repository;
    6. @Repository
    7. public interface HCategoryMapper {
    8. int deleteByPrimaryKey(Integer categoryId);
    9. int insert(HCategory record);
    10. int insertSelective(HCategory record);
    11. HCategory selectByPrimaryKey(Integer categoryId);
    12. int updateByPrimaryKeySelective(HCategory record);
    13. int updateByPrimaryKey(HCategory record);
    14. HCategoryVo selectByCId(@Param("cid") Integer cid);
    15. }

    在biz包里面新建一个HBookBiz接口类

    1. package com.liwen.biz;
    2. import com.liwen.vo.HBookVo;
    3. /**
    4. * @软件包名 com.liwen.biz
    5. * @用户 liwen
    6. * @create 2023-08-27 下午10:50
    7. * @注释说明:
    8. */
    9. public interface HBookBiz {
    10. HBookVo selectByBId(Integer bid);
    11. }
    1. package com.liwen.biz;
    2. import com.liwen.vo.HCategoryVo;
    3. public interface HCategoryBiz {
    4. HCategoryVo selectByCId(Integer cid);
    5. }

    在Biz里面的impl包里面新

    1. package com.liwen.biz.impl;
    2. import com.liwen.biz.HBookBiz;
    3. import com.liwen.mapper.HBookMapper;
    4. import com.liwen.vo.HBookVo;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. /**
    8. * @软件包名 com.liwen.biz.impl
    9. * @用户 liwen
    10. * @create 2023-08-27 下午10:53
    11. * @注释说明:
    12. */
    13. @Service
    14. public class HBookBizImpl implements HBookBiz {
    15. @Autowired
    16. private HBookMapper hBookMapper;
    17. @Override
    18. public HBookVo selectByBId(Integer bid) {
    19. return hBookMapper.selectByBId(bid);
    20. }
    21. }

    建HBookBizImpl 接口实现HBookBiz接口类

    1. package com.liwen.biz.impl;
    2. import com.liwen.biz.HBookBiz;
    3. import com.liwen.mapper.HBookMapper;
    4. import com.liwen.vo.HBookVo;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. /**
    8. * @软件包名 com.liwen.biz.impl
    9. * @用户 liwen
    10. * @create 2023-08-27 下午10:53
    11. * @注释说明:
    12. */
    13. @Service
    14. public class HBookBizImpl implements HBookBiz {
    15. @Autowired
    16. private HBookMapper hBookMapper;
    17. @Override
    18. public HBookVo selectByBId(Integer bid) {
    19. return hBookMapper.selectByBId(bid);
    20. }
    21. }
    1. package com.liwen.biz.impl;
    2. import com.liwen.biz.HCategoryBiz;
    3. import com.liwen.mapper.HCategoryMapper;
    4. import com.liwen.vo.HCategoryVo;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Service;
    7. /**
    8. * @软件包名 com.liwen.biz.impl
    9. * @用户 liwen
    10. * @create 2023-08-27 下午11:12
    11. * @注释说明:
    12. */
    13. @Service
    14. public class HCategoryBizImpl implements HCategoryBiz {
    15. @Autowired
    16. private HCategoryMapper hCategoryMapper;
    17. @Override
    18. public HCategoryVo selectByCId(Integer cid) {
    19. return hCategoryMapper.selectByCId(cid);
    20. }
    21. }

    4、测试

    1. package com.liwen.biz.impl;
    2. import com.liwen.biz.HBookBiz;
    3. import com.liwen.vo.HBookVo;
    4. import org.junit.Test;
    5. import org.junit.runner.RunWith;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.test.context.ContextConfiguration;
    8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    9. import static org.junit.Assert.*;
    10. /**
    11. * @软件包名 com.liwen.biz.impl
    12. * @用户 liwen
    13. * @create 2023-08-27 下午10:59
    14. * @注释说明:
    15. */
    16. @RunWith(SpringJUnit4ClassRunner.class)
    17. @ContextConfiguration(locations = {"classpath:spring-context.xml"})
    18. public class HBookBizImplTest {
    19. @Autowired
    20. private HBookBiz hBookBiz;
    21. @Test
    22. public void selectByBId() {
    23. HBookVo hBookVo = this.hBookBiz.selectByBId(8);
    24. System.out.println(hBookVo);
    25. }
    26. }
    1. package com.liwen.biz.impl;
    2. import com.liwen.biz.HCategoryBiz;
    3. import com.liwen.vo.HCategoryVo;
    4. import org.junit.Test;
    5. import org.junit.runner.RunWith;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.test.context.ContextConfiguration;
    8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    9. import static org.junit.Assert.*;
    10. /**
    11. * @软件包名 com.liwen.biz.impl
    12. * @用户 liwen
    13. * @create 2023-08-27 下午11:14
    14. * @注释说明:
    15. */
    16. @RunWith(SpringJUnit4ClassRunner.class)
    17. @ContextConfiguration(locations = {"classpath:spring-context.xml"})
    18. public class HCategoryBizImplTest {
    19. @Autowired
    20. private HCategoryBiz hCategoryBiz;
    21. @Test
    22. public void selectByCId(){
    23. HCategoryVo hCategoryVo = hCategoryBiz.selectByCId(8);
    24. System.out.println(hCategoryVo);
    25. hCategoryVo.gethBooks().forEach(System.out::println);
    26. }
    27. }

  • 相关阅读:
    入门力扣自学笔记125 C++ (题目编号1656)
    第73步 时间序列建模实战:多步滚动预测 vol-1(以决策树回归为例)
    UE5 运行时生成距离场数据
    LeetCode——动态规划篇(六)
    从零开始的Hadoop学习(五)| HDFS概述、shell操作、API操作
    【Discuz】x3.5论坛修改记录
    机器学习 —— 聚类算法
    MyBatis学习:MyBatis动态代理的说明和使用要求及初步使用
    【C语言-程序编译】一行行代码究竟是怎么一步步到可执行程序的?
    MAUI+Blazor混合应用开发示例
  • 原文地址:https://blog.csdn.net/Ying_hao_kun/article/details/132586720