MybatisPlus分页查询插件
公共配置类
@Configuration
public class MybatisPlusPageConfig {
/* 旧版本配置
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}*/
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,
* 需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
分页查询
// controller
@ApiOperation("分页查询优惠券")
@GetMapping("page_coupon")
public JsonData pageCouponList(
@ApiParam(value = "当前页") @RequestParam(value = "page", defaultValue = "1") int page,
@ApiParam(value = "每页显示多少条") @RequestParam(value = "size", defaultValue = "10") int size
) {
Map<String, Object> pageMap = couponService.pageCouponActivity(page, size);
return JsonData.buildSuccess(pageMap);
}
//service
@Override
public Map<String, Object> pageCouponActivity(int page, int size) {
Page<CouponDO> pageInfo = new Page<>(page, size);
IPage<CouponDO> couponDOIPage = couponMapper.selectPage(pageInfo, new QueryWrapper<CouponDO>()
.eq("publish", CouponPublishEnum.PUBLISH)
.eq("category", CouponCategoryEnum.PROMOTION)
.orderByDesc("create_time"));
Map<String, Object> pageMap = new HashMap<>(3);
//总条数
pageMap.put("total_record", couponDOIPage.getTotal());
//总页数
pageMap.put("total_page", couponDOIPage.getPages());
pageMap.put("current_data", couponDOIPage.getRecords().stream().map(obj -> beanProcess(obj)).collect(Collectors.toList()));
return pageMap;
}