• Mybatis(四)——多表级联查询


    多表级联和多表关联查询不同,多表关联是指两个表通过主外键在一条SQL中完成所有数据的提取,多表级联查询是指通过一个对象获取与他关联的另外一个对象,执行SQL语句是多条。

    实体关系分析

    在这里插入图片描述

    表repository_stockout结构和数据如下图所示
    在这里插入图片描述
    表repository_stockoutinfo结构和数据如下图所示,通过字段out_code与表repository_stockout关联
    在这里插入图片描述

    OneToMany对象关联查询

    创建实体类RepositoryStockoutinfoDO.java

    @Data
    public class RepositoryStockoutinfoDO {
    
        private Integer id;
    
        private String outCode;
    
        private String materialCode;
    
        private Integer amount;
    
        private String categoryCode;
    
        private String remarks;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    RepositoryStockoutinfoMapper.xml中增加select查询,对于出库单详情按出库单号进行筛选查询

    <mapper namespace="stockoutinfo">
        <select id="selectByOutCode" parameterType="String"
                resultType="com.zl.mybatis.entity.RepositoryStockoutinfoDO">
            select * from repository_stockoutinfo where out_code = #{value}
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    创建实体类RepositoryStockoutDO.java其中添加stockinfoList属性用于保存RepositoryStockoutinfoDO的数据

    @Data
    public class RepositoryStockoutDO {
    
        private Integer id;
    
        private String outCode;
    
        private String remarks;
    
        private String tBranchCode;
    
        private String companyCode;
    
        private List<RepositoryStockoutinfoDO> stockinfoList;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    RepositoryStockoutMapper.xml中增加查询语句

    <mapper namespace="stockout">
        
        <resultMap id="rmStockout" type="com.zl.mybatis.entity.RepositoryStockoutDO">
            
            <id column="id" property="id">id>
            <result column="out_code" property="outCode">result>
            
            <collection property="stockinfoList" select="stockoutinfo.selectByOutCode"
                        column="out_code"/>
        resultMap>
        <select id="selectOneToMany" resultMap="rmStockout">
            select * from repository_stockout limit 0,10;
        select>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    测试用例

        @Test
        public void testOneToMany() throws Exception{
            SqlSession session = null;
            try {
                session = MybatisUtils.openSession();
                List<RepositoryStockoutDO> list = session.selectList("stockout.selectOneToMany");
                for (RepositoryStockoutDO s : list){
                    System.out.println(s.getOutCode()+s.getStockinfoList().size());
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                MybatisUtils.closeSession(session);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    运行结果,从表repository_stockoutinfo查询出来的出库单详情信息自动映射到RepositoryStockoutDO的stockinfoList属性
    在这里插入图片描述

    ManyToOne对象关联查询

    在多的一方要关联到一的一方,只需要持有一的一方实体就可以了,也就是在出库单详情类RepositoryStockoutinfoDO中添加RepositoryStockoutDO类型的属性。

    @Data
    public class RepositoryStockoutinfoDO {
    
        private Integer id;
    
        private String outCode;
    
        private String materialCode;
    
        private Integer amount;
    
        private String categoryCode;
    
        private String remarks;
    
        private RepositoryStockoutDO repositoryStockoutDO;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    RepositoryStockoutinfoMapper.xml中添加select查询

        <resultMap id="rmStockoutInfo" type="com.zl.mybatis.entity.RepositoryStockoutinfoDO">
            <id column="id" property="id"/>
            <result column="out_code" property="outCode"/>
            <association property="repositoryStockoutDO" select="stockout.selectByCode" column="out_code"/>
        resultMap>
        <select id="selectManyToOne" resultMap="rmStockoutInfo">
            select * from repository_stockoutinfo limit 0,10
        select>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    MyBatis的默认策略,在out_code被使用在对象关联上会导致我们原有RepositoryStockoutinfoDO中的out_code没有得到正确的赋值,为了解决这个问题我们可以手动的增加< result>完成这个字段的映射。

    RepositoryStockoutMapper.xml

        <select id="selectByCode" resultType="com.zl.mybatis.entity.RepositoryStockoutDO">
            select * from repository_stockout where out_code=#{value}
        select>
    
    • 1
    • 2
    • 3

    < association>标签用于声明从多的一方关联到一的一方,property指向多的一方中引用的一的一方实体对象, select指向关联查询时调用哪个SQL语句获取到repositoryStockoutDO,column根据查询结果中每一个stockoutinfo的out_code字段代入到SQL语句中得到与之关联的RepositoryStockoutDO实体赋值给RepositoryStockoutinfoDO的repositoryStockoutDO属性。

    测试用例

    @Test
        public void testManyToOne() throws Exception{
            SqlSession session = null;
            try {
                session = MybatisUtils.openSession();
                List<RepositoryStockoutinfoDO> list = session.selectList("stockoutinfo.selectManyToOne");
                for(RepositoryStockoutinfoDO record : list){
                    System.out.println(record.getMaterialCode()+":"+record.getRepositoryStockoutDO().getId());
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                MybatisUtils.closeSession(session);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    运行结果
    在这里插入图片描述

    通过在MyBatis中配置对象的一对多或多对一可以很大程度的降低我们日常开发的工作量,让繁琐的SQL语句由MyBatis自动帮我们执行,所有的SQL放在xml中由MyBatis自动的管理与执行降低出错风险,同时也很大程度的提高了开发效率。

  • 相关阅读:
    【自用笔记】nginx 配置多 context 及简单负载均衡
    从0开始读C++Primer|第一章 开始
    谁的孙子最多II
    易懂-SpringMvc介绍和简单搭建
    Electron打包Vue踩坑记录
    双Orin PCIe RC&EP模式互通
    Oracle 与 MySQL 的区别总结
    FX2TRT
    超纯水制备抛光树脂及填装要求
    EVPN-Vxlan隧道跨子网互访实验配置(集中式网关)
  • 原文地址:https://blog.csdn.net/xfx_1994/article/details/126140430