1,先在網上看了大量的資料覺得均不靠譜。PM說只是需要實現一個簡單的分頁功能就OK,直接在sql里面實現即可
2,首先是mybatis映射文件的配置問題
因為mybatis不能直接解析xml里面的<=,而在oracle里面暫且想到的sql語句需要用到,經過調試
實現的xml映射文件:
<select id="" parameterType="" resultType="" resultMap="">
Select ROWNUM,ID,NAME FROM(Select ROWNUM as ROWNO, ID,NAME from CHANGED_CONTENT
<where>
<![CDATA[ROWNUM <= #{endRow}]]>
</where>
)
<where>
<![CDATA[ROWNO > #{startRow}]]>
</where>
</select>
3,下面就是Pager和PagerHelp類
Pager:
public class Pager {
private int totalRows; // 總行數
private int pageSize = 1; // 每頁顯示的行數
private int currentPage; // 當前頁號
private int totalPages; // 總頁數
private int startRow; // 當前頁在數據庫中的起始行
private int endRow; //結束行 此為oracle查詢需要增加
public Pager() {
}
public Pager(int _totalRows) {
totalRows = _totalRows;
totalPages = totalRows / pageSize;
int mod = totalRows % pageSize;
if (mod > 0) {
totalPages++;
}
currentPage = 1;
startRow = 0;
}
public int getStartRow() {
return startRow;
}
public int getTotalPages() {
return totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalRows() {
return totalRows;
}
public void setEndRow(int endRow) {
this.endRow = endRow;
}
public void first() {
currentPage = 1;
startRow = 0;
}
public void previous() {
if (currentPage == 1 || currentPage == 0) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
}
public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
}
public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
}
}
}
PageHelper:
public class PagerHelper {
public static Pager getPager(HttpServletRequest httpServletRequest,int totalRows) {
//定義pager對象,用於傳到頁面
Pager pager = new Pager(totalRows);
//從Request對象中獲取當前頁號
String currentPage = httpServletRequest.getParameter("currentPage");
//如果當前頁號為空,表示為首次查詢該頁
//如果不為空,則刷新pager對象,輸入當前頁號等信息
if (currentPage != null) {
pager.refresh(Integer.parseInt(currentPage));
}
//獲取當前執行的方法,首頁,前一頁,后一頁,尾頁。
String pagerMethod = httpServletRequest.getParameter("pageMethod");
if (pagerMethod != null) {
if (pagerMethod.equals("first")) {
pager.first();
} else if (pagerMethod.equals("previous")) {
pager.previous();
} else if (pagerMethod.equals("next")) {
pager.next();
} else if (pagerMethod.equals("last")) {
pager.last();
}
}
return pager;
}
}
4,下面就是在spring control里面實現與前台的jsp頁面交互,當然還有service及serviceImpl里面主要是通過映射文件配置的sql返回對應的list
此處略
5,jsp頁面相關代碼:
function first(obj){
var currentPage = obj;
if(1 < currentPage){
var url = "findcontent.do?pageMethod=first¤tPage="+currentPage;
document.all.form1.method = "post";
document.all.form1.action = url;
document.all.form1.submit();
}else{
alert("已經是首頁了");
}
}
function previous(obj){
var currentPage = obj;
if(1 < currentPage){
var url = "findcontent.do?pageMethod=previous¤tPage="+currentPage;
document.all.form1.method = "post";
document.all.form1.action = url;
document.all.form1.submit();
}else{
alert("已經是最前一頁了");
}
}
function next(obj,totalPages){
var currentPage = obj;
if(currentPage < totalPages){
var url = "findcontent.do?pageMethod=next¤tPage="+currentPage;
document.all.form1.method = "post";
document.all.form1.action = url;
document.all.form1.submit();
}else{
alert("已經是最后一頁了");
}
}
function last(obj,totalPages){
var currentPage = obj;
if(currentPage < totalPages){
var url = "findcontent.do?pageMethod=last¤tPage="+currentPage;
document.all.form1.method = "post";
document.all.form1.action = url;
document.all.form1.submit();
}else{
alert("已經是尾頁了");
}
}
<tr>
<td colspan="2">
第<%=pager.getCurrentPage()%>頁 共<%=pager.getTotalPages()%>頁
<a href="#" onclick="first(<%=pager.getCurrentPage()%>)">首頁</a>
<a href="#" onclick="previous(<%=pager.getCurrentPage()%>)">上一頁</a>
<a href="#" onclick="next(<%=pager.getCurrentPage()%>,<%=pager.getTotalPages()%>)">下一頁</a>
<a href="#" onclick="last(<%=pager.getCurrentPage()%>,<%=pager.getTotalPages()%>)">尾頁</a>
</td>
</tr>
至此便實現了此架構下的簡單分頁功能