分頁插件使用的方式
修改 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 即可