Mybatis-plus 内置分页插件, 并支持多种数据库
官网 : 分页插件 | MyBatis-Plus (baomidou.com)
通过 @MapperScan
指定 mapper接口的路径
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.yuan.mapper")
public class MybatisPlusConfig {
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
//interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
return interceptor;
}
}
准备封装用于分页查询的工具类 BrandInfoPage
import lombok.Data;
@Data
public class BrandInfoPage {
private Integer pageNo = 1;
private Integer pageSize = 10;
}
服务层查询
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.yuan.entity.BrandInfo;
import com.yuan.mapper.BrandInfoMapper;
import com.yuan.page.BrandInfoPage;
import com.yuan.service.BrandInfoService;
import org.springframework.stereotype.Service;
@Service
public class BrandInfoServiceImpl extends ServiceImpl<BrandInfoMapper, BrandInfo>
implements BrandInfoService{
@Override
public IPage<BrandInfo> listForPage(BrandInfoPage page) {
IPage<BrandInfo> pageInfo = new Page<>();
// 设置页号
pageInfo.setCurrent(page.getPageNo());
// 设置页大小
pageInfo.setSize(page.getPageSize());
return baseMapper.selectPage(pageInfo, null);
}
}
baseMapper.selectPage(pageInfo, null);
第二个参数为 QueryWrapper 用于条件查询
分析数据
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuan.entity.BrandInfo;
import com.yuan.page.BrandInfoPage;
import com.yuan.service.BrandInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BrandInfoController {
@Autowired
private BrandInfoService brandInfoService;
@RequestMapping("/brandInfo/query")
public IPage<BrandInfo> query(BrandInfoPage page){
System.out.println("page = " + page);
IPage<BrandInfo> list = brandInfoService.listForPage(page);
System.out.println("list = " + list);
System.out.println("分页数据集合 = " + list.getRecords());
System.out.println("当前页号 = " + list.getCurrent());
System.out.println("每页最多多少条 = " + list.getSize());
System.out.println("总记录数 = " + list.getTotal());
System.out.println("总页数 = " + list.getPages());
}
}
import lombok.Data;
@Data
public class BrandInfoPage {
private Integer pageNo = 1;
private Integer pageSize = 10;
/**
* 中文名 模糊
*/
private String cnName;
/**
* 英文名 精确
*/
private String enName;
}
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.yuan.entity.BrandInfo;
import com.yuan.mapper.BrandInfoMapper;
import com.yuan.page.BrandInfoPage;
import com.yuan.service.BrandInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@Service
public class BrandInfoServiceImpl extends ServiceImpl<BrandInfoMapper, BrandInfo>
implements BrandInfoService{
@Override
public IPage<BrandInfo> listForPage(BrandInfoPage page) {
IPage<BrandInfo> pageInfo = new Page<>();
// 设置页号
pageInfo.setCurrent(page.getPageNo());
// 设置页大小
pageInfo.setSize(page.getPageSize());
LambdaQueryWrapper<BrandInfo> queryWrapper = new LambdaQueryWrapper<BrandInfo>()
.like(page.getCnName() != null, BrandInfo::getBrandCnName, page.getCnName())
.eq( !StringUtils.isEmpty(page.getEnName()), BrandInfo::getBrandEnName, page.getEnName())
.orderByAsc(BrandInfo::getBrandNum)
;
return baseMapper.selectPage(pageInfo, queryWrapper);
}
}