一、环境搭建
客户端(发送请求)=> Servlet(处理请求,1、指定处理Service 2、指定对应页面) => Service(业务处理。1、调用具体Dao并对返回数据做对应处理) => Dao(数据库操作。单一增删改查)
基本结构如下图
二、代码实现分页
1、Sql语句(Sql Server 2012及以上)
SELECT * FROM 表名 order by 排序字段 OFFSET (当前页-1)*页容量 ROWS FETCH next 页容量 rows only
2、domain包用于存储数据实体结构
2.1、PageBean.java
1 /** 2 * PageBean<T>,泛型可以提高其复用性 3 * 用于分页: 4 * 1、页容量(PageSize) 5 * 2、当前页面(PageIndex) 6 * 3、总页数(PageCount) 7 * 4、总条数(PageTotal) 8 * 5、分页数据集合(List) 9 */ 10 public class PageBean<T> { 11 private int PageSize; 12 private int PageIndex; 13 private int PageCount; 14 private int PageTotal; 15 private List<T> list; 16 public int getPageSize() { 17 return PageSize; 18 } 19 public void setPageSize(int pageSize) { 20 PageSize = pageSize; 21 } 22 public int getPageIndex() { 23 return PageIndex; 24 } 25 public void setPageIndex(int pageIndex) { 26 PageIndex = pageIndex; 27 } 28 public int getPageCount() { 29 return PageCount; 30 } 31 public void setPageCount(int pageCount) { 32 PageCount = pageCount; 33 } 34 public int getPageTotal() { 35 return PageTotal; 36 } 37 public void setPageTotal(int pageTotal) { 38 PageTotal = pageTotal; 39 } 40 public List<T> getList() { 41 return list; 42 } 43 public void setList(List<T> list) { 44 this.list = list; 45 } 46 }
2.2、UserInfo
1 public class UserInfo { 2 private String UName; 3 private Date SubTime;//java.util.Date 4 private String Remark; 5 public String getUName() { 6 return UName; 7 } 8 public void setUName(String uname) { 9 UName = uname; 10 } 11 public Date getSubTime() { 12 return SubTime; 13 } 14 public void setSubTime(Date subTime) { 15 SubTime = subTime; 16 } 17 public String getRemark() { 18 return Remark; 19 } 20 public void setRemark(String remark) { 21 Remark = remark; 22 } 23 }
3、dao.impl包实现dao包中的接口
1 public class UserInfoDaoImpl implements IUserInfoDao { 2 3 /** 4 * 获取分页数据 5 */ 6 @Override 7 public List<UserInfo> getUserInfoByPageIndex(int p_intPageIdex) throws SQLException { 8 QueryRunner runner= new QueryRunner(JDBCUtil.getDataSource()); 9 return runner.query("select * from UserInfo order by Id offset ? row fetch next ? row only", 10 new BeanListHandler<UserInfo>(UserInfo.class), (p_intPageIdex-1)*PAGE_SIZE,PAGE_SIZE); 11 } 12 13 /** 14 * 获取总记录数 15 */ 16 @Override 17 public int getUserInfoCount() throws SQLException { 18 QueryRunner runner= new QueryRunner(JDBCUtil.getDataSource()); 19 Long countLong= (Long) runner.query("select Count(*) from UserInfo",new ScalarHandler()); 20 return countLong.intValue();//Long类型获取int类型的值 21 } 22 }
4、service.impl包实现service包中的接口
1 public class UserInfoServiceImpl implements IUserInfoService { 2 3 @Override 4 public PageBean<UserInfo> getPageList(int p_intPageIndex) throws SQLException { 5 PageBean<UserInfo> bean=new PageBean<UserInfo>(); 6 bean.setPageIndex(p_intPageIndex); 7 int pageSize=IUserInfoDao.PAGE_SIZE; 8 bean.setPageSize(pageSize); 9 IUserInfoDao dao=new UserInfoDaoImpl(); 10 bean.setList(dao.getUserInfoByPageIndex(p_intPageIndex)); 11 int count=dao.getUserInfoCount(); 12 bean.setPageCount(count); 13 14 //200,10 20 15 //201,10 11 16 bean.setPageTotal(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1); 17 return bean; 18 } 19 }
5、servlet调用service.impl中的方法
1 public class UserInfoServlect extends HttpServlet { 2 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 try { 6 7 int intPageIndex = 1; 8 if(req.getParameter("pageIndex")!=null) { 9 intPageIndex=Integer.parseInt(req.getParameter("pageIndex")); 10 } 11 IUserInfoService service=new UserInfoServiceImpl(); 12 PageBean<UserInfo> bean= service.getPageList(intPageIndex); 13 req.setAttribute("bean", bean); 14 req.getRequestDispatcher("Index.jsp").forward(req, resp); 15 } catch (SQLException e) { 16 17 e.printStackTrace(); 18 } 19 } 20 21 @Override 22 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 23 24 doGet(req, resp); 25 } 26 }
6、创建jsp文件
6.1、导入标签库
把jstl包复制到工程lib目录下,使用<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>此代码将jstl标签库导入到当前页面
注意:Index.jsp文件是创建在WebContent目录下
6.2、完整代码
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <TABLE border="1" width="700"> 12 <TR> 13 <TD>姓名</TD> 14 <TD>时间</TD> 15 <TD>备注</TD> 16 </TR> 17 <c:forEach items="${bean.list }" var="userInfo"> 18 <TR> 19 <TD>${userInfo.UName }</TD> 20 <!-- 根据 JavaBeans 规范,属性的前两个字母不能是一大一小,或者是一小一大。userInfo.SubTime不可这样写--> 21 <TD>${userInfo.subTime }</TD> 22 <TD>${userInfo.remark }</TD> 23 </TR> 24 </c:forEach> 25 <tr> 26 <td colspan="3"> 27 第 ${bean.pageIndex } / ${bean.pageTotal } 28 29 每页显示${bean.pageSize }条 30 总的记录数${bean.pageTotal } 31 <c:if test="${bean.pageIndex !=1 }"> 32 <a href="UserInfoServlet?pageIndex=1">首页</a> 33 | <a href="UserInfoServlet?pageIndex=${bean.pageIndex-1 }">上一页</a> 34 </c:if> 35 36 <c:forEach begin="1" end="${bean.pageTotal }" var="i"> 37 <c:if test="${bean.pageIndex == i }"> 38 ${i } 39 </c:if> 40 <c:if test="${bean.pageIndex != i }"> 41 <a href="UserInfoServlet?pageIndex=${i }">${i }</a> 42 </c:if> 43 44 </c:forEach> 45 46 47 <c:if test="${bean.pageIndex !=bean.pageTotal }"> 48 <a href="UserInfoServlet?pageIndex=${bean.pageIndex+1 }">下一页</a> | 49 <a href="UserInfoServlet?pageIndex=${bean.pageTotal }">尾页</a> 50 </c:if> 51 </td> 52 </tr> 53 </TABLE> 54 </body> 55 </html>
7、页面效果
三、总结
1、Service层与Dao层区别,Dao层只做单一的数据库操作,Service中一个方法可以执行多个Dao操作,可做复杂业务逻辑处理(分页)
2、开发中基本的环境架构 Servlet、Service、Dao
3、页面跳转及传值,四个作用域pageScope、requestScope、sessionScope、applicationScope。
3.1、request.setAttribute("bean", bean);//作用域赋值
3.2、request.getRequestDispatcher("Index.jsp").forward(req, resp);//请求转发,一次请求
4、JavaBeans 规范,属性的前两个字母不能是一大一小,或者是一小一大