1,前端和后端之间约定的信息应该是PageSize、PageNum。
2,后端在分页之前先查询一把数据总数。
3,基于数据总数、PageSize以及PageNum就可以计算出MySQL中需要的Limit和OffSet值。
代码示例:
一,查询基类
package com.freedom.dal.common;
/**
class QueryBase {
//数据总量
private Integer totalSize;
//每页大小
private Integer pageSize;
//当前页数
private Integer currentPage;
// for paging
private int startRow;
private int endRow;
protected Integer getDefaultPageSize() {
return defaultPageSize;
}
public boolean isFirstPage() {
return this.getCurrentPage().intValue() == 1;
}
public int getPreviousPage() {
int back = this.getCurrentPage().intValue() - 1;
if (back <= 0) {
back = 1;
}
return back;
}
public boolean isLastPage() {
return this.getTotalPage() == this.getCurrentPage().intValue();
}
public int getNextPage() {
int back = this.getCurrentPage().intValue() + 1;
if (back > this.getTotalPage()) {
back = this.getTotalPage();
}
return back;
}
public Integer getCurrentPage() {
if (currentPage == null) {
return defaultFristPage;
}
return currentPage;
}
public void setCurrentPageString(String pageString) {
if (isBlankString(pageString)) {
this.setCurrentPage(defaultFristPage);
}
try {
Integer integer = new Integer(pageString);
this.setCurrentPage(integer);
} catch (NumberFormatException ignore) {
this.setCurrentPage(defaultFristPage);
}
}
public void setCurrentPage(Integer cPage) {
if ((cPage == null) || (cPage.intValue() <= 0)) {
this.currentPage = null;
} else {
this.currentPage = cPage;
}
setStartEndRow();
}
private void setStartEndRow() {
this.startRow = this.getPageSize().intValue() * (this.getCurrentPage().intValue() - 1) + 1;
this.endRow = this.startRow + this.getPageSize().intValue() - 1;
}
public Integer getPageSize() {
if (pageSize == null) {
return getDefaultPageSize();
}
return pageSize;
}
public boolean hasSetPageSize() {
return pageSize != null;
}
public void setPageSizeString(String pageSizeString) {
if (isBlankString(pageSizeString)) {
return;
}
try {
Integer integer = new Integer(pageSizeString);
this.setPageSize(integer);
} catch (NumberFormatException ignore) {
}
}
private boolean isBlankString(String pageSizeString) {
if (pageSizeString == null) {
return true;
}
if (pageSizeString.trim().length() == 0) {
return true;
}
return false;
}
public void setPageSize(Integer pSize) {
if ((pSize == null) || (pSize.intValue() <= 0)) {
this.pageSize = null;
} else {
this.pageSize = pSize;
}
setStartEndRow();
}
public Integer getTotalItem() {
if (totalSize == null) {
return defaultTotleItem;
}
return totalSize;
}
public void setTotalItem(Integer tItem) {
if (tItem == null) {
throw new IllegalArgumentException("TotalItem can't be null.");
}
this.totalSize = tItem;
int current = this.getCurrentPage().intValue();
int lastPage = this.getTotalPage();
if (current > lastPage) {
this.setCurrentPage(new Integer(lastPage));
}
}
public int getTotalPage() {
int pgSize = this.getPageSize().intValue();
int total = this.getTotalItem().intValue();
int result = total / pgSize;
if ((total == 0) || ((total % pgSize) != 0)) {
result++;
}
return result;
}
public int getPageFristItem() {
int cPage = this.getCurrentPage().intValue();
if (cPage == 1) {
return 1;
}
cPage--;
int pgSize = this.getPageSize().intValue();
return (pgSize * cPage) + 1;
}
public int getPageLastItem() {
int cPage = this.getCurrentPage().intValue();
int pgSize = this.getPageSize().intValue();
int assumeLast = pgSize * cPage;
int totalItem = getTotalItem().intValue();
if (assumeLast > totalItem) {
return totalItem;
} else {
return assumeLast;
}
}
public int getEndRow() {
return endRow;
}
public void setEndRow(int endRow) {
this.endRow = endRow;
}
public int getStartRow() {
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
protected String getSQLBlurValue(String value) {
if (value == null) {
return null;
}
return value + '%';
}
public boolean nextPage() {
if(this.currentPage != null && this.currentPage.intValue() >= this.getTotalPage()) return false;
if(this.currentPage == null) {
this.setCurrentPage(defaultFristPage);
} else {
this.setCurrentPage(getNextPage());
}
return true;
}
public int getStart() {
int startRow = getStartRow() - 1;
return startRow > 0 ? startRow : 0;
}
}
二,DAO中基本逻辑
1,生成查询基类,并将前端传递过来的pageSize和pageNum设置给查询基类。
2,查询数据总量,将数据总量设置到查询基类中。
3,查询。
三,xml配置
SELECT
<include refid="ConfigDOColums"></include>
FROM
CONFIGS
WHERE
<include refid="configQueryConditions"></include>
LIMIT #{start}, #{pageSize}
创建一个分页类pageBean,所有的类可以继承它,在你的Action或者Controller里面,全部将实体类参数全部封装到Map,mybatis的参数全部采用Map传递比较好,然后写一个通用的sql头和sql尾,把你的业务逻辑sql都写在头和尾之间,就可以了。这也是一个笨方法,看你怎么写了
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。