Mybatis-Plus 多表联查分页

作者: 殷天文 | 来源:发表于2018-06-05 23:53 被阅读433次

    上一节,简单讲述了 Mybatis-Plus 搭建与使用入门,这一节,简单讲一下如何使用 MP 实现多表分页。

    分析

    使用的工程,依旧是 spring-boot,关于分页,官网给出了一个单表的 demo,其实多表分页实现原理相同,都是通过 mybatis 的拦截器
    (拦截器做了什么?他会在你的 sql 执行之前,为你做一些事情,例如分页,我们使用了 MP 不用关心 limit,拦截器为我们拼接。我们也不用关心总条数,拦截器获取到我们 sql 后,拼接 select count(*) 为我们查询总条数,添加到参数对象中)。

    实现

    1. 配置拦截器
    @EnableTransactionManagement
    @Configuration
    @MapperScan("com.web.member.mapper")
    public class MybatisPlusConfig {
        /**
         * mybatis-plus SQL执行效率插件【生产环境可以关闭】
         */
        @Bean
        public PerformanceInterceptor performanceInterceptor() {
            return new PerformanceInterceptor();
        }
    
        /*
         * 分页插件,自动识别数据库类型 多租户,请参考官网【插件扩展】
         */
        @Bean
        public PaginationInterceptor paginationInterceptor() {
            return new PaginationInterceptor();
        }
    }
    
    2. mapper 接口以及 xml
    /**
     * <p>
     * 用户表 Mapper 接口
     * </p>
     *
     * @author 殷天文
     * @since 2018-06-01
     */
    public interface UserMapper extends BaseMapper<User> {
    
        List<UserListModel> selectUserListPage(Pagination page ,@Param("user") UserListBean user);
        
    }
    

    这里要注意的是,这个 Pagination page 是必须要有的,否则 MP 无法为你实现分页。

        <select id="selectUserListPage" resultType="com.web.member.model.UserListModel">
            SELECT
                *
            FROM
                ftms_user u
            LEFT JOIN ftms_user_level l ON u.level_id = l.id
            WHERE 1=1
                <if test="user.nickname != null">
                    and u.nickname like "%"#{user.nickname}"%" 
                </if>
        </select>
    
    3. service 实现
    import com.web.member.beans.admin.UserListBean;
    import com.web.member.entity.User;
    import com.web.member.mapper.UserMapper;
    import com.web.member.model.UserListModel;
    import com.web.member.service.UserService;
    import com.baomidou.mybatisplus.plugins.Page;
    import com.baomidou.mybatisplus.service.impl.ServiceImpl;
    
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    /**
     * <p>
     * 用户表 服务实现类
     * </p>
     *
     * @author 殷天文
     * @since 2018-06-01
     */
    @Service
    public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    
        @Transactional(readOnly=true)
        @Override
        public Page<UserListModel> selectUserListPage(UserListBean user) {
            Page<UserListModel> page = new Page<>(user.getCurr(), user.getNums());// 当前页,总条数 构造 page 对象
            return page.setRecords(this.baseMapper.selectUserListPage(page, user));
        }
        
    }
    

    最后将结果集 set 到 page 对象中,page 对象的 json 结构如下

    {
        "total": 48,//总记录
        "size": 10,//每页显示多少条
        "current": 1,//当前页
        "records": [//结果集 数组
            {...},
            {...},
            {...},
             ...
        ],
        "pages": 5 // 总页数
    }
    

    相关文章

      网友评论

      • d858a72c93f8:作者您好:
        使用您的方法之后,多对多查询仍然有问题:比如:用户关联部门和车间等等,多个关联时,采用left join连接,但是分页的效果并不对,查出来的结果总数是不对的,会多出来,请问这种情况应该怎么解决?
      • 行走的机器:明显是错的, 一对多这个分出来肯定是错的
        殷天文:@行走的机器 哪里错了?一对多肯定要单独查询count啊 🙂🙂🙂
      • c7a0af1a4782:你好,可以把UserListBean的代码贴一下吗
        殷天文:@平凡之路_dbef 就是一些user表的属性,还有 当前第几页,每页多少条

      本文标题:Mybatis-Plus 多表联查分页

      本文链接:https://www.haomeiwen.com/subject/pncvsftx.html