MyBatis-plus:IPage分页

1、引入MyBatis-plus依赖

        
        
            com.baomidou
            mybatis-plus-extension
            3.4.2
            compile
        

2、创建配置类MybatisPlusConfig实现分页拦截

package com.heima.wemedia.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {


    /**
     * IPage的分页使用的是拦截器,属于物理分页,好处就是处理大量数据时,查询速度快。
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //向Mybatis过滤器链中添加分页拦截器
        interceptor.addInnerInterceptor(new
                PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

}

 3、业务代码

package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.wemedia.model.dto.WmMaterialDto;
import com.heima.model.common.wemedia.model.po.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 

* 自媒体图文素材信息表 服务实现类 *

* * @author itcast * @since 2023-08-05 */ @Service @Slf4j public class WmMaterialServiceImpl extends ServiceImpl implements WmMaterialService { /*获取素材列表*/ @Override public ResponseResult list(WmMaterialDto dto) { //检查参数 dto.checkParam(); //构造分页查询 IPage pageInfo = new Page(dto.getPage(),dto.getSize());//分页参数 //条件查询器 LambdaQueryWrapper lwq = new LambdaQueryWrapper<>(); //判断是否收藏 if(dto.getIsCollection() != null && dto.getIsCollection() == '1'){ lwq.eq(WmMaterial::getCollection,dto.getIsCollection()); } //按用户查询 lwq.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId()); //按时间排序 lwq.orderByDesc(WmMaterial::getCreatedTime); //执行iservice的page查询 pageInfo = page(pageInfo,lwq); //结果返回 ResponseResult responseResult = new PageResponseResult( dto.getPage(), dto.getSize(), (int)pageInfo.getTotal()); responseResult.setData(pageInfo.getRecords()); return responseResult; } }

你可能感兴趣的:(mybatis)