mybatis+oracle+springMVC实现简单分页
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>
至此便实现了此架构下的简单分页功能
转载于:https://blog.51cto.com/jackal413145701/1144603