您的位置:首页 > 其它

毕业实习第二周day2-SSM + PageHelper实现分页

2019-07-04 10:47 113 查看
  1. 导入PageHelper相关jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
</dependency>
  1. 配置Spring-dao.xml
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
reasonable=true
</value>
</property>
</bean>
</array>
</property>
  1. 定义dao层接口
Page<User> findAllUser();
  1. 在user-mapper.xml中完成对应sql语句的编写
<select id="findAllUser" resultMap="userResultMap">
select * from user
</select>
  1. 定义 service接口并实现
PageInfo findAllUser(int pageNum,int pageSize);
@Override
public PageInfo findAllUser(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
Page<User> users = userDao.findAllUser();
return new PageInfo<>(users);
}
  1. 实现Controller层
@Controller
@RequestMapping("page")
public class PageController {
@Autowired
UserDao userDao;
@Autowired
UserServce userServce;

@RequestMapping("alluser")
public String findAllUser(@RequestParam(value = "pageNum",required = false,defaultValue = "1") int pageNum,
@RequestParam(value = "pageSize",defaultValue = "3",required = false) int pageSize,
Model model){
PageInfo pg = userServce.findAllUser(pageNum,pageSize);
model.addAttribute("pg",pg);
return "allUser";
}
}
  1. 新建allUser.jsp显示数据
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false" %>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
<html>
<body>
<h2>Hello World!</h2>
<a href="${path}/page/alluser">显示</a>
</body>
</html>
allUser.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
<html>
<head>
<title>Title</title>
</head>
<body>
<table>
<c:forEach var="user" items="${pg.list}">
<tr>
<td>${user.username}</td>
<td>${user.password}</td>
</tr>
</c:forEach>
<a href="${path}/page/alluser?pageNum=${pg.pageNum + 1}">下一页</a>
<a href="${path}/page/alluser?pageNum=${pg.pageNum - 1}">上一页</a>
</table>
</body>
</html>
  1. 测试,分页成功
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: