共計(jì) 2109 個(gè)字符,預(yù)計(jì)需要花費(fèi) 6 分鐘才能閱讀完成。
自動(dòng)寫代碼機(jī)器人,免費(fèi)開通
丸趣 TV 小編給大家分享一下 mybatis 分頁插件 pageHelper 的使用示例,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
工作的框架 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 ?
!DOCTYPE configuration
PUBLIC -//mybatis.org//DTD Config 3.0//EN
http://mybatis.org/dtd/mybatis-3-config.dtd
configuration
!-- 分頁助手 --
plugins
!-- com.github.pagehelper 為 PageHelper 類所在包名 --
plugin interceptor= com.github.pagehelper.PageHelper
!-- 數(shù)據(jù)庫方言 --
property name= dialect value= MySQL /
!-- 設(shè)置為 true 時(shí),使用 RowBounds 分頁會(huì)進(jìn)行 count 查詢 會(huì)去查詢出總數(shù) --
property name= rowBoundsWithCount value= true /
/plugin
/plugins
/configuration
在 spring-mybatis.xml 中添加一個(gè) bean 屬性
bean id= sqlSessionFactory > 加載全局的配置文件
property name= configLocation value= classpath:mybatis-config.xml /property
配置 mapper 的掃描,找到所有的 mapper.xml 映射文件。
property name= mapperLocations value= classpath:com/lyitong/mapping/*.xml /property
備注:如果你的 mybatis-config.xml 配置文件開啟了如下別名配置:
typeAliases
!-- javabean 的首字母小寫的非限定類名來作為它的別名(其實(shí)別名是不去分大小寫的)。也可在 javabean 加上注解 @Alias 來自定義別名, 例如: @Alias(student) --
package name= com.lyt.usermanage.mapper /
/typeAliases
那么你的 spring 和 mybatis 整合文件就得加上相應(yīng)的屬性,否則會(huì)造成 mybatis 配置文件加載不成功報(bào)異常,如下:
bean id= sqlSessionFactory > 相比于上面的配置我們這里多了一步
property name= typeAliasesPackage value= classpath:com/lyt/usermanage/pojo/* /property
配置的時(shí)候要注意 mybatis 配置文件和 spring-mybatis 整合文件的屬性要統(tǒng)一。
2. 如上操作配置完成,下面第二種方法
直接在 spring-mybatis.xml 中配置如下屬性
bean id= sqlSessionFactory > 配置文件加載好之后,就可以直接使用,具體使用代碼如下:
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;
前臺(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ī)定
看完了這篇文章,相信你對“mybatis 分頁插件 pageHelper 的使用示例”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注丸趣 TV 行業(yè)資訊頻道,感謝各位的閱讀!
向 AI 問一下細(xì)節(jié)