springmvc+mybatis集成PageHelper分页插件

PageHelper版本5.0 sprng-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的类型


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

修改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条

关于spring整合通用mapper的可以查看:https://blog.csdn.net/BushQiang/article/details/80022820

案例下载地址:https://download.csdn.net/download/bushqiang/10365542

猜你喜欢

转载自blog.csdn.net/bushqiang/article/details/80033910
今日推荐