springBoot项目中分页插件配置

本文展示了如何在SpringBoot应用中使用MyBatis的PaginationInterceptor插件实现分页查询。通过配置@Bean注解添加插件,并在Controller中定义分页查询接口,实现了对讲师列表的分页展示,返回包含总条数和分页数据的响应。

在全局配置类中添加插件

/**
 * 分页插件
  */
 @Bean
 public PaginationInterceptor paginationInterceptor() {
     return new PaginationInterceptor();
 }

分页controller方法

@ApiOperation(value = "分页讲师列表")
@GetMapping("{page}/{limit}")
public R pageList(
    @ApiParam(name = "page", value = "当前页码", required = true)
    @PathVariable Long page,

    @ApiParam(name = "limit", value = "每页记录数", required = true)
    @PathVariable Long limit){

    Page<Teacher> pageParam = new Page<>(page, limit);

    teacherService.page(pageParam, null);
    List<Teacher> records = pageParam.getRecords();
    long total = pageParam.getTotal();

    return  R.ok().data("total", total).data("rows", records);
}

这样就能实现分页效果

<think>好的,我现在需要解决用户的问题:在Spring Boot项目中如何配置和使用分插件,比如PageHelper或者MyBatis-Plus的分功能。用户还提到了之前遇到的依赖冲突问题,需要特别注意这一点。 首先,我得回忆一下MyBatis-Plus和PageHelper各自的配置方法。MyBatis-Plus自带分插件,可能更简单,而PageHelper需要额外配置。不过用户可能两者都想了解,所以需要分别说明。 对于MyBatis-Plus的分功能,我记得需要添加PaginationInnerInterceptor拦截器。然后,在Service层使用Page对象进行分查询。但用户可能对具体步骤不清楚,比如依赖的添加、配置类如何编写,以及如何在代码中使用。 然后,关于PageHelper,用户提到在整合时遇到依赖冲突,特别是mybatis-spring和mybatis的版本问题。所以,在配置PageHelper时,必须排除冲突的依赖。比如,使用pagehelper-spring-boot-starter时,可能需要排除掉MyBatis-Plus自带的mybatis依赖,或者调整版本号。这时候需要具体步骤,比如在pom.xml中如何排除依赖。 另外,用户提供的引用内容提到,排除pagehelper的依赖中的mybatis部分,或者排除MyBatis-Plus中的相关依赖。所以,在步骤中需要详细说明如何检查依赖冲突,使用Maven的dependency:tree命令,然后进行排除操作。 接下来,需要分步骤写出两种分方式的配置方法。对于MyBatis-Plus,步骤可能包括添加依赖、配置插件、使用Page对象。而对于PageHelper,则是添加依赖(注意排除冲突)、配置参数、使用PageHelper.startPage方法。 同时,用户可能对两者的区别和适用场景有疑问,是否需要比较一下?不过用户的问题主要是配置和使用,所以可能不需要深入比较,但可以简要提及。 还要注意代码示例的正确性,比如配置类的注解是否正确,是否需要@Configuration,分插件的Bean是否正确创建。例如,MyBatis-Plus的配置类中,添加分插件到MybatisPlusInterceptor的拦截器中。 对于PageHelper,可能需要配置properties,比如helperDialect,但使用starter的话,可能已经自动配置,只需要在application.yml中设置参数即可。 最后,生成相关问题,可能包括依赖冲突解决、性能优化、如何选择分插件等。 现在需要组织这些信息,按照用户要求的格式,分步骤说明两种方法,确保依赖和配置正确,并注意处理冲突的情况。同时,引用用户提供的引用内容作为参考,添加引用标识。</think>### 如何在 Spring Boot配置和使用分插件 #### 一、使用 MyBatis-Plus 分功能 MyBatis-Plus 内置了分插件配置简单且与框架深度集成。 1. **添加依赖** 在 `pom.xml` 中添加 MyBatis-Plus 的 Spring Boot 依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3.1</version> </dependency> ``` 2. **配置插件** 在配置类中注册分拦截器: ```java @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); return interceptor; } } ``` 3. **使用分查询** 在 Service 层通过 `Page` 对象实现分: ```java public Page<User> getUserPage(int pageNum, int pageSize) { Page<User> page = new Page<>(pageNum, pageSize); return userMapper.selectPage(page, null); } ``` --- #### 二、使用 PageHelper 分插件 PageHelper 是通用分插件,需注意依赖冲突问题(尤其是与 MyBatis-Plus 共存时)[^2]。 1. **添加依赖(需排除冲突)** 在 `pom.xml` 中添加 PageHelper 依赖,并排除冲突的 MyBatis 依赖: ```xml <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.6</version> <exclusions> <exclusion> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </exclusion> <exclusion> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </exclusion> </exclusions> </dependency> ``` 2. **配置参数(可选)** 在 `application.yml` 中设置方言等参数: ```yaml pagehelper: helper-dialect: mysql reasonable: true ``` 3. **使用分查询** 在查询前调用 `PageHelper.startPage`: ```java public List<User> getUserPage(int pageNum, int pageSize) { PageHelper.startPage(pageNum, pageSize); return userMapper.selectAll(); } ``` --- #### 三、关键注意事项 1. **依赖冲突解决** 若同时使用 MyBatis-Plus 和 PageHelper,需通过 `mvn dependency:tree` 检查依赖树,确保 MyBatis 相关库版本一致。 2. **性能优化** - MyBatis-Plus 分会自动优化 SQL,生成 `COUNT` 查询。 - PageHelper 需手动添加 `PageInfo` 包装类获取总条数。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值