springcloud-mybatisplus分页

按照官网说明

IPage<UserVo> selectPageVo(IPage<?> page, Integer state);
// or (class MyPage extends Ipage<UserVo>{ private Integer state; })
MyPage selectPageVo(MyPage page);
// or
List<UserVo> selectPageVo(IPage<UserVo> page, Integer state);
<select id="selectPageVo" resultType="xxx.xxx.xxx.UserVo">
    SELECT id,name FROM user WHERE state=#{state}
</select>

如果返回类型是 IPage 则入参的 IPage 不能为null,因为 返回的IPage == 入参的IPage; 如果想临时不分页,可以在初始化IPage时size参数传 <0 的值;
如果返回类型是 List 则入参的 IPage 可以为 null(为 null 则不分页),但需要你手动 入参的IPage.setRecords(返回的 List);
如果 xml 需要从 page 里取值,需要 page.属性 获取

 具体使用方法,在mybatisplus能正常使用的情况下,不需要引包,加入mybatisplus分页插件PaginationInnerInterceptor

@Configuration
public class MyBatisPlusConfig {

    /**
     * 分页插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

然后使用

public IPage<Product> selectByPage(ProductDTO productDTO) {
        IPage<Product> page = new Page<>();
        page.setCurrent(productDTO.getNowPage());
        page.setSize(productDTO.getPageSize());
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(productDTO.getName())) {
            queryWrapper.like("name", productDTO.getName());
        }
        page = productMapper.selectPage(page, queryWrapper);
        return page;
    }

结果:

{
    "records": [
        {
            "id": 1,
            "name": "苹果11",
            "price": 10000,
            "number": 23123
        },
        {
            "id": 2,
            "name": "苹果2",
            "price": 10000,
            "number": 111
        },
        {
            "id": 3,
            "name": "苹果3",
            "price": 30000,
            "number": 222
        }
    ],
    "total": 5,
    "size": 3,
    "current": 1,
    "orders": [],
    "optimizeCountSql": true,
    "searchCount": true,
    "countId": null,
    "maxLimit": null,
    "pages": 2
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值