SpringBoot MyBatis-Plus 分页查询 【SpringBoot系列7】

墨蓝 2023-10-08 21:46 51阅读 0赞

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

在Mybatis-Plus的 BaseMapper 中,已经内置了2个支持分页的方法:

  1. <P extends IPage<T>> P selectPage(P page, @Param("ew") Wrapper<T> queryWrapper);
  2. <P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param("ew") Wrapper<T> queryWrapper);
  3. 复制代码

所以可以基于 BaseMapper 来实现基本的分页查询 当前来查询 用户分类数据

format_png

1 基于 BaseMapper 来实现分页查询

  1. /**
  2. * <p>
  3. * 用户分类表 前端控制器
  4. * </p>
  5. *
  6. * @author 早起的年轻人
  7. * @since 2023-03-14
  8. */
  9. @Api(tags="用户分类模块")
  10. @RestController()
  11. @RequestMapping("/auto/userCategory")
  12. public class UserCategoryController {
  13. @Resource
  14. IUserCategoryService userCategoryService;
  15. @GetMapping(value="/findPage")
  16. @ApiOperation(value = "分页查询")
  17. public Object findPage(@RequestParam(required = false,defaultValue = "1") Integer index,
  18. @RequestParam(required = false,defaultValue = "10") Integer pageSize) {
  19. Object page = userCategoryService.findPage(index, pageSize);
  20. return page;
  21. }
  22. }
  23. 复制代码
  24. public interface IUserCategoryService extends IService<UserCategory> {
  25. Object findPage(Integer index, Integer pageSize);
  26. }
  27. 复制代码
  28. /**
  29. * <p>
  30. * 用户分类表 服务实现类
  31. * </p>
  32. *
  33. * @author 早起的年轻人
  34. * @since 2023-03-14
  35. */
  36. @Service("userCategoryService")
  37. public class UserCategoryServiceImpl extends ServiceImpl<UserCategoryMapper, UserCategory> implements IUserCategoryService {
  38. @Resource
  39. UserCategoryMapper userCategoryMapper;
  40. @Override
  41. public Object findPage(Integer index, Integer pageSize){
  42. UserCategory userCategory = userCategoryMapper.selectById(1);
  43. Page<UserCategory> page = Page.of(index,pageSize);
  44. //queryWrapper组装查询where条件
  45. LambdaQueryWrapper<UserCategory> queryWrapper = new LambdaQueryWrapper<>();
  46. //查询条件 ID = 1 的数据
  47. queryWrapper.eq(UserCategory::getId,1);
  48. //发起查询
  49. userCategoryMapper.selectPage(page,queryWrapper);
  50. return page;
  51. }
  52. }
  53. 复制代码
  54. @Mapper
  55. public interface UserCategoryMapper extends BaseMapper<UserCategory> {
  56. }
  57. 复制代码

postman 执行访问

format_png 1

如果分页查询出现 total 为0的问题,可以添加一个分页插件自定义配置,比如我这里直接放到了启动类里面

  1. @EnableFeignClients(basePackages = "com.biglead.feign.clients")
  2. @SpringBootApplication
  3. @MapperScan(basePackages = "com.biglead.admin.mapper")
  4. public class AdminApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(AdminApplication.class, args);
  7. }
  8. /**
  9. * 分页插件
  10. */
  11. @Bean
  12. public MybatisPlusInterceptor mybatisPlusInterceptor() {
  13. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  14. interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
  15. return interceptor;
  16. }
  17. }

作者:早起的年轻人
链接:https://juejin.cn/post/7213665579701092409

发表评论

表情:
评论列表 (有 0 条评论,51人围观)

还没有评论,来说两句吧...

相关阅读