Mybatis-Puls & pagehelper分页问题

本文探讨了Mybatis-Plus和PageHelper这两个Mybatis扩展在实际使用中可能出现的版本冲突问题,特别是在分页功能上。当它们与不同版本的Mybatis并存时,可能导致兼容性问题。解决方案包括通过Maven的exclusions标签排除冲突的Mybatis依赖,或者选择一个进行替代。推荐采用统一Mybatis版本并排除冲突依赖的方法,以保持系统的稳定性和兼容性。

前尘往事

Mybatis-Puls & pagehelper,mybatis领域的两个大块头来的。

用过mybatis的基本上都有用过他们。

mybatis-plus大本营:https://mp.baomidou.com/

pagehelper大本营:https://pagehelper.github.io/

双方团队的粉丝都很多,大部分都属于两面派的那种。

平时没事,大家都嘻嘻哈哈,和稀泥,打太极。

但是用的不好,双方就会直接干起来。

分页罢工的事情,很多人都踩过。

细究原因....

前因后果

Mybatis-Plus和pagehelper都是基于Mybatis的基础上进行二开的。

所以,他们的问题基本上就比较明显。

每个的对应的Mybatis的版本都是不一样的。

在实际代码的引入包中,会发生版本的冲突问题。

一般分页出问题的,大多数都是因为版本不兼容的问题导致的。

对应的版本不一致,用时一时爽,调试火葬场。

有兴趣的可以看下pagehelper和mybatis-plus的版本中包含的mybatis的包的版本。

您会发现两位猪哥的版本都是按照自己的喜好来的。

pagehelper更新的版本(传送门):

https://github.com/pagehelper/pagehelper-spring-boot

前思后想

分页问题要解决的话,江湖上一般就两种方案:

1、两者共存。

让Mybatis版本统一,干掉其中的一个mybatis的版本。

maven中一般使用exclusion,将其中一个忽略掉。

如下所示:

     <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>1.2.13</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.mybatis</groupId>
                        <artifactId>mybatis</artifactId>
                    </exclusion>
                    <exclusion>
                        <artifactId>mybatis-spring</artifactId>
                        <groupId>org.mybatis</groupId>
                    </exclusion>
                </exclusions>
            </dependency>

2、干掉其中一个。

2.1、干掉pagehelper,分页直接用mybatis-plus的IPage来实现。

2.2、干掉mybatis-plus,苦哈哈去刷xml文档。

总结

推荐使用方法一,毕竟两者都有各自的优势来的。

### 分页查询的集成与使用 在实际开发中,为了提升查询效率用户体验,通常会采用分页查询的方式处理大数据量。MyBatis-Plus 提供了内置的分页功能,而 PageHelper 是一个独立的分页插件,两者可以结合使用,以实现更灵活的分页需求。 #### 依赖引入 首先,需要在 `pom.xml` 文件中引入 MyBatis-Plus PageHelper 的依赖。以下是一个典型的 Maven 项目配置: ```xml &lt;dependencies&gt; &lt;!-- MyBatis-Plus 核心依赖 --&gt; &lt;dependency&gt; &lt;groupId&gt;com.baomidou&lt;/groupId&gt; &lt;artifactId&gt;mybatis-plus-boot-starter&lt;/artifactId&gt; &lt;version&gt;3.5.1&lt;/version&gt; &lt;/dependency&gt; &lt;!-- PageHelper 分页插件 --&gt; &lt;dependency&gt; &lt;groupId&gt;com.github.pagehelper&lt;/groupId&gt; &lt;artifactId&gt;pagehelper-spring-boot-starter&lt;/artifactId&gt; &lt;version&gt;1.4.2&lt;/version&gt; &lt;/dependency&gt; &lt;!-- MySQL 数据库驱动 --&gt; &lt;dependency&gt; &lt;groupId&gt;mysql&lt;/groupId&gt; &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt; &lt;version&gt;8.0.28&lt;/version&gt; &lt;/dependency&gt; &lt;/dependencies&gt; ``` #### 配置文件设置 在 `application.yml` 文件中配置 MyBatis-Plus PageHelper 的相关参数: ```yaml server: port: 8080 spring: datasource: url: jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&amp;characterEncoding=UTF-8&amp;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&lt;User&gt; getUsersWithPagination(int pageNum, int pageSize) { // 使用 PageHelper 进行分页 PageHelper.startPage(pageNum, pageSize); // 查询所有用户 List&lt;User&gt; users = userMapper.selectList(new QueryWrapper&lt;User&gt;().lambda()); // 使用 PageInfo 包装结果 return new PageInfo&lt;&gt;(users); } public Page&lt;User&gt; getMyBatisPlusUsersWithPagination(int pageNum, int pageSize) { // 构造 Page 对象 Page&lt;User&gt; page = new Page&lt;&gt;(pageNum, pageSize); // 使用 MyBatis-Plus 分页查询 return userMapper.selectPage(page, new QueryWrapper&lt;User&gt;().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(&quot;/users/pagehelper&quot;) public PageInfo&lt;User&gt; getUsersWithPageHelper(@RequestParam int pageNum, @RequestParam int pageSize) { return userService.getUsersWithPagination(pageNum, pageSize); } @GetMapping(&quot;/users/mybatisplus&quot;) public com.baomidou.mybatisplus.extension.plugins.pagination.Page&lt;User&gt; 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
发出的红包

打赏作者

沛哥儿

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值