mybatis主子表嵌套查询,返回一个对象带有集合属性

前段时间在工作中遇到需要一对多关联查询的情况,百度了好久遇到各种坑,所以在此记录一下,话不多说直接开始

首先,先建几个测试类,主子表通过grade_id关联,具体属性注释我就省略不写了,大家能看懂

  • 主表
	@Data
	@TableName("grade")
	public class Grade implements Serializable {
	    private Long id;
	    private String name;
	    private String address;
		}
  • 子表1
    @Data
    @TableName("teacher")
    public class Teacher implements Serializable {
        private Long id;
        private String name;
        private Integer age;
        private String categroy;
        private Integer level;
        private Long gradeId;		// 关联主表id
    	}
    
  • 子表2
    @Data
    @TableName("student")
    public class Student implements Serializable {
        private Long id;
        private String name;
        private Integer age;
        private String address;
        private Long gradeId;		// 关联主表id
    }
    
  • 表结构和实体类一样我就不放了,没啥特别的

其次,创建一个DTO用来接收查询出来的结果,这个大家都懂,不解释

@Data
public class GradeDTO implements Serializable {
    private Long id;
    private String name;
    private String address;
    private List<Teacher> teacherList;
    private List<Student> studentList;
}

重点,配置mapper.xml文件

  • 首先需要配置resultMap用来接收返回结果,这个大家百度得到,关联集合就是用collection,如下
        <resultMap type="com.kasi.mybatis_plus.model.GradeDTO" id="gradeMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="address" property="address"/>
        <collection property="teacherList" ofType="com.kasi.mybatis_plus.model.Teacher" >
            <id column="t_id" property="id"/>
            <result column="t_name" property="name"/>
            <result column="t_age" property="age"/>
            <result column="t_categroy" property="categroy"/>
            <result column="t_level" property="level"/>
        </collection>
        <collection property="studentList" ofType="com.kasi.mybatis_plus.model.Student">
            <id column="s_id" property="id"/>
            <result column="s_name" property="name"/>
            <result column="s_age" property="age"/>
            <result column="s_address" property="address"/>
        </collection>
    </resultMap>
    
    常用的属性就不说了,特别要注意的是ofType这个要指向各自表的实体类,然后子表的column属性不要和主表重名,一旦重名就会出问题,不知道是不是我配置的问题,希望有大佬指教,反正不重名就可以
  • 接下来就是查询语句了,很简单,只要别名和上面resultMap对的上就ok
        <select id="get" resultMap="gradeMap">
        SELECT
            a.id,
            a.name,
            a.address,
            b.id as t_id,
            b.name as t_name,
            b.age as t_age,
            b.categroy as t_categroy,
            b.level as t_level,
            c.id as s_id,
            c.name as s_name,
            c.age as s_age,
            c.address as s_address
        FROM grade a
        LEFT JOIN teacher b ON a.id=b.grade_id
        LEFT JOIN student c ON a.id=c.grade_id
        WHERE a.id=#{id}
    </select>
    

    最后,看看查询结果,其实很简单的,网上教程也很多,但是别名那个地方很多人都没有强调导致犯了很多错

    ps:查询集合也是一样的,不说了
    • 数据表查询
      在这里插入图片描述
    • 实际查询结果
      {
      "id": 1,
      "name": "3年1班",
      "address": "三楼",
      "teacherList": [
          {
              "id": 1,
              "name": "宋元桥",
              "age": 50,
              "categroy": "武当",
              "level": 1,
              "gradeId": null
          },
          {
              "id": 2,
              "name": "俞莲舟",
              "age": 43,
              "categroy": "武当",
              "level": 2,
              "gradeId": null
          }
      ],
      "studentList": [
          {
              "id": 1,
              "name": "金毛狮王",
              "age": 47,
              "address": "冰火岛",
              "gradeId": null
          },
          {
              "id": 2,
              "name": "紫衫龙王",
              "age": 39,
              "address": "灵蛇岛",
              "gradeId": null
          }
      ]
      
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章