1. 程式人生 > 其它 >條件查詢_JSP多條件查詢分頁

條件查詢_JSP多條件查詢分頁

技術標籤:條件查詢

8e86007497cdea6a4faa587b88ee52a9.png

DAO資料訪問

package dao;​import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanListHandler;import org.apache.commons.dbutils.handlers.ScalarHandler;​import utils.JdbcUtils;import utils.PageBean;import entity.Muli;​public class MuliDao { // 建立QueryRunner物件 private QueryRunner qr = JdbcUtils.getQueryRuner();​  // 查詢全部資訊列表 public List listMuliAll() { String sql = "SELECT * FROM mulihuji "; try { // 多行資料,封裝成物件的集合 List listmuli = qr.query(sql, new BeanListHandler(Muli.class)); return listmuli; } catch (Exception e) { throw new RuntimeException(e); } }  // 分頁顯示全部資料 public void listMuliAll(PageBean pb,Muli muli) throws SQLException {​ String countsql = "select count(*) from mulihuji where 1=1"; String sql = "select * from mulihuji where 1=1";  List params = new ArrayList(); // 新增查詢條件 String xm = muli.getXm(); if (xm != null && xm != "") { countsql += " and xm like ?"; sql += " and xm like ?"; params.add("%" + xm + "%"); } String sfzh = muli.getSfzh(); if (sfzh != null && sfzh != "") { countsql += " and sfzh like ?"; sql += " and sfzh like ?"; params.add("%" + sfzh + "%"); }   //得到總記錄數  Long count = qr.query(countsql, new ScalarHandler(),params.toArray()); pb.setTotalCount(count.intValue()); /* * 問題: jsp頁面,如果當前頁為首頁,再點選上一頁報錯! 如果當前頁為末頁,再點下一頁顯示有問題! 解決: 1. 如果當前頁 <= 0; * 當前頁設定當前頁為1; 2. 如果當前頁 > 最大頁數; 當前頁設定為最大頁數 */ // 判斷 if (pb.getCurrentPage() <= 0) { pb.setCurrentPage(1); // 把當前頁設定為1 } else if (pb.getCurrentPage() > pb.getTotalPage()) { pb.setCurrentPage(pb.getTotalPage()); // 把當前頁設定為最大頁數 }​ // 1. 獲取當前頁: 計算查詢的起始行、返回的行數 int currentPage = pb.getCurrentPage(); int index = (currentPage - 1) * pb.getPageCount(); // 查詢的起始行 int pagecount = pb.getPageCount(); // 每頁的行數   sql += " limit ?,?"; params.add(index); params.add(pagecount);  List pageData = qr.query(sql, new BeanListHandler(Muli.class),params.toArray() ); //獲取一頁的資料 // 設定到pb物件中 pb.setPageData(pageData); ​ }​ }​

Servlet設計

package servlet;​import java.io.IOException;import java.sql.SQLException;​import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;​​import dao.MuliDao;import entity.Muli;import utils.PageBean;​/** * Servlet implementation class QueryListMuliServlet */@WebServlet("/querymuli")public class QueryListMuliServlet extends HttpServlet { private static final long serialVersionUID = 1L;​ /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String xm = request.getParameter("xm"); String sfzh = request.getParameter("sfzh"); //例項化實體類並賦值  Muli muli=new Muli(); muli.setXm(xm); muli.setSfzh(sfzh);  //1. 獲取“當前頁”引數; (第一次訪問當前頁為null)  String currPage = request.getParameter("currentPage"); // 判斷 if (currPage == null || "".equals(currPage.trim())){ currPage = "1"; // 若第一次訪問,設定當前頁為1; } // 轉換為整數型 int currentPage = Integer.parseInt(currPage); //例項化pageben類 PageBean pb = new PageBean(); pb.setCurrentPage(currentPage);  try { //呼叫資料訪問的方法,傳遞兩個引數 MuliDao mulidao = new MuliDao(); mulidao.listMuliAll(pb,muli);  request.setAttribute("pageBean