0%

pagehelper

需要的jar包

1
2
3
4
5
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>

在spring-dao.xml配置文件中添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />

<!-- 加载mybatis全局配置文件 -->
<property name="configLocation"
value="classpath:mybatis/mybatisConfig.xml" />
<property name="plugins">
<set>
<!--配置pageHelper 分页插件-->
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<!--方言:-->
<prop key="helperDialect">mysql</prop>
</props>
</property>
</bean>
</set>
</property>
</bean>

在controller中的代码

1
2
3
4
5
6
7
@RequestMapping("findUserList")
public String findUserList(@RequestParam(defaultValue ="1") Integer pageNum,@RequestParam(defaultValue ="5") Integer pageSize, Model model) {
PageInfo pageInfo = userService.findPage(pageNum, pageSize);
model.addAttribute("pageInfo", pageInfo);
System.out.println(pageInfo);
return "list.jsp";
}

在services层中的代码

1
2
3
4
5
6
7
8
9
@Autowired
private UserMapper userMapper;
@Override
public PageInfo findPage(int page, int pageSize) {
PageHelper.startPage(page,pageSize);
List<User> list=userMapper.selectByExample(null);
PageInfo pageInfo = new PageInfo(list);
return pageInfo;
}

前端页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
      <c:forEach items="${pageInfo.list}" var="user" varStatus="i">
<tr>
<td height="18" bgcolor="#FFFFFF">
<div align="center" class="STYLE1">
<input name="checkbox" type="checkbox" class="STYLE2" value="checkbox"/>
</div>
</td>
<td height="18" bgcolor="#FFFFFF" class="STYLE2">
<div align="center" class="STYLE2 STYLE1">
A00${i.count+(pageInfo.pageNum-1)*pageInfo.pageSize}
</div>
</td>
<td height="18" bgcolor="#FFFFFF">
<div align="center" class="STYLE2 STYLE1">${user.name}</div>
</td>
<td height="18" bgcolor="#FFFFFF">
<div align="center" class="STYLE2 STYLE1">${user.sex==true?'男':'女'}</div>
</td>
<td height="18" bgcolor="#FFFFFF">
<div align="center" class="STYLE2 STYLE1">${user.age}</div>
</td>
<td height="18" bgcolor="#FFFFFF">
<div align="center">
<a href="#">
<fmt:formatDate value="${user.birthday}" pattern="yyyy-MM-dd"/>
</a>
</div>
</td>

</c:forEach>
共${pageInfo.total}条纪录,当前第${pageInfo.pageNum}/${pageInfo.pages}页,每页${pageInfo.pageSize}条纪录
<a href="${pageContext.request.contextPath}/findUserList?pageNum=${pageInfo.isFirstPage==true?pageInfo.pageNum:pageInfo.prePage}">上一页</a>
<a href="${pageContext.request.contextPath}/findUserList?pageNum=${pageInfo.isLastPage==true?pageInfo.pageNum:pageInfo.nextPage}">下一页</a>

PageInfo中的信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class PageInfo<T> extends PageSerializable<T>  {
private static final long serialVersionUID = 1L;
//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//由于startRow 和endRow 不常用,这里说个具体的用法
//可以在页面中"显示startRow 到endRow 共size 条数据"
//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总页数
private int pages;
//前一页
private int prePage;
//下一页
private int nextPage;
//是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
//导航条上的第一页
private int navigateFirstPage;
//导航条上的最后一页
private int navigateLastPage;
}
1
2
3
4
5
6
7
public class PageSerializable<T> implements Serializable {
private static final long serialVersionUID = 1L;
//数据库中总记录数
private long total;
//结果集
protected List<T> list;

springboot中使用分页插件

依赖
1
2
3
4
5
6
	
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
配置
1
2
3
4
pagehelper:
helper-dialect: mysql
reasonable: false
support-methods-arguments: true
赏口饭吃吧!