mybatis分頁插件pageHelper實(shí)例詳解

分頁插件pagehelper也是一個(gè)很重要的插件,本文主要和大家介紹mybatis分頁插件pagehelper詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下,希望能幫助到大家。

mybatis分頁插件pageHelper詳解及簡(jiǎn)單實(shí)例

工作的框架spring springmvc mybatis3

首先使用分頁插件必須先引入maven依賴,在pom.xml中添加如下

<!-- 分頁助手 -->  <dependency><groupid>com.github.pagehelper</groupid><artifactid>pagehelper</artifactid><version>3.7.5</version></dependency>

其次需要在配置文件中添加配置,有兩種方式

1,新建mybatis-config.xml內(nèi)容如下

<?xml  version="1.0" encoding="UTF-8"?>nbsp;configuration  ?PUBLIC?"-//mybatis.org//DTD?Config?3.0//EN"  ?"http://mybatis.org/dtd/mybatis-3-config.dtd"&gt;    ?<configuration>  ?<!-- 分頁助手 -->  ?<plugins>  ??<!-- com.github.pagehelper為PageHelper類所在包名 -->  ??<plugin>  ??<!-- 數(shù)據(jù)庫(kù)方言 -->  ????<property></property>  ????<!-- 設(shè)置為true時(shí),使用RowBounds分頁會(huì)進(jìn)行count查詢 會(huì)去查詢出總數(shù) -->  ????<property></property>  ??</plugin></plugins>  ?</configuration>

在spring-mybatis.xml中添加一個(gè)bean屬性

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

加載全局的配置文件

<property></property>

配置mapper的掃描,找到所有的mapper.xml映射文件。

<property></property>

備注:如果你的mybatis-config.xml配置文件開啟了如下別名配置:

<typealiases>  ????<!-- javabean 的首字母小寫的非限定類名來作為它的別名(其實(shí)別名是不去分大小寫的)。也可在javabean 加上注解@Alias 來自定義別名, 例如: @Alias(student) -->  ????<package></package>  ??</typealiases>

那么你的spring和mybatis整合文件就得加上相應(yīng)的屬性,否則會(huì)造成mybatis配置文件加載不成功報(bào)異常,如下:

?  ????<property></property>  ????  ????<property></property>  ????  ????<property></property>  ????  ????<property></property>  ??

相比于上面的配置我們這里多了一步

????<property></property>

配置的時(shí)候要注意mybatis配置文件和spring-mybatis整合文件的屬性要統(tǒng)一。

2.如上操作配置完成,下面第二種方法

直接在spring-mybatis.xml中配置如下屬性

<property></property><property></property>  ??  ???  ????  ?????  ??????dialect=mysql  ??????rowBoundsWithCount=true  ?????  ????  ???  ??

配置文件加載好之后,就可以直接使用,具體使用代碼如下:

PageHelper.startPage(Integer.parseInt(currentPage),?Integer.parseInt(pageSize));  ??List<lytbbstz>?publishTz?=?bbsTzDao.getPublishTz(userId);  ??PageInfo<lytbbstz>?info?=?new?PageInfo<lytbbstz>(publishTz);  ??map.put("status",?1);  ??map.put("tzList",?info.getList());  ??return?map;</lytbbstz></lytbbstz></lytbbstz>

前臺(tái)需要傳入的參數(shù)是當(dāng)前頁和頁面顯示數(shù)目,當(dāng)然頁面顯示數(shù)目也可以后臺(tái)規(guī)定,一般在接收參數(shù)時(shí)最好加上默認(rèn)配置如下:

@RequestParam(defaultValue="1",value="currentPage")String?currentPage,?@RequestParam(defaultValue="10",value="pageSize")String?pageSize

這是如果接收參數(shù)為空字符串時(shí)它自身默認(rèn)顯示的頁面和條數(shù),這個(gè)可以自己規(guī)定

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊10 分享