开发者社区> 殇丶善若水> 正文

Mybatis分页插件PageHelper-5.1.1

简介: Mybatis分页插件PageHelper-5.1.1
+关注继续查看

PageHelper-5.1.1和PageHelper-4.0.0是有区别的
PageHelper-4.0.0的版本时,Mybatis全局配置文件SqlMapConfig.xml的内容是:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <plugins>
        <!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库-->
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

当我PageHelper的版本改为5.1.1时,tomcat启动报错:
image
java.lang.ClassCastException: com.github.pagehelper.PageHelper cannot be cast to org.apache.ibatis.plugin.Interceptor
这句报错信息说明PageHelper这个类没有实现Interceptor这个接口,我们看下源码:

package com.github.pagehelper;

import com.github.pagehelper.dialect.AbstractHelperDialect;
import com.github.pagehelper.page.PageAutoDialect;
import com.github.pagehelper.page.PageMethod;
import com.github.pagehelper.page.PageParams;
import com.github.pagehelper.util.StringUtil;
import java.util.List;
import java.util.Properties;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.RowBounds;

public class PageHelper extends PageMethod implements Dialect {
    private PageParams pageParams;
    private PageAutoDialect autoDialect;

    public PageHelper() {
    }

PageHelper可以看到,这个类并没有实现Interceptor这个接口
在这里我们要了解下PageHelper在Mybatis中是如何工作的:
通过mybatis的pulgin实现了Interceptor接口,从而获得要执行的sql语句实现分页技术,而我们的PageHelper5.1.1版本中的这个类,并没有出现implements Interceptor,找找pagehelper这个包下的其他类
image
PageInterceptor类内容如下:

package com.github.pagehelper;

@Intercepts({@Signature(
    type = Executor.class,
    method = "query",
    args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
), @Signature(
    type = Executor.class,
    method = "query",
    args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}
)})
public class PageInterceptor implements Interceptor {
    protected Cache<String, MappedStatement> msCountMap = null;
    private Dialect dialect;
    private String default_dialect_class = "com.github.pagehelper.PageHelper";
    private Field additionalParametersField;
    private String countSuffix = "_COUNT";

    public PageInterceptor() {
    }

PageInterceptor实现了Interceptor接口
将SqlMapConfig.xml做修改:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <plugins>
        <!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库-->
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

重新运行tomcat
然后。。。
image
网上找的:PageHelper插件4.0.0以后的版本支持自动识别使用的数据库,可以不用配置

<property name="dialect" value="mysql"/>

将SqlMapConfig.xml做修改:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <plugins>
        <!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库-->
            <!--<property name="dialect" value="mysql"/>-->
        </plugin>
    </plugins>
</configuration>

再次重新运行tomcat
image
成功了!
结束

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Mybatis-Plus学习(二):乐观锁插件
Mybatis-Plus学习(二):乐观锁插件
19 0
Mybatis插件better-mybatis-generator的下载与使用
Mybatis插件better-mybatis-generator的下载与使用
62 0
【IntelliJ IDEA】idea中的插件之一:Free Mybatis plugin跳转插件的使用(方便在Dao接口和Mappper XML文件之间进行切换)
之前使用MyBatis框架或者是在IDEA中,发现Mapper接口和XML文件之间跳转十分的麻烦,我之前经常的操作是在Mapper接口中将接口名称复制一下,然后去查找对应的XML文件,打开后CRTL+F查找对应的xml实现,整个过程效率很低下,搜了搜果然有前辈已经出了一款IDEA的插件解决了这个问题,把这个好用的跳转插件推荐给大家。
175 0
Mybatis pagehelper分页查询插件
你好看官,里面请!今天笔者讲的是pagehelper分页查询。不懂或者觉得我写的有问题可以在评论区留言,我看到会及时回复。 注意:本文仅用于学习参考,不可用于商业用途,如需转载请跟我联系。
55 0
Mybatis源码剖析之插件interceptor执行原理
mybatis通过插件 对(Executor、StatementHandler、ParameterHandler、ResultSetHandler) 这四个 核心对象创建代理进行拦截 对mybatis来说插件就是拦截器,用来增强核心对象的功能,增强功能本质上是借助于底层的 动态代理实现的,换句话说,MyBatis中的四大对象都是代理对象
30 0
Mybatis-Plus动态表名插件实现数据库分表查询
Mybatis-Plus动态表名插件实现数据库分表查询
143 0
Spring Boot中的Mybatis分页插件-pagehelper的使用
Spring Boot中的Mybatis分页插件-pagehelper的使用
98 0
Mybatis分页插件PageHelper的学习与使用
Mybatis分页插件PageHelper的学习与使用
102 0
【手撕Mybatis的分页插件】【查询结果集是0,直接返回[]】【提高查询我们的性能】
【手撕Mybatis的分页插件】【查询结果集是0,直接返回[]】【提高查询我们的性能】
38 0
文章
问答
文章排行榜
最热
最新
相关电子书
更多
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题)
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载