【mybatis-plus】mybatis-plus多表分页查询

在我们写业务逻辑的时候,自己写分页相对来说是一件非常麻烦的事情,因为我们要写很多逻辑语句来进行分页查询,mybatis-plus为我们提供了很好的分页方法,使得我们在进行分页时只需引用其中的方法便可以实现分页查询,下面我们一起来看一看。

mybatis-plus分页配置

我这里用的是springboot的分页配置,如果是ssm的分页配置,也可以参考mybatis-plus官网分页插件进行配置。

@Configuration
@MapperScan("填要用到的mapper类的路径")
public class MybatisPlusConfig {
    
    

    @Bean
    public PaginationInterceptor paginationInterceptor() {
    
    
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}

其实就是将官网的分页配置黏贴下来,改一下包路径即可。

自定义分页类

@Data
@ApiModel("分页")
public class PageVo {
    
    

    /** 当前页 */
    @ApiModelProperty(value = "当前页")
    private Integer currentPage;

    /** 当前页显示数据的条数 */
    @ApiModelProperty(value = "当前页显示数据的条数")
    private Integer pageSize;

   

    public Integer getCurrentPage() {
    
    

        return currentPage = "".equals(currentPage) || currentPage ==null ? 0 : currentPage;
    }

    public Integer getPageSize() {
    
    
        return pageSize = "".equals(pageSize) || pageSize ==null ? 20 : pageSize;
    }
}

定义接收参数的实体类dto

需求为通过传过来的用户id,来查询用户写的话题,所以我们的实体类中要有用户的id,然后我们是采取分页查询,所以extends pageVo

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class TopicUserDto extends PageVo implements Serializable {
    
    
    private Long createBy;
}

定义返回的实体类PageResultDto

我们将查询出来的列表以及总页数和总条数封装到一个PageResultDto实体类中,前台将接收这个实体类对象。

@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class PageResultDto<T> {
    
    
    /** 总条数 */
    private Long total;
    /** 总页数 */
    private Long totalPage;
    /** 当前页数据 */
    private List<T> date;

    public PageResultDto(Long total, List<T> items) {
    
    
        this.total = total;
        this.date = items;
    }

}

分页操作实现

业务层逻辑

我们来看业务层

@Override
    public PageResultDto<TopicExtendsDto> getMyPost(TopicUserDto topicUserDto) {
    
    
		//定义一个Page对象,传过去当前页和当前页显示的条数
        Page<TopicUserDto> page=new Page<>(topicUserDto.getCurrentPage(), topicUserDto.getPageSize());
        //定义QueryWrapper对象,来对查询条件进行限制
        QueryWrapper<TopicUserDto> queryWrapper=new QueryWrapper<>();
        //查询有关用户的id,然后进行groupby
        queryWrapper.eq("t.`create_by`", topicUserDto.getCreateBy()).groupBy("t.`id`");
        //调用mapper中数据库的sql语句,并返回结果Ipage对象
        IPage<TopicExtendsDto> topicByUserId = topicMapper.getTopicByUserId(page, queryWrapper);
        //定义PageResultDto,封装数据给前台 
        PageResultDto<TopicExtendsDto> pageResultDto = new PageResultDto<>();
        //将查询的数据,总条数,总页数返回
        pageResultDto.setDate(topicByUserId.getRecords()).
                setTotal(topicByUserId.getTotal()).
                setTotalPage(topicByUserId.getPages());
        return  pageResultDto;
    }

mapper层操作

 @Select("SELECT  t.`id`,t.`title`,t.`create_time`,COUNT( DISTINCT tl.`id`)  likeNum,COUNT(DISTINCT c.`id`) commentNum FROM topic t\n" +
            "LEFT JOIN topic_like tl ON t.`id`=tl.`topic_id`\n" +
            "LEFT JOIN `comment` c ON t.`id`=c.`topic_id`\n" +
            "${ew.customSqlSegment}")
    IPage<TopicExtendsDto> getTopicByUserId(Page page, @Param(Constants.WRAPPER) QueryWrapper<TopicUserDto> queryWrapper);

有两个要注意的地方:
(1)传过来的Page对象,我们发现在sql语句中并没有limit,但是却能够进行分页,这也是mybatis-plus中的,如果有感兴趣的想要了解其中原理,大家可以自己查询,因为我不知道…
(2)${ew.customSqlSegment}这个是对QueryWrapper进行解析,我们在用QueryWrapper进行条件查询后,相当于一个条件where的查询,那么${ew.customSqlSegment}是对条件进行解析,直接解析成 where WHERE t.create_by=1111 GROUP BY t.id ;

测试

我们可以测试一下,写一个测试方法

@Test
    public  void testGetMyPost(){
    
    
        TopicUserDto topicUserDto =new TopicUserDto();
        topicUserDto.setCreateBy(1111L);
        topicUserDto.setCurrentPage(1);
        topicUserDto.setPageSize(5);
        topicService.getMyPost(topicUserDto);

    }

在这里插入图片描述
在这里插入图片描述

可以发现我们设置当前页为第一页,显示条数为5,结果里面也是5条,而且查询出总条数为8条,但是分页时只要5条。
测试成功!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Black_Customer/article/details/108560206