知识分享:Mybatis框架如何使用分页插件呢?

分页插件使用的方式

  • 修改 pom 文件,添加分页 jar 包依赖

  • 修改 mybatis.xml 文件

  • UserDao 接口,UserMapper.xml 添加对应方法与实现 sql

  • 对应 UserService 接口添加分页查询方法

  • 测试分页效果

案例实操

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 添加对应方法与实现 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.对应 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 当前页号
    ​
  • @param pageSize 设置每页显示数量
    ​
  • @return
    ​
    */
    ​
    public PageInfo<User> queryUsers(int pageNum,int pageSize);
    ​
    } </pre>

UserServiceImpl 实现方法:

<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 类设置分页页号与每页大小
    ​
    */
    ​
    PageHelper.startPage(pageNum, pageSize);
    ​
    List<User> list=userDao.queryUsers();
    ​
    PageInfo<User> pageInfo=new PageInfo<User>(list);
    ​
    return pageInfo;
    ​
    }
    ​
    } </pre>

5.测试分页效果

数据库原始记录

[图片上传失败...(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>

结果:

[图片上传失败...(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>

结果

[图片上传失败...(image-b38c7d-1609136540366)]

备注:分页插件 如果传入的页码 操作记录总页数 此时我们得到的是最后一页的记录

第三次测试 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>

结果:

[图片上传失败...(image-2d88dd-1609136540365)]

扩展

分页插件压缩版

[图片上传失败...(image-14fb50-1609136540365)]

解压即可使用,和之前配置一样去配置好 config.xml,再运行 run.bat 即可

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容