mybatis-plus-join的一对多查询不能分页问题

使用mybatis-plus-join的一对多查询,不能使用分页插件,如下代码:

MPJLambdaWrapper<TableA> wrapper = new MPJLambdaWrapper<TableA>()
                .selectAll(TableA.class)
                .selectCollection(TableB.class, TableADTO::getBList, b -> b
                        .collection(TableC.class, TableBDTO::getCcList, c -> c
                                .collection(TableD.class, TableCDTO::getDList, d -> d
                                        .collection(TableE.class, TableDDTO::getEList))))
                .leftJoin(TableB.class, TableB::getAid, TableA::getId)
                .leftJoin(TableC.class, TableC::getBid, TableB::getId)
                .leftJoin(TableD.class, TableD::getCid, TableC::getId)
                .leftJoin(TableE.class, TableE::getDid, TableD::getId);

        List<TableADTO> dtos = tableAMapper.selectJoinList(TableADTO.class, wrapper);

等效与

<resultMap id="xxxxxxxx" type="com.github.yulichang.join.dto.UserDTO">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <collection property="addressList" javaType="java.util.List"
        ofType="com.github.yulichang.join.entity.UserAddressDO">
        <id property="id" column="mpj_id"/>
        <result property="address" column="address"/>
        <result property="userId" column="user_id"/>
    </collection>
</resultMap>

mybaytis会在分页后再处理结果集进行collection 处理,会导致结果集数量变少
如 关联查询数据,分页查询后得到20条,但是其中一条主表数据对应两条数据,collection处理后,会变成19条数据,就导致Page.size值为20,而Page.records数量为19

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus是一个基于MyBatis的增强工具,提供了许多便捷的功能来简化开发。其中,MyBatis-Plus-join是MyBatis-Plus的一个扩展模块,用于支持关联查询。 在使用MyBatis-join的依赖。可以在项目的pom.xml文件中添加如下依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.4.3.1</version> </dependency> ``` 2. 在实体类中定义关联关系。使用MyBatis-Plus-join时,需要在实体类中定义关联关系,可以使用`@TableField`注解来指定关联字段。例如: ```java public class User { private Long id; private String name; @TableField(exist = false) private List<Role> roles; // getter and setter } public class Role { private Long id; private String roleName; // getter and setter } ``` 3. 编写Mapper接口。在Mapper接口中,可以使用`@Select`注解来编写关联查询的SQL语句。例如: ```java @Mapper public interface UserMapper extends BaseMapper<User> { @Select("SELECT u.*, r.* FROM user u LEFT JOIN user_role ur ON u.id = ur.user_id LEFT JOIN role r ON ur.role_id = r.id") @Results({ @Result(property = "id", column = "id"), @Result(property = "name", column = "name"), @Result(property = "roles", column = "id", many = @Many(select = "com.example.mapper.RoleMapper.selectByUserId")) }) List<User> selectUserWithRoles(Page<User> page); } ``` 4. 编写关联查询的子查询。在上面的例子中,使用了`@Many`注解来指定关联查询的子查询方法。例如: ```java @Mapper public interface RoleMapper extends BaseMapper<Role> { @Select("SELECT r.* FROM role r LEFT JOIN user_role ur ON r.id = ur.role_id WHERE ur.user_id = #{userId}") List<Role> selectByUserId(Long userId); } ``` 5. 调用分页查询方法。在Service层或者Controller层中,可以调用Mapper接口中定义的分页查询方法来进行查询。例如: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public IPage<User> getUserWithRoles(Page<User> page) { return userMapper.selectUserWithRoles(page); } } ``` 以上就是使用MyBatis-Plus-join进行分页查询的基本步骤。通过定义关联关系和编写关联查询的SQL语句,可以方便地进行关联查询并实现分页功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值