分頁查詢
-
減少服務器內存開銷
-
提高用戶體驗
效果圖
思緒圖
分頁顯示Bean文件代碼
package cn.ytmj.findlist.domain;
import java.util.List;
/**
* @author rui
* @create 2019-08-17 23:34
* 分頁對象
* 使用泛型為多種頁面提供服務
*/
public class PageBean<T> {
private int totalCount; //總記錄數
private int totalPage; // 總頁數
private List<T> list; //每頁的數據list集合
private int currentPage; //當前頁碼
private int rows; //每頁顯示的條數
public PageBean(){}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
@Override
public String toString() {
return "PageBean{" +
"totalCount=" + totalCount +
", totalPage=" + totalPage +
", list=" + list +
", currentPage=" + currentPage +
", rows=" + rows +
'}';
}
}
FindUserByPageServlet代碼
@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取數據
String currentPage = request.getParameter("currentPage");
String rows = request.getParameter("rows");
if(null==currentPage||"".equals(currentPage)){
currentPage="1";
}
if(null==rows||"".equals(rows)){
rows="5";
}
//調用service
UserService userService = new UserServiceImpl();
PageBean<User> pageBean = userService.findUserByPage(Integer.parseInt(currentPage), Integer.parseInt(rows));
request.setAttribute("pageBean", pageBean);
//轉發
request.getRequestDispatcher("/list.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
}
service
PageBean<User> findUserByPage(int currentPage, int rows);
serviceimpl
public class UserServiceImpl implements UserService {
UserDao userDao = new UserDaoImpl();
@Override
public PageBean<User> findUserByPage(int currentPage, int rows) {
PageBean<User> pageBean = new PageBean<>();
if (currentPage <= 0) {
currentPage = 1;
}
int totalCount = userDao.findTotalCount();
//計算總頁數
int totalPage = totalCount % ows == 0 ? totalCount / rows : totalCount / rows + 1;
pageBean.setTotalPage(totalPage);
if (currentPage > totalPage) {
currentPage = totalPage;
}
List<User> list = userDao.findUserByPage(currentPage, rows);
pageBean.setTotalCount(totalCount);
pageBean.setCurrentPage(currentPage);
pageBean.setRows(rows);
pageBean.setList(list);
return pageBean;
}
}
dao
//查詢當前頁面的所有數據
List<User> findUserByPage(int currentPage, int rows);
//總條數
int findTotalCount();
daoimpl
-
通過JDBCUtils獲取DataSource
package cn.ytmj.findlist.util; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; /** * JDBC工具類 使用Durid連接池 */ public class JDBCUtils { private static DataSource ds ; static { try { //1.加載配置文件 Properties pro = new Properties(); //使用ClassLoader加載配置文件,獲取字節輸入流 InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); pro.load(is); //2.初始化連接池對象 ds = DruidDataSourceFactory.createDataSource(pro); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 獲取連接池對象 */ public static DataSource getDataSource(){ return ds; } /** * 獲取連接Connection對象 */ public static Connection getConnection() throws SQLException { return ds.getConnection(); } }
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public List<User> findUserByPage(int currentPage, int rows) {
String sql = "select * from user limit ? , ? ";
List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class), (currentPage - 1) * rows, rows);
return list;
}
@Override
public int findTotalCount() {
String sql = "select count(*) from user";
int count = jdbcTemplate.queryForObject(sql,Integer.class);
return count;
}
}
jsp頁面分頁顯示相關代碼
Bootstrap分頁按鈕模板(輕微修改),以備后用
<div style="float: left">
<nav>
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li class="active"><a href="#">1 <span class="sr-only"></span></a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
<span style="font-size: 25px ;margin-left: 5px">共16條數據,共4頁</span>
</ul>
</nav>
</div>
修改后jsp代碼
<div style="float: left">
<nav>
<ul class="pagination">
<%-- 判斷是否是第一頁--%>
<c:if test="${pageBean.currentPage==1}">
<li class="disabled">
</c:if>
<c:if test="${pageBean.currentPage!=1}">
<li>
</c:if>
<a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pageBean.currentPage-1}&rows=5"
aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<c:forEach var="i" varStatus="s" step="1" begin="1" end="${pageBean.totalPage}">
<c:if test="${pageBean.currentPage == i}">
<li class="active">
<a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5"
name="li">${i}</a></li>
</c:if>
<c:if test="${pageBean.currentPage != i}">
<li>
<a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5"
name="li">${i}</a></li>
</c:if>
</c:forEach>
<%-- 判斷是否是最后頁--%>
<c:if test="${pageBean.currentPage >= pageBean.totalPage}">
<li class="disabled">
</c:if>
<c:if test="${pageBean.currentPage!=pageBean.totalPage}">
<li>
</c:if>
<a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pageBean.currentPage+1}&rows=5"
aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
<span style="font-size: 25px ;margin-left: 5px">共${pageBean.totalCount}條數據,共${pageBean.totalPage}頁</span>
</ul>
</nav>
</div>
</div>
jsp+servlet實現登錄,數據操作(分頁查詢,模糊查詢等)githubhttps://github.com/PoetryAndYou/List-Operation