分頁技巧道理與完成之Java+Oracle代碼完成分頁(二)。本站提示廣大學習愛好者:(分頁技巧道理與完成之Java+Oracle代碼完成分頁(二))文章只能為提供參考,不一定能成為您想要的結果。以下是分頁技巧道理與完成之Java+Oracle代碼完成分頁(二)正文
緊接著上篇—分頁技巧道理與完成之分頁的意義及辦法(一) ,本篇持續剖析分頁技巧。上篇講的是分頁技巧的簡略道理與引見,這篇深刻剖析一下分頁技巧的代碼完成。
上篇最初講到了分頁的最好完成是在數據庫層停止分頁,並且分歧的數據庫有分歧的分頁完成,好比Oracle是用三層sql嵌套完成分頁的、MySQL是用limit症結字完成的(上篇已講到)。
這篇以Java+Oracle為基本,講授代碼層的完成。
就如日常平凡我們很在分頁中看到的,分頁的時刻前往的不只包含查詢的成果集(List),並且還包含總的頁數(pageNum)、以後第幾頁(pageNo)等等信息,所以我們封裝一個查詢成果PageModel類,代碼以下:
package kane; import java.util.List; public class PageModel<E> { private List<E> list; private int pageNo; private int pageSize; private int totalNum; private int totalPage; public List<E> getList() { return list; } public void setList(List<E> list) { this.list = list; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalNum() { return totalNum; } public void setTotalNum(int totalNum) { this.totalNum = totalNum; setTotalPage((getTotalNum() % pageSize) == 0 ? (getTotalNum() / pageSize) : (getTotalNum() / pageSize + 1)); } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } // 獲得第一頁 public int getFirstPage() { return 1; } // 獲得最初頁 public int getLastPage() { return totalPage; } // 獲得前頁 public int getPrePage() { if (pageNo > 1) return pageNo - 1; return 1; } // 獲得後頁 public int getBackPage() { if (pageNo < totalPage) return pageNo + 1; return totalPage; } // 斷定'首頁'及‘前頁'能否可用 public String isPreable() { if (pageNo == 1) return "disabled"; return ""; } // 斷定'尾頁'及‘下頁'能否可用 public String isBackable() { if (pageNo == totalPage) return "disabled"; return ""; } }
個中應用泛型是為了能使的該分頁類能停止重用,好比在查詢用戶時可以封裝User對象、在查詢財政中的流向單時可以封裝流向單FlowCard類。
我們以查詢用戶為例,用戶選擇查詢前提,起首挪用Servlet獲得查詢參數,然後要求營業邏輯層獲得分頁封裝成果類。營業邏輯挪用Dao層獲得成果集、獲得中記載數封裝成份頁類。最初Servlet將成果設置到jsp頁面顯示。
起首來說解Servlet,代碼以下:
package kane; import java.io.*; import java.util.*; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import kane.UserInfo; import kane.UserInfoManage; import kane.PageModel; public class UserBasicSearchServlet extends HttpServlet { private static final long serialVersionUID = 1L; private int pageSize = 0; @Override public void init(ServletConfig config) throws ServletException { pageSize = Integer.parseInt(config.getInitParameter("pageSize")); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 1.獲得頁面參數並結構參數對象 int pageNo = Integer.parseInt(req.getParameter("pageNo")); String sex = req.getParameter("gender"); String home = req.getParameter("newlocation"); String colleage = req.getParameter("colleage"); String comingyear = req.getParameter("ComingYear"); UserInfo u = new UserInfo(); u.setSex(sex); u.setHome(home); u.setColleage(colleage); u.setCy(comingyear); // 2.挪用營業邏輯獲得成果集 UserInfoManage userInfoManage = new UserInfoManage(); PageModel<UserInfo> pagination = userInfoManage.userBasicSearch(u, pageNo, pageSize); List<UserInfo> userList = pagination.getList(); // 3.封裝前往成果 StringBuffer resultXML = new StringBuffer(); try { resultXML.append("<?xml version='1.0' encoding='gb18030'?>/n"); resultXML.append("<root>/n"); for (Iterator<UserInfo> iterator = userList.iterator(); iterator .hasNext();) { UserInfo userInfo = iterator.next(); resultXML.append("<data>/n"); resultXML.append("/t<id>" + userInfo.getId() + "</id>/n"); resultXML.append("/t<truename>" + userInfo.getTruename() + "</ truename >/n"); resultXML.append("/t<sex>" + userInfo.getSex() + "</sex>/n"); resultXML.append("/t<home>" + userInfo.getHome() + "</home>/n"); resultXML.append("</data>/n"); } resultXML.append("<pagination>/n"); resultXML.append("/t<total>" + pagination.getTotalPage() + "</total>/n"); resultXML.append("/t<start>" + pagination.getFirstPage() + "</start>/n"); resultXML.append("/t<end>" + pagination.getLastPage() + "</end>/n"); resultXML.append("/t<pageno>" + pagination.getPageNo() + "</pageno>/n"); resultXML.append("</pagination>/n"); resultXML.append("</root>/n"); } catch (Exception e) { e.printStackTrace(); } writeResponse(req, resp, resultXML.toString()); } public void writeResponse(HttpServletRequest request, HttpServletResponse response, String result) throws IOException { response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Content-Type", "text/xml; charset=gb18030"); PrintWriter pw = response.getWriter(); pw.write(result); pw.close(); } }
個中User對象代碼以下:
package kane; import java.util.Date; public class UserInfo { private int id; private String username; private String password; private String truename; private String sex; private Date birthday; private String home; private String colleage; private String comingYear; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTruename() { return truename; } public void setTruename(String truename) { this.truename = truename; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getHome() { return home; } public void setHome(String home) { this.home = home; } public String getColleage() { return colleage; } public void setColleage(String colleage) { this.colleage = colleage; } public String getCy() { return comingYear; } public void setCy(String cy) { this. comingYear= cy; } }
接著是營業邏輯層代碼,代碼以下:
package kane; import java.sql.Connection; import kane.DBUtility; import kane.PageModel; public class UserInfoManage { private UserInfoDao userInfoDao = null; public UserInfoManage () { userInfoDao = new UserInfoDao(); } public PageModel<UserInfo> userBasicSearch(UserInfo u, int pageNo, int pageSize) throws Exception { Connection connection = null; PageModel<UserInfo> pagination = new PageModel<UserInfo>(); try { connection = DBUtility.getConnection(); DBUtility.setAutoCommit(connection, false); pagination.setList(userInfoDao.getUserList(u, pageNo, pageSize)); pagination.setPageNo(pageNo); pagination.setPageSize(pageSize); pagination.setTotalNum(userInfoDao.getTotalNum(u)); DBUtility.commit(connection); } catch (Exception e) { DBUtility.rollBack(connection); e.printStackTrace(); throw new Exception(); } finally { DBUtility.closeConnection(); } return pagination; } }
個中DBUtility為數據庫的銜接封裝類。
最初是Dao層代碼完成,代碼以下:
package kane; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import kane.UserInfo; import kane.DBUtility; public class UserInfoDao { public List<UserInfo> getUserList(UserInfo userInfo, int pageNo, int pageSize) throws Exception { PreparedStatement pstmt = null; ResultSet rs = null; List<UserInfo> userList = null; try { String sql = "select * from(select rownum num,u.* from(select * from user_info where sex = ? and home like '" + userInfo.getHome() + "%" + "' and colleage like '" + userInfo.getColleage() + "%" + "' and comingyear like '" + userInfo.getCy() + "%" + "' order by id) u where rownum<=?) where num>=?"; userList = new ArrayList<UserInfo>(); Connection conn = DBUtility.getConnection(); pstmt = conn.prepareStatement(sql); pstmt.setString(1, userInfo.getSex()); pstmt.setInt(2, pageNo * pageSize); pstmt.setInt(3, (pageNo - 1) * pageSize + 1); rs = pstmt.executeQuery(); while (rs.next()) { UserInfo user = new UserInfo(); user.setId(rs.getInt("id")); user.setTruename(rs.getString("truename")); user.setSex(rs.getString("sex")); user.setHome(rs.getString("home")); userList.add(user); } } catch (SQLException e) { e.printStackTrace(); throw new Exception(e); } finally { DBUtility.closeResultSet(rs); DBUtility.closePreparedStatement(pstmt); } return userList; } public int getTotalNum(UserInfo userInfo) throws Exception { PreparedStatement pstmt = null; ResultSet rs = null; int count = 0; try { String sql = "select count(*) from user_info where sex=? and home like '" + userInfo.getHome() + "%" + "' and colleage like '" + userInfo.getColleage() + "%" + "' and comingyear like '" + userInfo.getCy()+ "%" + "'"; Connection conn = DBUtility.getConnection(); pstmt = conn.prepareStatement(sql); pstmt.setString(1, userInfo.getSex()); rs = pstmt.executeQuery(); if (rs.next()) { count = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); throw new Exception(e); } finally { DBUtility.closeResultSet(rs); DBUtility.closePreparedStatement(pstmt); } return count; } }
最初就是servlet將獲得的成果前往給jsp頁面顯示出來。
注:個中DBUtility代碼是封裝數據庫銜接操作的代碼,以下:
package kane; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class DBUtility { private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>(); public static Connection getConnection() { Connection conn = null; conn = threadLocal.get(); if (conn == null) { try { Class.forName("oracle.jdbc.driver.OracleDriver"); conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:oracle", "admin", "admin"); threadLocal.set(conn); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } return conn; } // 封裝設置Connection主動提交 public static void setAutoCommit(Connection conn, Boolean flag) { try { conn.setAutoCommit(flag); } catch (SQLException e) { e.printStackTrace(); } } // 設置事務提交 public static void commit(Connection conn) { try { conn.commit(); } catch (SQLException e) { e.printStackTrace(); } } // 封裝設置Connection回滾 public static void rollBack(Connection conn) { try { conn.rollback(); } catch (SQLException e) { e.printStackTrace(); } } // 封裝封閉Connection、PreparedStatement、ResultSet的函數 public static void closeConnection() { Connection conn = threadLocal.get(); try { if (conn != null) { conn.close(); conn = null; threadLocal.remove(); } } catch (SQLException e) { e.printStackTrace(); } } public static void closePreparedStatement(PreparedStatement pstmt) { try { if (pstmt != null) { pstmt.close(); pstmt = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void closeResultSet(ResultSet rs) { try { if (rs != null) { rs.close(); rs = null; } } catch (SQLException e) { e.printStackTrace(); } } }
應用ThreadLocal是為了包管事務的分歧,使得統一個線程的一切數據庫操作應用統一個Connection。
到此一個簡略的代碼完成就完成了。
以上就是本文的全體內容,願望對年夜家的進修有所贊助,也願望年夜家多多支撐。