mybatis-plus + PageHelper集成分页

本文记录了如何正确配置Mybatis-Plus与PageHelper的集成,避免因依赖冲突导致的错误。关键在于排除PageHelper中包含的Mybatis依赖,并在配置文件中设置相关参数。通过PageHelper.startPage()方法启动分页,然后使用mapper.selectList()获取数据,最后通过PageInfo获取分页信息。

看了很多文章都没说清楚怎么集成mybatis-plus + PageHelper,搞了挺久的不是报错就是缺少包,后面解决了特此记录一下。
先说配置pom和applocation.yml

mybatis + pagehelper
pom文件:

<!--pagehelper分页插件 -->
<dependency>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper-spring-boot-starter</artifactId>
   <version>1.2.5</version>
</dependency>

application.yml

pagehelper:
  auto-dialect: mysql
  reasonable: 
### 分查询的成与使用 在实际开发中,为了提升查询效率用户体验,通常会采用分查询的方式处理大数据量。MyBatis-Plus 提供了内置的分功能,而 PageHelper 是一个独立的分插件,两者可以结合使用,以实现更灵活的分需求。 #### 依赖引入 首先,需要在 `pom.xml` 文件中引入 MyBatis-Plus PageHelper 的依赖。以下是一个典型的 Maven 项目配置: ```xml <dependencies> <!-- MyBatis-Plus 核心依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency> <!-- PageHelper插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.2</version> </dependency> <!-- MySQL 数据库驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> </dependencies> ``` #### 配置文件设置 在 `application.yml` 文件中配置 MyBatis-Plus PageHelper 的相关参数: ```yaml server: port: 8080 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: mapper-locations: classpath:/mapper/*Mapper.xml typeAliasesPackage: com.example.entity global-config: id-type: AUTO field-strategy: NOT_EMPTY db-column-underline: true refresh-mapper: true configuration: map-underscore-to-camel-case: true cache-enabled: true pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql ``` #### 分查询实现 接下来,可以在服务层实现分查询功能。通过构造 `Page` 对象并调用 PageHelper 的 `startPage` 方法,可以轻松实现分查询。 ```java import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.example.entity.User; import com.example.mapper.UserMapper; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Autowired private UserMapper userMapper; public PageInfo<User> getUsersWithPagination(int pageNum, int pageSize) { // 使用 PageHelper 进行分 PageHelper.startPage(pageNum, pageSize); // 查询所有用户 List<User> users = userMapper.selectList(new QueryWrapper<User>().lambda()); // 使用 PageInfo 包装结果 return new PageInfo<>(users); } public Page<User> getMyBatisPlusUsersWithPagination(int pageNum, int pageSize) { // 构造 Page 对象 Page<User> page = new Page<>(pageNum, pageSize); // 使用 MyBatis-Plus查询 return userMapper.selectPage(page, new QueryWrapper<User>().lambda()); } } ``` #### 控制器层 在控制器层,可以通过调用服务层的方法来获取分数据,并返回给前端。 ```java import com.example.service.UserService; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @Autowired private UserService userService; @GetMapping("/users/pagehelper") public PageInfo<User> getUsersWithPageHelper(@RequestParam int pageNum, @RequestParam int pageSize) { return userService.getUsersWithPagination(pageNum, pageSize); } @GetMapping("/users/mybatisplus") public com.baomidou.mybatisplus.extension.plugins.pagination.Page<User> getUsersWithMyBatisPlus(@RequestParam int pageNum, @RequestParam int pageSize) { return userService.getMyBatisPlusUsersWithPagination(pageNum, pageSize); } } ``` #### 注意事项 - **分插件冲突**:在某些情况下,MyBatis-Plus PageHelper 可能会存在冲突,导致分失效。为了避免这种情况,建议在使用 PageHelper 时,尽量避免与 MyBatis-Plus 的分方法混用。 - **复杂 SQL 支持**:PageHelper 在处理复杂 SQL 时表现更好,而 MyBatis-Plus 的分功能在某些嵌套查询场景下可能会失效。 - **多数据源支持**:PageHelper 天然支持多数据源,而 MyBatis-Plus 需要特殊配置才能支持多数据源。 通过上述步骤,可以顺利地在 Spring Boot 项目中MyBatis-Plus PageHelper,实现高效的分查询功能。[^3]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值