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

最近项目里要返回一个下面这种json的数据,用的mybatis-plus框架

sql查数据出来是这种的

要组装成上面的json

首先想到的是用一对多关联查询,一条sql搞定,然后用collection自己组装就可以

 <resultMap id="CheckItemResponse" type="com.xxx.response.subject.CheckItemDtoResponse">
        <id column="id" property="id" />
        <result column="value" property="deviceTypeCodeName" />
        <result column="check_item" property="checkItem" />
        <result column="subject_type" property="subjectType" />
        <result column="subject" property="subject" />
        <collection property="checkItemInfoDtoResponses" ofType=com.xxx.CheckItemInfoDtoResponse">
            <result property="no" column="no"/>
            <result property="option" column="option"/>
        </collection>
    </resultMap>

用上面的collection后数据一切都OK了,后面前端调接口的人找到我说分页是不是有问题??然后我自己测试了一下,分页真的有问题!!!!

{
    "msg": "success",
    "code": 200,
    "data": {
        "total": 4,
        "size": 10,
        "pages": 1,
        "current": 1,
        "records": [
            {
                "id": 4,
                "deviceTypeCode": null,
                "deviceTypeCodeName": null,
                "checkItem": null,
                "subjectType": null,
                "subject": "标题名称",
                "checkItemInfoDtoResponses": [
                    {
                        "id": null,
                        "itemId": null,
                        "no": "a",
                        "option": "正常"
                    }
                ]
            },
            {
                "id": 5,
                "deviceTypeCode": null,
                "deviceTypeCodeName": null,
                "checkItem": null,
                "subjectType": null,
                "subject": "检查外观",
                "checkItemInfoDtoResponses": [
                    {
                        "id": null,
                        "itemId": null,
                        "no": "a",
                        "option": "正常"
                    },
                    {
                        "id": null,
                        "itemId": null,
                        "no": "b",
                        "option": "异常"
                    },
                    {
                        "id": null,
                        "itemId": null,
                        "no": "c",
                        "option": "瞎吃"
                    }
                ]
            }
        ]
    }
}

total应该是2条,结果查出来是4条,后面网上搜索了一下,确实用上面collection这么查会导致分页不正确

所以只好改一下collection就可以了,分成两次查就可以了

<collection property="checkItemInfoDtoResponses" column="id" select="getCheckItemInfo">
            <result property="no" column="no"/>
            <result property="option" column="option"/>
        </collection>

column是关联的id,意思就是根据哪个字段来查询关联表的字段

<select id="getCheckItemInfo" resultType="com.xxx.CheckItemInfoDtoResponse">
        SELECT
        b.no,
        b.option
        FROM
        check_item_info b
        WHERE b.item_id=#{id}
        order by b.sort asc
    </select>

使用上面的collection 分页就可以正常了

会出现N+1问题,所有还是谨慎使用

MyBatis-Plus是一款MyBatis的增强工具,它提供了很多实用的功能,比如一对多关联查询。在MyBatis-Plus一对多关联查询可以通过使用@TableName注解和@TableField注解来实现。 假设我们有两张表,一张是学生表,另一张是课程表,一个学生可以选多门课程,那么我们就可以用一对多关联查询来查询某个学生选的所有课程。 首先,在学生表定义一个属性List<Course> courses,并使用@TableField注解将该属性与课程表的外键关联起来: ``` public class Student { @TableId private Long id; private String name; @TableField(exist = false) private List<Course> courses; } ``` 然后,在课程表定义一个属性Long studentId,并使用@TableField注解将该属性与学生表的主键关联起来: ``` public class Course { @TableId private Long id; private String name; @TableField("student_id") private Long studentId; } ``` 最后,我们使用MyBatis-Plus提供的wrapper类进行关联查询: ``` QueryWrapper<Student> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("id", studentId); List<Student> students = studentMapper.selectList(queryWrapper); for (Student student : students) { QueryWrapper<Course> courseQueryWrapper = new QueryWrapper<>(); courseQueryWrapper.eq("student_id", student.getId()); List<Course> courses = courseMapper.selectList(courseQueryWrapper); student.setCourses(courses); } ``` 以上就是MyBatis-Plus实现一对多关联查询的方法。如果您还有其他问题或需要进一步的帮助,请随时提出。
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值