关于mybatis中collection一对多关联查询分页出错问题总结

在使用mybatis作为数据分页查询的时候,如果使用了collection进行一对多关联的时候有可能出现分页不正确的问题:

以下是两个实体类,老师类 和班级类,一个老师可以对应多个班级(暂假设为为一对多的关系)

1.老师类

import java.util.List;

/*老师*/
@Data
@Getter
@Setter
public class Teacher {
       private Long id;
       private String name;
       private Integer age;
       private List<Group> groupList;
}

数据库数据:

1    张小花老师    24
2    李戴莫老师    26
3    王敖明老师    55
4    周小玲老师    45
5    周涛老师    33



2.班级类

/*班级*/
@Data
@Getter
@Setter
public class Group {
       private Long id;
       private String name;
       private Integer number;
       //private List student;
}

数据库数据:

3    花桥河高一(4)班    44    2
4    花桥河高一(5)班    38    3
5    花桥河高一(6)班    38    4
6    花桥河高一(7)班    38    1
7    花桥河高一(8)班    38    1
8    花桥河高一(8)班    38    5


TeacherMapper.xml类

<mapper namespace="com.oasis.test.mapper.TeacherMapper">
    <resultMap id="teacherResultMap" type="com.oasis.test.entity.Teacher">
        <id column="tid" property="id"/>
        <result column="teacher_name" property="name"/>
        <result column="age" property="age"/>
        <collection property="groupList" ofType="com.oasis.test.entity.Group" column="id">
            <id column="gid" property="id"/>
            <result column="group_name" property="name"/>
            <result column="number" property="number"/>
        collection>
    resultMap>
    <select id="getTeacherList" parameterType="java.lang.Long"
            resultMap="teacherResultMap">
        SELECT
        t.ID tid, t.TEACHER_NAME,AGE,g.GROUP_NAME,NUMBER,g.id gid
        FROM
        t_teacher t LEFT JOIN t_group g ON t.id=g.teacher_id limit 0,5
    select>

查询结果:

"data": [

    {

      "id": 2,

      "name": "李戴莫老师",

      "age": 26,

      "groupList": [

        {

          "id": 3,

          "name": "花桥河高一(4)",

          "number": 44

        }

      ]

    },

    {

      "id": 3,

      "name": "王敖明老师",

      "age": 55,

      "groupList": [

        {

          "id": 4,

          "name": "花桥河高一(5)",

          "number": 38

        }

      ]

    },

    {

      "id": 4,

      "name": "周小玲老师",

      "age": 45,

      "groupList": [

        {

          "id": 5,

          "name": "花桥河高一(6)",

          "number": 38

        }

      ]

    },

    {

      "id": 1,

      "name": "张小花老师",

      "age": 24,

      "groupList": [

        {

          "id": 6,

          "name": "花桥河高一(7)",

          "number": 38

        },

        {

          "id": 7,

          "name": "花桥河高一(8)",

          "number": 38

        }

      ]

    }

  ]



从查询结果看只有4条数据,但是我查询的是5条数据(见mapper绿色部分),且数据库数据条数正好有五条,出现这个问题的主要原因是collection采用以上写法,他会将collection中查询到的条数作为分页约束(见以上金色部分)


正确写法:实体类和数据库数据不变,在collection中添加select关联查询:

1.修改TeacherMapper的collection:

<mapper namespace="com.oasis.test.mapper.TeacherMapper">
    <resultMap id="teacherResultMap" type="com.oasis.test.entity.Teacher">
        <id column="id" property="id"/>
        <result column="teacher_name" property="name"/>
        <result column="age" property="age"/>
        <collection property="groupList" ofType="com.oasis.test.entity.Group" column="id"
                    select="com.oasis.test.mapper.GroupMapper.getGroupListByTeacherId">
        collection>
    resultMap>
    <select id="getTeacherList" parameterType="java.lang.Long"
            resultMap="teacherResultMap">
        SELECT
        ID,TEACHER_NAME,AGE
        FROM
        t_teacher limit 0,5
    select>

2.添加GroupMapper.xml类

<mapper namespace="com.oasis.test.mapper.GroupMapper">
    <resultMap id="groupResultMap" type="com.oasis.test.entity.Group">
        <id column="id" property="id"/>
        <result column="group_name" property="name"/>
        <result column="number" property="number"/>
    resultMap>
    <select id="getGroupListByTeacherId" parameterType="java.lang.Long"
            resultMap="groupResultMap">
        SELECT
       ID,GROUP_NAME,NUMBER
        FROM
        t_group WHERE teacher_id=#{id}
    select>

mapper>

3.执行命令得到查询结果

"data": [
    {
      "id": 1,
      "name": "张小花老师",
      "age": 24,
      "groupList": [
        {
          "id": 6,
          "name": "花桥河高一(7)班",
          "number": 38
        },
        {
          "id": 7,
          "name": "花桥河高一(8)班",
          "number": 38
        }
      ]
    },
    {
      "id": 2,
      "name": "李戴莫老师",
      "age": 26,
      "groupList": [
        {
          "id": 3,
          "name": "花桥河高一(4)班",
          "number": 44
        }
      ]
    },
    {
      "id": 3,
      "name": "王敖明老师",
      "age": 55,
      "groupList": [
        {
          "id": 4,
          "name": "花桥河高一(5)班",
          "number": 38
        }
      ]
    },
    {
      "id": 4,
      "name": "周小玲老师",
      "age": 45,
      "groupList": [
        {
          "id": 5,
          "name": "花桥河高一(6)班",
          "number": 38
        }
      ]
    },
    {
      "id": 5,
      "name": "周涛老师",
      "age": 33,
      "groupList": [
        {
          "id": 8,
          "name": "花桥河高一(8)班",
          "number": 38
        }
      ]
    }
  ]

总共5条数据,符合要求



以上是开发中的一些总结,记录下来,如有错误之处请指出





你可能感兴趣的:(关于mybatis中collection一对多关联查询分页出错问题总结)