Spring-Mybatis集成PageHelper

2020-06-24  本文已影响0人  蓝色Hippie

一、maven依赖(如果不是maven项目,可以自己去下载jar包)

在进行maven依赖时,各位大佬都提到了一定要注意jar包版本的匹配,在一位大佬博客扒了一套可行性方案,mybatis 3.2.8,pagehelper 5.0.2,jsqlparser 0.9.5,依赖如下

<dependency>

     <groupId>org.mybatis</groupId> 

     <artifactId>mybatis</artifactId> 

     <version>3.2.8</version>

 </dependency>

 <dependency> 

     <groupId>com.github.pagehelper</groupId> 

     <artifactId>pagehelper</artifactId>

     <version>5.0.2</version> </dependency>

<dependency>

     <groupId>com.github.jsqlparser</groupId>

     <artifactId>jsqlparser</artifactId> 

     <version>0.9.5</version>

 </dependency>

二、将pagehelper与spring进行集成,有两种方式,一种是直接与spring集成,一种是配置在mybatis的配置文件里面

1.spring集成

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

       <property name="dataSource" ref="dataSource" />

       <property name="mapperLocations" value="classpath*:com/p2p/mapper/*.xml" />

     <property name="typeAliasesPackage" value="com.p2p.domain" />

 <!-- 配置分页插件 -->

     <property name="plugins"> 

         <array> 

         <bean class="com.github.pagehelper.PageInterceptor">

         <property name="properties"> <value> helperDialect=mysql reasonable=true autoRuntimeDialect=true         </value> </property> 

         </bean> 

     </array> </property> </bean>

在applicationContext.xml文件中加入配置分页插件那一段就可以了,千万记住bean里面的class等于com.github.pagehelper.PageInterceptor,之前我抄大佬的代码可能是因为版本问题,他们都是com.github.pagehelper.PageHelper

2.mybatis配置

<?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> 

 <!-- 配置mybatis的分页插件PageHelper -->

 <plugins> 

 <plugin interceptor="com.github.pagehelper.PageInterceptor"> 

 </plugin>

 </plugins> 

 </configuration>

因为我是用spring集成的,这是额外写的一个mybatis的核心配置文件,如果有核心配置文件,把plugins那一段加上就可以了,最后在applicationContext-mvc.xml文件里面引入mybatis的配置文件

三、业务层使用

四、插件注意事项

上一篇 下一篇

猜你喜欢

热点阅读