现在一直在使用mybatis,对于他的分页起初只是使用limit将页数和每一页的条数进行查询将该页的条目数进行显示。
这样感觉很麻烦分页需要写两条sql 查询条目、查询总个数
后来在网上找到mybatis的分页插件,自己试着整合到项目中,感觉不错。
记录一下
主要是参照 http://git.oschina.net/free/Mybatis_PageHelper
1· 首先要先引用分页需要的jar包
1)假如说你项目使用maven搭建需要添加如下依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
</dependency>
2)如果不是maven搭建直接引入jar包到lib下:
jsqlparser-0.9.1.jar
pagehelper-4.0.0.jar
2·在mybatis配置xml中配置拦截器插件:
<configuration>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="false"/>
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
<typeAliases>
<package name="com.jewery.model"/>
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
<!-- 和startPage中的pageNum效果一样-->
<property name="offsetAsPageNum" value="true"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true"/>
<!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
<!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
<property name="pageSizeZero" value="true"/>
<!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
<!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
<!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
<property name="reasonable" value="false"/>
<!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
<!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->
<!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值 -->
<!-- 不理解该含义的前提下,不要随便复制该配置 -->
<!-- <property name="params" value="pageNum=start;pageSize=limit;"/> -->
</plugin>
</plugins>
</configuration>
3·与spring进行集成:
有两种方式:
1)使用spring的属性配置方式可以使用plugins属性 像下面这样配置:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations">
<array>
<value>classpath:mapper/*.xml</value>
</array>
</property>
<property name="typeAliasesPackage" value="com.isea533.ssm.model"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=hsqldb
</value>
</property>
</bean>
</array>
</property>
</bean>
2)配置
configLocation
属性指向上面的
mybatis-config.xml
文件。有关分页插件的配置都在
mybatis-config.xml
,具体配置内容参考上面的
mybatis-config.xml
。
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
<property name="mapperLocations" value="classpath:com/jewery/mapping/*.xml" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean>
4·在代码中使用:
@Override
public void findRowsByParams(BrandBean brandBean) {
PageHelper.startPage(1, 10);
List<Brande> brandes = brandeMapper.findALL();
PageInfo<Brande> pages = new PageInfo<Brande>(brandes);
System.out.println("---------------------------------------");
System.out.println(JSON.toJSONString(pages));
System.out.println("---------------------------------------");
Assert.assertEquals(10, brandes.size());
for (Brande brande : brandes) {
System.out.println(brande.toString());
}
}
5·OK
注意mybatis需要使用3.2.4之上的版本
感谢 http://git.oschina.net/free/Mybatis_PageHelper大神,如有侵犯请告知,会删除