java
主页 > 软件编程 > java >

springboot使用mybatis一对多的关联查询问题记录

2022-01-26 | 秩名 | 点击:

springboot使用mybatis一对多的关联查询

由于刚开始写java不久,对sql语句的熟悉度还是不够熟练,虽然现在使用的mybatisPlus比较多,但我始终觉得sql不能忘也不能不用,刚好最近有个需求需要做到关联的查询,时间也算充足,所以用sql来写,于是踩了很久坑,终于跳出来了,小小记录一下。

一对多

# 我这里是一对多查询,一张主表两张副表,最后还要有一张VO表(就是做关联映射用的),主表和副表的实体我就不贴了,以下是VO实体

在这里插入图片描述

这是我的controller

1

2

3

4

5

6

7

8

9

@RequestMapping(value = "/queryChartAll", method = RequestMethod.GET)

public R queryChartAll(){

    List statementEnteringVO = statementEnteringMapper.queruMapperPage();

    if(statementEnteringVO != null){

        return R.data(statementEnteringVO);

    }else{

        return R.fail(ResultCode.ERROR);

    }

}

mapper

1

2

3

public interface StatementEnteringMapper extends BaseMapper {

    List queruMapperPage();

}

mapper.xml(注意了,最难受也是最坑的)

为了展示方便,我贴少点的字段,不然老长的代码看球不明白,再注一下:一对多使用collection,一对一使用association,文章主讲一对多的。所以也不对association做解释了,感兴趣的朋友可以自己去了解一下,用法是一样的。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

xml version="1.0" encoding="UTF-8"?>

 

<mapper namespace="com.ld.ldstat.mapper.StatementEnteringMapper">

    <resultMap id="queryChartAllTwo" type="com.ld.ldstat.vo.StatementEnteringVO">

        <id property="id" jdbcType="BIGINT" column="sid">id>

        <result property="theDate" jdbcType="DATE" column="the_date">result>

        <result property="deptName" jdbcType="VARCHAR" column="dept_name">result>

        <result property="deptId" jdbcType="BIGINT" column="dept_id">result>

         

        

        <collection property="cashCar" javaType="java.util.List" ofType="com.ld.ldstat.entity.CashCar">

            <id property="id" jdbcType="BIGINT" column="cid">id>

            <result property="parentId" jdbcType="BIGINT" column="c_parent_id"/>

            <result property="projectName" jdbcType="VARCHAR" column="c_project_name"/>

            <result property="ninetyWithin" jdbcType="INTEGER" column="ninety_within"/>

            <result property="ninetyExcept" jdbcType="INTEGER" column="ninety_except"/>

            <result property="cashCar" jdbcType="INTEGER" column="cash_car"/>

        collection>

         

        

        <collection property="subtotalAll" ofType="com.ld.ldstat.entity.SubtotalAll">

            <id property="id" jdbcType="BIGINT" column="aid">id>

            <result property="parentId" jdbcType="BIGINT" column="a_parent_id"/>

            <result property="projectName" jdbcType="VARCHAR" column="a_project_name"/>

            <result property="subtotalType" jdbcType="INTEGER" column="subtotal_type"/>

            <result property="task" jdbcType="INTEGER" column="task"/>

            <result property="theDay" jdbcType="INTEGER" column="the_day"/>

        collection>

    resultMap>

    <select id="queruMapperPage" resultMap="queryChartAllTwo">

        SELECT

        se.id sid,se.the_date,se.dept_name,

        ca.id cid,ca.project_name c_project_name,ca.parent_id c_parent_id,ca.ninety_Within,ca.ninety_except,ca.cash_car,

        sa.id aid,sa.project_name a_project_name,sa.parent_id a_parent_id,sa.task,sa.the_day

        FROM

        statement_entering se

        LEFT JOIN cash_car ca on se.id = ca.parent_id

        LEFT JOIN subtotal_all sa on se.id = sa.parent_id

         

        

    select>

mapper>

以下是需要注意的点(我就是在这里踩了好久的坑)

以下sql用的是左连接语句LEFT JOIN,具体的sql语句我也不解释了,因为要了解的太多了,【尴尬】

这里的这些字段必须要使用别名,为啥?因为几张表的字段相同所以会出现覆盖的问题,比如我副表1和副表2同时存在一个相同字段project_name,如果不给其声明别名,副表2该字段的数据会被副表1的该字段覆盖掉,原理我也解释不清楚,哈哈!!

在这里插入图片描述

对应的映射字段要使用别名,上图

在这里插入图片描述

线划的丑了些,将就吧!

看下最终获取到的数据结构

在这里插入图片描述

完美,理想中的效果。。。

至于最终效果图为啥这么多null,不用怀疑,这些是我没有写对应的映射

原文链接:https://blog.csdn.net/qq_31676725/article/details/122674040
相关文章
最新更新