十一、分页项目--MySql

一、分页项目

创建数据库的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; 
	
	//每页显示的条数
	//默认显示4行
	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) {
		
		//1.获取当前页
		int index = (page.getCurrentPage()-1)*page.getPageCount();
		int count = page.getPageCount();
		
		//2.查询总记录数
		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();
		}
		
		
		//3.查询当前页数据
		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");
			
			//1.第一次获取数据时,currentPage为空
			if(currentPage==null||"".equals(currentPage)) {
				currentPage="1";
			}
			//转换
			int  currPage=Integer.valueOf(currentPage);
			
		
			//2.获取数据
			PageBean<StudentEntity> page = service.getAll(currPage);
			
			//3.设置数据
			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 {
	
	
	/**
	 * 1.初始化C3P0连接池
	 */
	private static ComboPooledDataSource dataSource;
	
	static {
		dataSource=new ComboPooledDataSource();
	}
	
	/**
	 * 2.创建DBUtils的核心工具类对象
	 * 
	 * 
	 * @return
	 */
	public static QueryRunner getQueryRunner() {
		
		//创建DBUtils的核心对象。
		//在创建QueryRunner对象时,如果传入了数据源对象
		//那么在使用QueryRunner的方法时,就不用再传入连接对象、
		//会自动从数据源中获取连接。(同时也不用手动关闭连接,它会自动关闭连接资源)
		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>

	<!-- 可以使用多数据源
	<named-config name="db2">
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</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>
	</named-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>

二、展示效果

在这里插入图片描述

发布了94 篇原创文章 · 获赞 0 · 访问量 608

猜你喜欢

转载自blog.csdn.net/weixin_45602227/article/details/104383349