在javaweb中使用分頁查詢的詳細步驟


首先在原有的數據庫實體bean上在建立一個分頁實體bean

/**
* 用於展示分頁數據的Javabean對象
* @author Lenovo
*
*/
public class PageenationBean {

private Integer currPage;//當前頁數

private Integer totalPage;//總頁數

private List<UserBean> dataList;//table的數據

public Integer getCurrPage() {
return currPage;
}

public void setCurrPage(Integer currPage) {
this.currPage = currPage;
}

public Integer getTotalPage() {
return totalPage;
}

public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}

public List<UserBean> getDataList() {
return dataList;
}

public void setDataList(List<UserBean> dataList) {
this.dataList = dataList;
}
}

然后在持久層和業務層書寫分頁查詢的方法

持久層:

public interface IUserDao {

/**
* 獲取總條數
* @return
*/
public int getTotalCount();
/**
* 通過起始索引獲取數據
* @param StartIndex
* @return
*/
public List<UserBean> getUserListByStartIndex(int StartIndex);

業務層:

public interface IUserService {


/**
* 獲取總頁數
* @return
*/
public int getTotalPage();

/**
* 通過起始索引獲取數據
* @param StartIndex
* @return
*/
public List<UserBean> getUserListByCurrPage(int currPage);

然后實現接口方法

持久類接口實現:

public class UserDaoImpl extends BaseDao implements IUserDao{

@Override
public int getTotalCount() {
this.setConnection();
int totalCount = 0;
try {
ps = con.prepareStatement("select count(*) from stuscore");
rs = ps.executeQuery();
if(rs.next()) {
totalCount =rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
this.closeConnection();
return totalCount;
}

@Override
public List<UserBean> getUserListByStartIndex(int StartIndex) {
List<UserBean> list = new ArrayList<UserBean>();
this.setConnection();
try {
ps = con.prepareStatement("select * from stuscore limit ?,5");
ps.setInt(1, StartIndex);
rs=ps.executeQuery();
while(rs.next()) {
UserBean user=new UserBean();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setGrade(rs.getString("grade"));
user.setSex(rs.getString("sex"));
user.setAge(rs.getInt("age"));
user.setScore(rs.getInt("score"));
list.add(user);
}

} catch (Exception e) {
e.printStackTrace();
}finally {
this.closeConnection();
}
return list;
}

業務類接口實現:

public class UserServiceImpl implements IUserService{

private IUserDao dao= new UserDaoImpl();
@Override
public int getTotalPage() {
//得到數據總條數
int totalCount= dao.getTotalCount();
//計算總頁數公式:totalPage = (totalCount + pageSize -1)/pageSize
int totalPage = (totalCount+5-1)/5;
return totalPage;
}

@Override
public List<UserBean> getUserListByCurrPage(int currPage) {
//通過當前頁計算起始索引
int startIndex = (currPage-1)*5;
//通過起始索引查詢數據
List<UserBean> userList =dao.getUserListByStartIndex(startIndex);
return userList;
}

然后 書寫servlet方法

@WebServlet("/ShowUserListServlets")
public class ShowUserListServlets extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//動態獲取當前第幾頁並做計算
String operation = request.getParameter("operation");
String currPageStr = request.getParameter("currPage");
int currPage = 1;
//調取業務層查詢數據
IUserService service= new UserServiceImpl();
//獲取總頁數
int totalPage = service.getTotalPage();
if(currPageStr == null) {
currPage = 1;
}else if("首頁".equals(operation)){
currPage = 1;
}else if("上一頁".equals(operation)) {
currPage = Integer.parseInt(currPageStr)-1;
if(currPage <= 0) {
currPage = 1;
}
}else if("下一頁".equals(operation)) {
currPage = Integer.parseInt(currPageStr)+1;
if(currPage >= totalPage) {
currPage = totalPage;
}
}

List<UserBean> userList = service.getUserListByCurrPage(currPage);

//構建javabean對象-PagenationBean
PageenationBean pagenationBean = new PageenationBean();
pagenationBean.setCurrPage(currPage);
pagenationBean.setTotalPage(totalPage);
pagenationBean.setDataList(userList);

//放入作用域
request.setAttribute("page", pagenationBean);
request.getRequestDispatcher("userList.jsp").forward(request, response);
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

最后用將jsp作為顯示

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<table border="2" style="color:red,border-collapse: collapse;">
<tr>
<th>學號</th>
<th>姓名</th>
<th>性別</th>
<th>年齡</th>
</tr>
<c:forEach items="${page.dataList }" var="u">
<tr>
<td>${u.id }</td>
<td>${u.name }</td>
<td>${u.sex}</td>
<td>${u.age}</td>
</tr>
</c:forEach>
</table>
<h3>
第${page.currPage }頁/共${page.totalPage }頁
</h3>
<br>
<form action="ShowUserListServlets" method="get">
<input type="submit" value="首頁" name="operation">
<input type="submit" value="上一頁" name="operation">
<input type="submit" value="下一頁" name="operation">
<input type="submit" value="尾頁" name="operation">
<%--隱藏表單域作用,記錄當前是第幾頁 --%>
<input type="hidden" name="currPage" value="${page.currPage }" >
<hr>
班級:<input type="text" name="class"><br>
性別:<input type="text" name="sex"><br>
成績段:
<input type="text" name="min">至
<input type="text" name="max"><br>
<input type="submit" value="查詢">
</form>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM