导入依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.0</version>
</dependency>
修改spring核心配置文件:
applicationContext.xm
<!--sqlsessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!-- config params as the following -->
<value>
param1=value1
</value>
</property>
</bean>
</array>
</property>
</bean>
控制层代码:
@RequestMapping("/userList")
public String showUser(Model model, @RequestParam(defaultValue = "1") int pageNum,String username){
System.out.println("=============="+username+"=================");
//调用业务层代码
PageInfo<SmbmsUser> pageInfo = smbmsUserService.showList(pageNum,username);
System.out.println("========="+pageInfo+"===================");
//把list集合存储起来
model.addAttribute("username",username);
model.addAttribute("page",pageInfo);
return "userList";
}
前端:
<tr th:each="user:${page.list}">
<td th:text="${user.usercode}">hanlu</td>
<td th:text="${user.username}">韩露</td>
<td th:text="${user.gender}==1?'女':'男'">女</td>
<td th:text="${user.age}">20</td>
<td th:text="${user.phone}">15918230478</td>
<td th:switch="${user.userrole}">
<span th:case="1">管理员</span>
<span th:case="2">经理</span>
<span th:case="3">普通用户</span>
</td>
<td>
<a href="userView.html"><img src="images/read.png" alt="查看" title="查看"/></a>
<a href="userUpdate.html"><img src="images/xiugai.png" alt="修改" title="修改"/></a>
<a href="#" class="removeUser"><img src="images/schu.png" alt="删除" title="删除"/></a>
</td>
</tr>
</table>
<div>
共<span th:text="${page.total}">4</span>条记录,<span th:text="${page.pageNum}">1</span>/<span th:text="${page.pages}">4</span>
<a th:if="${page.pageNum}==1" href="javascript:void(0)">上一页</a>
<a href="#" th:if="${page.pageNum}!=1" th:href="@{/userList(pageNum=${page.pageNum}-1,username=${username})}">上一页</a>
<span th:each="i:${#numbers.sequence(1,page.pages)}">
<a th:href="@{/userList(pageNum=${i},username=${username})}" th:text="${i}">下一页</a>
</span>
<a th:if="${page.pageNum}==${page.pages}" href="javascript:void(0)">下一页</a>
<a href="#" th:if="${page.pageNum}!=${page.pages}" th:href="@{/userList(pageNum=${page.pageNum}+1,username=${username})}">下一页</a>
</div>