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

柔光的暖阳◎ 2023-10-08 20:06 54阅读 0赞

SpringCloud 大型系列课程正在制作中,欢迎大家关注与提意见。
程序员每天的CV 与 板砖,也要知其所以然,本系列课程可以帮助初学者学习 SpringBooot 项目开发 与 SpringCloud 微服务系列项目开发

1 项目准备

创建SpringBoot基础项目
SpringBoot项目集成mybatis
SpringBoot 集成 Druid 数据源【SpringBoot系列3】
SpringBoot MyBatis 实现分页查询数据【SpringBoot系列4】
SpringBoot MyBatis-Plus 集成 【SpringBoot系列5】
SpringBoot mybatis-plus-generator 代码生成器 【SpringBoot系列6】

官网地址:https://baomidou.com/

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);

所以可以基于 BaseMapper 来实现基本的分页查询
当前来查询 用户分类数据
在这里插入图片描述

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. public interface IUserCategoryService extends IService<UserCategory> {
  24. Object findPage(Integer index, Integer pageSize);
  25. }
  26. /**
  27. * <p>
  28. * 用户分类表 服务实现类
  29. * </p>
  30. *
  31. * @author 早起的年轻人
  32. * @since 2023-03-14
  33. */
  34. @Service("userCategoryService")
  35. public class UserCategoryServiceImpl extends ServiceImpl<UserCategoryMapper, UserCategory> implements IUserCategoryService {
  36. @Resource
  37. UserCategoryMapper userCategoryMapper;
  38. @Override
  39. public Object findPage(Integer index, Integer pageSize){
  40. UserCategory userCategory = userCategoryMapper.selectById(1);
  41. Page<UserCategory> page = Page.of(index,pageSize);
  42. //queryWrapper组装查询where条件
  43. LambdaQueryWrapper<UserCategory> queryWrapper = new LambdaQueryWrapper<>();
  44. //查询条件 ID = 1 的数据
  45. queryWrapper.eq(UserCategory::getId,1);
  46. //发起查询
  47. userCategoryMapper.selectPage(page,queryWrapper);
  48. return page;
  49. }
  50. }
  51. @Mapper
  52. public interface UserCategoryMapper extends BaseMapper<UserCategory> {
  53. }

postman 执行访问

在这里插入图片描述

如果分页查询出现 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://gitee.com/android.long/spring-boot-study/tree/master/biglead-api-05-mybatis-plus
有兴趣可以关注一下公众号:biglead

发表评论

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

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

相关阅读