知識分享:Mybatis框架如何使用分頁插件呢?

分頁插件使用的方式

  • 修改 pom 文件善玫,添加分頁 jar 包依賴

  • 修改 mybatis.xml 文件

  • UserDao 接口工窍,UserMapper.xml 添加對應(yīng)方法與實(shí)現(xiàn) sql

  • 對應(yīng) UserService 接口添加分頁查詢方法

  • 測試分頁效果

案例實(shí)操

1.修改 pom 文件,添加分頁 jar 包依賴

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n17" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="xml"><dependency>
?
<groupId>com.github.pagehelper</groupId>
?
<artifactId>pagehelper</artifactId>
?
<version>4.1.0</version>
?
</dependency> </pre>

2.修改 mybatis.xml 文件

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n19" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="xml"><plugins>
?

?
<plugin interceptor="com.github.pagehelper.PageHelper">
?
<property name="dialect" value="mysql" />
?

?

?

?
<property name="offsetAsPageNum" value="true" />
?

?

?
<property name="rowBoundsWithCount" value="true" />
?

?

?
<property name="pageSizeZero" value="true" />
?

?

?

?
<property name="reasonable" value="true" />
?

?

?

?
<property name="params"
?
value="pageNum=start;pageSize=limit;pageSizeZero=zero;reasonable=heli;count=cou
?
ntsql" />
?
</plugin>
?
</plugins> </pre>

3.UserDao 接口壁畸,UserMapper.xml 添加對應(yīng)方法與實(shí)現(xiàn) sql

UserDao 接口:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n23" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="java">public interface UserDao {
?
public User queryUserById(int id);
?
public List<User> queryUsers();
?
} </pre>

UserMapper.xml

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n25" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="xml"><?xml version="1.0" encoding="UTF-8" ?>
?
<!DOCTYPE mapper
?
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
?
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
?
<mapper namespace="com.mage.dao.UserDao">
?
<select id="queryUserById" parameterType="int" resultType="user">
?
select id,userName,userPwd from user where id=#{id}
?
</select>
?
<select id="queryUsers" resultType="user">
?
select id,userName,userPwd from user
?
</select>
?
</mapper> </pre>

4.對應(yīng) UserService 接口添加分頁查詢方法

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n27" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="java">public interface UserService {
?
public User queryUserById();
?
/**
?

  • 分頁方法定義
    ?
  • @param pageNum 當(dāng)前頁號
    ?
  • @param pageSize 設(shè)置每頁顯示數(shù)量
    ?
  • @return
    ?
    */
    ?
    public PageInfo<User> queryUsers(int pageNum,int pageSize);
    ?
    } </pre>

UserServiceImpl 實(shí)現(xiàn)方法:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n30" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="java">@Service
?
public class UserServiceImpl implements UserService{
?
@Resource
?
private UserDao userDao;
?
public User queryUserById(){
?
return userDao.queryUserById(7);
?
}
?
@Override
?
public PageInfo<User> queryUsers(int pageNum, int pageSize) {
?
/**
?

  • PageHelper 類設(shè)置分頁頁號與每頁大小
    ?
    */
    ?
    PageHelper.startPage(pageNum, pageSize);
    ?
    List<User> list=userDao.queryUsers();
    ?
    PageInfo<User> pageInfo=new PageInfo<User>(list);
    ?
    return pageInfo;
    ?
    }
    ?
    } </pre>

5.測試分頁效果

數(shù)據(jù)庫原始記錄

[圖片上傳失敗...(image-ef0301-1609136540367)]

測試

第一次 PageNum =1 pageSize=1

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n37" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="java">@Test
?
public void testQueryUsers() {
?
PageInfo<User> pageInfo= userService.queryUsers(1, 1);
?
for(User user:pageInfo.getList()){
?
System.out.println("user:"+user);
?
}
?
} </pre>

結(jié)果:

[圖片上傳失敗...(image-3f7928-1609136540366)]

第二次 pageNum=2 pageSize=1

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n42" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="java">@Test
?
public void testQueryUsers() {
?
PageInfo<User> pageInfo= userService.queryUsers(2, 1);
?
for(User user:pageInfo.getList()){
?
System.out.println("user:"+user);
?
}
?
} </pre>

結(jié)果

[圖片上傳失敗...(image-b38c7d-1609136540366)]

備注:分頁插件 如果傳入的頁碼 操作記錄總頁數(shù) 此時(shí)我們得到的是最后一頁的記錄

第三次測試 PageNum=3 pageSize=1

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n48" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="java">@Test
?
public void testQueryUsers() {
?
PageInfo<User> pageInfo= userService.queryUsers(3, 1);
?
for(User user:pageInfo.getList()){
?
System.out.println("user:"+user);
?
}
?
} </pre>

結(jié)果:

[圖片上傳失敗...(image-2d88dd-1609136540365)]

擴(kuò)展

分頁插件壓縮版

[圖片上傳失敗...(image-14fb50-1609136540365)]

解壓即可使用走敌,和之前配置一樣去配置好 config.xml跌榔,再運(yùn)行 run.bat 即可

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末残邀,一起剝皮案震驚了整個(gè)濱河市芥挣,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌扼菠,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件盗尸,死亡現(xiàn)場離奇詭異扣蜻,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)槽驶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進(jìn)店門辱姨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人稼锅,你說我怎么就攤上這事痊臭〉欠颍” “怎么了广匙?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長恼策。 經(jīng)常有香客問我鸦致,道長,這世上最難降的妖魔是什么涣楷? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任分唾,我火速辦了婚禮,結(jié)果婚禮上狮斗,老公的妹妹穿的比我還像新娘绽乔。我一直安慰自己,他們只是感情好碳褒,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布折砸。 她就那樣靜靜地躺著,像睡著了一般沙峻。 火紅的嫁衣襯著肌膚如雪鞍爱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天专酗,我揣著相機(jī)與錄音睹逃,去河邊找鬼。 笑死祷肯,一個(gè)胖子當(dāng)著我的面吹牛沉填,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播佑笋,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼翼闹,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蒋纬?” 一聲冷哼從身側(cè)響起猎荠,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蜀备,沒想到半個(gè)月后关摇,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡碾阁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年输虱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片脂凶。...
    茶點(diǎn)故事閱讀 39,926評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡宪睹,死狀恐怖愁茁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情亭病,我是刑警寧澤鹅很,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站罪帖,受9級特大地震影響道宅,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜胸蛛,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望樱报。 院中可真熱鬧葬项,春花似錦、人聲如沸迹蛤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽盗飒。三九已至嚷量,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間逆趣,已是汗流浹背蝶溶。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留宣渗,地道東北人抖所。 一個(gè)月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像痕囱,于是被迫代替她去往敵國和親田轧。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內(nèi)容