一、分页项目
创建数据库的SQL
CREATE TABLE `student` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`chinese` float DEFAULT NULL,
`english` float DEFAULT NULL,
`math` float DEFAULT NULL,
`b` date DEFAULT NULL,
`b2` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
PageBean.java
public class PageBean<T> {
private int currentPage=1;
private int pageCount=4;
private int totalPage;
private int totalCount;
private List<T> data;
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getTotalPage() {
if(pageCount==0||totalCount==0) {
return 0;
}
System.out.println("pageCount"+pageCount);
System.out.println("totalCount"+totalCount);
System.out.println(totalCount%pageCount);
if(totalCount%pageCount>0) {
totalPage=totalCount/pageCount+1;
}else {
totalPage=totalCount/pageCount;
}
return totalPage;
}
public static void main(String[] args) {
System.out.println(106/4);
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
}
StudentDao.java
public class StudentDao {
public PageBean<StudentEntity> getAll(PageBean<StudentEntity> page) {
int index = (page.getCurrentPage()-1)*page.getPageCount();
int count = page.getPageCount();
int totalCount = this.getTotalCount();
page.setTotalCount(totalCount);
if(page.getCurrentPage()<=0) {
page.setCurrentPage(1);
index = (page.getCurrentPage()-1)*page.getPageCount();
}
if(page.getCurrentPage()>page.getTotalPage()) {
page.setCurrentPage(page.getTotalPage());
index = (page.getCurrentPage()-1)*page.getPageCount();
}
try {
String sql="select * from student limit ?,?";
QueryRunner qr = JdbcUtil.getQueryRunner();
List<StudentEntity> data = qr.query(sql, new BeanListHandler<StudentEntity>(StudentEntity.class), index,count);
page.setData(data);
} catch (SQLException e) {
e.printStackTrace();
}
return page;
}
public int getTotalCount() {
String sql="select count(*) from student";
QueryRunner qr = JdbcUtil.getQueryRunner();
try {
long num = qr.query(sql, new ScalarHandler<Long>());
System.out.println(num);
return (int)num;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
}
StudentService.java
public class StudentService {
private StudentDao dao=new StudentDao();
public PageBean<StudentEntity> getAll(int currentPage) {
PageBean<StudentEntity> page=new PageBean<StudentEntity>();
page.setCurrentPage(currentPage);
return dao.getAll(page);
}
}
StudentController.java
public class StudentController extends HttpServlet{
private StudentService service=new StudentService();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String url="";
try {
String currentPage = req.getParameter("currentPage");
if(currentPage==null||"".equals(currentPage)) {
currentPage="1";
}
int currPage=Integer.valueOf(currentPage);
PageBean<StudentEntity> page = service.getAll(currPage);
req.setAttribute("page", page);
url="/WEB-INF/list.jsp";
} catch (Exception e) {
e.printStackTrace();
url="/error.jsp";
}
req.getRequestDispatcher(url).forward(req, resp);
}
}
JdbcUtil.java
public class JdbcUtil {
private static ComboPooledDataSource dataSource;
static {
dataSource=new ComboPooledDataSource();
}
public static QueryRunner getQueryRunner() {
return new QueryRunner(dataSource);
}
}
c3p0-config.xml
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/day11</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">3</property>
<property name="maxPoolSize">6</property>
<property name="maxIdleTime">1000</property>
</default-config>
</c3p0-config>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" align="center" width="400px" cellpadding="2" cellspacing="0">
<tr>
<th>序号</th>
<th>ID</th>
<th>姓名</th>
<th>语文</th>
<th>英语</th>
</tr>
<c:choose>
<c:when test="${not empty requestScope.page.data}">
<c:forEach items="${requestScope.page.data }" var="item" varStatus="indexObj">
<tr>
<td>${indexObj.index}</td>
<td>${item.id}</td>
<td>${item.name}</td>
<td>${item.chinese}</td>
<td>${item.english}</td>
</tr>
</c:forEach>
</c:when>
</c:choose>
<tr>
<th colspan="5" align="center">
<a href="${pageContext.request.contextPath}/list?currentPage=1">首页</a>
<a href="${pageContext.request.contextPath}/list?currentPage=${page.currentPage-1}">上一页</a>
<a href="${pageContext.request.contextPath}/list?currentPage=${page.currentPage+1}">下一页</a>
<a href="${pageContext.request.contextPath}/list?currentPage=${page.totalPage}">末页</a>
</th>
</tr>
</table>
</body>
</html>
二、展示效果