springmvc+mybatis集成PageHelper分页插件

简介: springmvc+mybatis集成PageHelper分页插件

PageHelper版本5.0 spring-mybatis的版本1.3.1

maven下载插件

 <!--分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>${pagehelper.version}</version>
        </dependency>

sprng-mybatis更低版本集成的方法不一样,一开始使用低版本的配置方法会出现Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.Object[]' to required type 'org.apache.ibatis.plugin.Interceptor[]' for property 'plugins'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.github.pagehelper.PageHelper] to required type [org.apache.ibatis.plugin.Interceptor] for property 'plugins[0]': no matching editors or conversion strategy found; nested exception is org.springframework.beans.factory.BeanCreationException: 大概的意思是不能将不能将类型[com.github.pagHelpor.PageHelper ]转换为需要的类型org.apache.ibatis.plugin.Interceptor[]


原来的方法

    <!-- 配置分页插件 -->
     <property name="plugins">
        <array>
          <bean class="com.github.pagehelper.PageHelper">
            <property name="properties">
              <value>
                dialect=mysql
                reasonable=true
              </value>
            </property>
          </bean>
        </array>
      </property>

查看mybatis-spring的源码发现plugin是Interceptor的类型

2018042121485550.png

然后去pagehelper5.0下查看发现确实是有这个类型

20180421215048440.png

修改spring-mybatis的配置,改成PageInterceptor的类

  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--数据库的数据源配置 -->
    <property name="dataSource" ref="dataSource" />
    <!-- mysqlSqlSessionFactory会自动扫描该路径下的所有文件并解析。 -->
    <property name="mapperLocations" value="classpath:mapper/*.xml" />
    <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 ,该属性可以给包中的类注册别名,注册后可以直接使用类名,而不用使用全限定的类名 -->
    <property name="typeAliasesPackage" value="com.smxy.lq.pojo" />
    <!--配置mybatis的分页插件  -->
    <property name="plugins">
      <array>
        <bean class="com.github.pagehelper.PageInterceptor">
          <property name="properties">
            <!--使用下面的方式配置参数,一行配置一个 -->
            <value>
              helperDialect=mysql
            </value>
          </property>
        </bean>
      </array>
    </property>
  </bean>

查询结果图,每页10条

20180421220508162.png


相关文章
|
2月前
|
SQL XML Java
8、Mybatis-Plus 分页插件、自定义分页
这篇文章介绍了Mybatis-Plus的分页功能,包括如何配置分页插件、使用Mybatis-Plus提供的Page对象进行分页查询,以及如何在XML中自定义分页SQL。文章通过具体的代码示例和测试结果,展示了分页插件的使用和自定义分页的方法。
8、Mybatis-Plus 分页插件、自定义分页
|
10天前
|
SQL JSON Java
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
22 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
|
1月前
|
SQL Java 数据库连接
解决mybatis-plus 拦截器不生效--分页插件不生效
本文介绍了在使用 Mybatis-Plus 进行分页查询时遇到的问题及解决方法。依赖包包括 `mybatis-plus-boot-starter`、`mybatis-plus-extension` 等,并给出了正确的分页配置和代码示例。当分页功能失效时,需将 Mybatis-Plus 版本改为 3.5.5 并正确配置拦截器。
342 6
解决mybatis-plus 拦截器不生效--分页插件不生效
|
1月前
|
SQL XML Java
springboot整合mybatis-plus及mybatis-plus分页插件的使用
这篇文章介绍了如何在Spring Boot项目中整合MyBatis-Plus及其分页插件,包括依赖引入、配置文件编写、SQL表创建、Mapper层、Service层、Controller层的创建,以及分页插件的使用和数据展示HTML页面的编写。
springboot整合mybatis-plus及mybatis-plus分页插件的使用
|
1月前
|
监控 关系型数据库 MySQL
zabbix agent集成percona监控MySQL的插件实战案例
这篇文章是关于如何使用Percona监控插件集成Zabbix agent来监控MySQL的实战案例。
37 2
zabbix agent集成percona监控MySQL的插件实战案例
|
2月前
|
SQL Java 关系型数据库
MyBatis-Plus 分页魅力绽放!紧跟技术热点,带你领略数据分页的高效与便捷
【8月更文挑战第29天】在 Java 开发中,数据处理至关重要,尤其在大量数据查询与展示时,分页功能尤为重要。MyBatis-Plus 作为一款强大的持久层框架,提供了便捷高效的分页解决方案。通过封装数据库分页查询语句,开发者能轻松实现分页功能。在实际应用中,只需创建 `Page` 对象并设置页码和每页条数,再通过 `QueryWrapper` 构建查询条件,调用 `selectPage` 方法即可完成分页查询。MyBatis-Plus 不仅生成分页 SQL 语句,还自动处理参数合法性检查,并支持条件查询和排序等功能,极大地提升了系统性能和稳定性。
48 0
|
2月前
|
IDE API 开发工具
通过IDE插件体验阿里云OpenAPI的高效集成, 精品礼品等你来拿!
轻量级的开放API工具——Alibaba Cloud Developer Toolkit及Alibaba Cloud API Toolkit。这些插件支持快速查阅阿里云产品的开放API,提供API调试与SDK示例生成等功能,帮助开发者轻松集成阿里云服务。您可通过JetBrains Marketplace或VS Code Marketplace搜索安装,完成身份验证后即刻体验。欢迎分享您的使用反馈,有机会获得精美礼品!
|
3月前
|
监控 druid Java
spring boot 集成配置阿里 Druid监控配置
spring boot 集成配置阿里 Druid监控配置
242 6
|
3月前
|
Java 关系型数据库 MySQL
如何实现Springboot+camunda+mysql的集成
【7月更文挑战第2天】集成Spring Boot、Camunda和MySQL的简要步骤: 1. 初始化Spring Boot项目,添加Camunda和MySQL驱动依赖。 2. 配置`application.properties`,包括数据库URL、用户名和密码。 3. 设置Camunda引擎属性,指定数据源。 4. 引入流程定义文件(如`.bpmn`)。 5. 创建服务处理流程操作,创建控制器接收请求。 6. Camunda自动在数据库创建表结构。 7. 启动应用,测试流程启动,如通过服务和控制器开始流程实例。 示例代码包括服务类启动流程实例及控制器接口。实际集成需按业务需求调整。
289 4
|
3月前
|
消息中间件 Java 测试技术
【RocketMQ系列八】SpringBoot集成RocketMQ-实现普通消息和事务消息
【RocketMQ系列八】SpringBoot集成RocketMQ-实现普通消息和事务消息
252 1