实体类,用户实体类
public class User {
private int id;
private String name;
private BigDecimal salary;
public User() {
}
public User(int id, String name, BigDecimal salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}
}
分页工具类
public class PageUtil {
private int firstPage =1; //首页
private int prePage; //上一页
private int nextPage; //下一页
private int totalPage; //末页/总页数
private int curPage; //当前页
private List datas; //需要显示的数据
private int curSize =9; //每页显示的数据
private int totalSize; // 总的记录数
public int getFirstPage() {
return firstPage;
}
public void setFirstPage(int firstPage) {
this.firstPage = firstPage;
}
public int getPrePage() {
return this.getCurPage()==this.getFirstPage()?this.getFirstPage():this.getCurPage()-1;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
public int getNextPage() {
return this.getCurPage()==this.getTotalPage()?this.getTotalPage():this.getCurPage()+1;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getTotalPage() {
return this.getTotalSize()%this.getCurSize()==0?this.getTotalSize()/this.getCurSize():this.getTotalSize()/this.getCurSize()+1;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public List getDatas() {
return datas;
}
public void setDatas(List datas) {
this.datas = datas;
}
public int getCurSize() {
return curSize;
}
public void setCurSize(int curSize) {
this.curSize = curSize;
}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
}
dao类
public class UserDao {
public int queryCount(){
QueryRunner runner = new QueryRunner(getSource(),true);
Long query = null;
try {
query = (Long) runner.query("select count(*) from user", new ScalarHandler());
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(query);
return query.intValue();
}
public List queryAll(int pageNo, int pageSize){
QueryRunner runner = new QueryRunner(getSource(), true);
BeanListHandler<User> handler = new BeanListHandler<>(User.class);
List<User> result = null;
try {
String sql = "select id,name,salary from user limit ?,?";
result = runner.query(sql, handler,(pageNo-1)*pageSize,pageSize);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("获取查询数据失败",e);
}
return result;
}
}
业务类
public class UserService {
public List queryAll(int pageNo, int pageSize){
List page = new UserDao().queryAll(pageNo,pageSize);
return page;
}
public int queryCount(){
return new UserDao().queryCount();
}
}
servlet类
@WebServlet("/user")
public class UserServlet extends HttpServlet {
UserService service = new UserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PageUtil pageUtil = new PageUtil();
//计算总记录数
pageUtil.setTotalSize(service.queryCount());
//计算当前页
String curpage = req.getParameter("curpage");
if (curpage == null || curpage.equals("")){
curpage = "1";
}
pageUtil.setCurPage(Integer.parseInt(curpage));
//计算每页显示的数据
pageUtil.setDatas(service.queryAll(pageUtil.getCurPage(),pageUtil.getCurSize()));
req.setAttribute("page",pageUtil);
req.getRequestDispatcher("WEB-INF/index.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
jsp显示
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/9/17
Time: 20:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<table border="1px" cellspacing="0" cellpadding="5">
<tr>
<th>编号</th>
<th>姓名</th>
<th>工资</th>
</tr>
<c:forEach items="${page.datas}" var="pa">
<tr>
<td>${pa.id}</td>
<td>${pa.name}</td>
<td>${pa.salary}</td>
</tr>
</c:forEach>
</table>
<a href="<c:url value='/user?curpage=${page.firstPage}' />">首页</a>
<a href="<c:url value='/user?curpage=${page.prePage}' />">下一页</a>
<a href="<c:url value='/user?curpage=${page.nextPage}' />">上一页</a>
<a href="<c:url value='/user?curpage=${page.totalPage}' />">尾页</a>
<form action="<c:url value='/user' />">
跳转到 <input type="text" name="curpage" size="2"/>页 <input value="跳转" type="submit"/>
</form>
跳转到 <input type="text" name="pageno" id="pagenoId" size="2"/>页 <input value="跳转" type="button" οnclick="tiaozhuan()"/>
当前为${page.curPage }页 /共有${page.totalPage }页 每页有${page.curSize }数据 总记录数${page.totalSize }
<script type="text/javascript">
function tiaozhuan() {
var pageid = document.getElementById("pagenoId");
var url = "<c:url value='/user?curpage="+pageid+"' />";
window.location.href = url;
}
</script>
</body>
</html>