1.导入依赖
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.1.1version>
dependency>
2.分页插件配置
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
3.service层引用插件
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
@Override
public ResponseResult findByNameAndPage(ChannelDto dto) {
if (dto == null) {
return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
}
dto.checkParam();
Page page = new Page(dto.getPage(), dto.getSize());
LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
String adChannelName = dto.getName();
if(StringUtils.isNotBlank(adChannelName)){
wrapper.like(AdChannel::getName,adChannelName);
}
IPage result = page(page, wrapper);
PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) result.getTotal());
responseResult.setData(result.getRecords());
return responseResult;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- Mybatis-plus的分页插件----IPage:
<1>IPage 需要传入IPage的实现类Page对象,该对象实现了IPage,IPage内部原理是基于拦截器,拦截的是方法以及方法中的参数,会判断是否是查询操作。如果是查询操作,才会进入分页的逻辑处理。
<2>进入分页的逻辑处理后,拦截器会通过反射获取该方法的参数进行判断是否存在IPage对象的实现类。如果不存在就不进行分页,存在则将该参数赋值给IPage对象,然后进行拼接sql处理完成分页操作。
<3>limit (curPage-1)*pageSize,pageSize;curPage表示从第几条数据开始查(默认索引是0,如果写1,从第pageSize条开始查),pageSize表示这页显示几条数据
4.sql打印yml配置
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml
# 设置别名包扫描路径,通过该属性可以给包中的类注册别名
type-aliases-package: com.heima.model.user.pojos
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl