天天看点

Mybatisplus实现在不分页时进行排序操作以及分页接口全量查询

优化分页插件实现在不分页时进行排序操作

原生mybatisplus分页与排序是绑定的,mpp优化了分页插件,使用MppPaginationInterceptor插件

在不分页的情况下支持排序操作

page参数size设置为-1可实现不分页取全量数据,同时设置OrderItem可以实现排序

从中央库引入jar

<dependency>
        <groupId>com.github.jeffreyning</groupId>
        <artifactId>mybatisplus-plus</artifactId>
        <version>1.3.0-RELEASE</version>
    </dependency>
           

使用MppPaginationInterceptor插件

@Bean
    public PaginationInterceptor paginationInterceptor() {
        return new MppPaginationInterceptor();
    }

           

mapper中按照一般分页接口定义,同时支持返回值为list或page对象的写法

@Mapper
public interface TestMapper extends BaseMapper<TestEntity> {
    public List<JoinEntity> queryUseRM(Page page);
}
           

page参数设置size=-1为全量查询,size>0时正常分页,设置OrderItem进行无论是否分页都实现排序

public void testOrder(){
        Page page=new Page();
        page.setSize(-1);
        page.addOrder(new OrderItem().setColumn("test.id").setAsc(true));
        page.addOrder(new OrderItem().setColumn("test2.some2").setAsc(true));
        List rp=testMapper.queryUseRM(page);
    }
           

demo下载

mybatisplus-plus 1.3.0 示例工程下载地址

链接:https://pan.baidu.com/s/1wyMBHS4ke_oLEEYOld6-jQ

扫描订阅公众号,回复"plus"获取下载密码

Mybatisplus实现在不分页时进行排序操作以及分页接口全量查询