springmvc+jdbc分页功能
工具类 SjPagination
import java.util.List;
import com.oceansoft.uums.common.constant.Constant;
public class SjPagination {
private List list; // 对象记录结果集
private int totalRows = 0; // 总记录数
private int pageSize = 5; // 每页显示记录数
private int totalPage = 1; // 总页数
private int pageIndex = 1; // 当前页
private boolean isFirstPage = false; // 是否为第一页
private boolean isLastPage = false; // 是否为最后一页
private boolean isPreviousPage = false; // 是否有前一页
private boolean isNextPage = false; // 是否有下一页
// private boolean isHasPreviousPage = false; // 是否有前一页
// private boolean isHasNextPage = false; // 是否有下一页
private int navigatePages = Constant.NAVIGATE_PAGES; // 导航页码数
private int[] navigatePageNumbers; // 所有导航页号
public SjPagination(int totalRows, int pageIndex) {
init(totalRows, pageIndex, pageSize);
}
public SjPagination(int totalRows, int pageIndex, int pageSize) {
init(totalRows, pageIndex, pageSize);
}
public SjPagination(List list, int totalRows, int pageIndex) {
super();
init(totalRows, pageIndex, pageSize);
this.list = list;
this.totalRows = totalRows;
this.pageIndex = pageIndex;
}
public SjPagination(List list, int totalRows, int pageSize, int pageIndex) {
super();
init(totalRows, pageIndex, pageSize);
this.list = list;
this.totalRows = totalRows;
this.pageSize = pageSize;
this.pageIndex = pageIndex;
}
private void init(int totalRows, int pageIndex, int pageSize) {
// 设置基本参数
this.totalRows = totalRows;
this.pageSize = pageSize;
this.totalPage = (this.totalRows - 1) / this.pageSize + 1;
// 根据输入可能错误的当前号码进行自动纠正
if (pageIndex < 1) {
this.pageIndex = 1;
} else if (pageIndex > this.totalPage) {
this.pageIndex = this.totalPage;
} else {
this.pageIndex = pageIndex;
}
// 基本参数设定之后进行导航页面的计算
calcNavigatePageNumbers();
// 以及页面边界的判定
judgePageBoudary();
}
private void calcNavigatePageNumbers() {
// 当总页数小于或等于导航页码数时
if (totalPage <= navigatePages) {
navigatePageNumbers = new int[totalPage];
for (int i = 0; i < totalPage; i++) {
navigatePageNumbers[i] = i + 1;
}
} else { // 当总页数大于导航页码数时
navigatePageNumbers = new int[navigatePages];
int startNum = pageIndex - navigatePages / 2;
int endNum = pageIndex + navigatePages / 2;
if (startNum < 1) {
startNum = 1;
// (最前navPageCount页
for (int i = 0; i < navigatePages; i++) {
navigatePageNumbers[i] = startNum++;
}
} else if (endNum > totalPage) {
endNum = totalPage;
// 最后navPageCount页
for (int i = navigatePages - 1; i >= 0; i--) {
navigatePageNumbers[i] = endNum--;
}
} else {
// 所有中间页
for (int i = 0; i < navigatePages; i++) {
navigatePageNumbers[i] = startNum++;
}
}
}
}
private void judgePageBoudary() {
isFirstPage = (1 == pageIndex);
isLastPage = (pageIndex == totalPage && 1 != pageIndex);
isPreviousPage = (1 != pageIndex);
isNextPage = (pageIndex != totalPage);
}
public void setList(List list) {
this.list = list;
}
public List getList() {
return list;
}
public int getTotalRows() {
return totalRows;
}
public int getPageSize() {
return pageSize;
}
public int getTotalPage() {
return totalPage;
}
public int getPageIndex() {
return pageIndex;
}
public int[] getNavigatePageNumbers() {
return navigatePageNumbers;
}
public boolean isFirstPage() {
return isFirstPage;
}
public boolean isLastPage() {
return isLastPage;
}
public boolean isPreviousPage() {
return isPreviousPage;
}
public boolean isNextPage() {
return isNextPage;
}
public String toString() {
String str = new String();
str = "[" + "totalRows=" + totalRows + ",totalPage=" + totalPage + ",pageIndex=" + pageIndex + ",pageSize="
+ pageSize + ",isFirstPage=" + isFirstPage + ",isLastPage=" + isLastPage + ",isPreviousPage="
+ isPreviousPage + ",hasNextPage=" + isNextPage + ",navigatePageNumbers=";
int len = navigatePageNumbers.length;
if (len > 0)
str += (navigatePageNumbers[0]);
for (int i = 1; i < len; i++) {
str += (" " + navigatePageNumbers[i]);
}
str += "]";
return str;
}
}
分页工具类
public class SjPageUtil {
public static int getStartRow(int pageIndex, int pageSize) {
if (pageIndex < 0) {
pageIndex = 1;
}
if (pageSize < 0) {
pageSize = 5;
}
return (pageIndex * pageSize - pageSize) + 1;
}
public static int getStartRow(int pageIndex) {
if (pageIndex < 0) {
pageIndex = 1;
}
return (pageIndex *5 - 5) + 1;
}
public static int getEndRow(int pageIndex, int pageSize) {
if (pageIndex < 0) {
pageIndex = 1;
}
if (pageSize < 0) {
pageSize = 5;
}
return pageIndex * pageSize;
}
public static int getEndRow(int pageIndex) {
if (pageIndex < 0) {
pageIndex = 1;
}
return pageIndex *5;
}
public static String getPageSQL(int pageIndex, int pageSize) {
if (pageIndex < 0) {
pageIndex = 1;
}
if (pageSize < 0) {
pageSize = 5;
}
return " BETWEEN " + ((pageIndex * pageSize - pageSize) + 1) + " AND " + pageIndex * pageSize;
}
public static String getCountSQL(String sql) {
return String.format("SELECT COUNT(1) FROM (%s)", sql);
}
public static String getPageSQL(String sql, int pageIndex) {
if (pageIndex < 0) {
pageIndex = 1;
}
String sb = new StringBuffer("SELECT * FROM (SELECT TMP_00.*, ROWNUM RUM FROM (").append(sql)
.append(") TMP_00 ")
.append(") TMP_0 WHERE TMP_0.RUM <= ").append(pageIndex * 5).append(" AND TMP_0.RUM >= ")
.append((pageIndex * 5 - 5) + 1).toString();
return sb;
}
}
Dao
public SjPagination selectbgzt(int pageIndex,int pageSize,String code,String spjg,String zt);
DaoImpl
@Override
public SjPagination selectbgzt(int pageIndex,int pageSize,String code,String spjg,String zt) {
String sql=”select * from T_QT_SJ_SJHDGL_SQJLB where spjg=’”+spjg+“‘and hdid=’”+code+”’ and skzxzt=’”+zt+”’”;
//String str=null;
if(spjg==null){
sql=”select * from T_QT_SJ_SJHDGL_SQJLB where spjg=0 and hdid=’”+code+”’”;
}
if(zt==null){
sql=”select * from T_QT_SJ_SJHDGL_SQJLB where spjg=0 and hdid=’”+code+”’”;
}
if(StringUtils.hasLength(spjg)&&StringUtils.hasLength(code)&&zt!=null){
sql=”select * from T_QT_SJ_SJHDGL_SQJLB where skzxzt=’”+zt+“‘and hdid=’”+code+”’ and spjg=’”+spjg+”’”;
}
String count_sql = SjPageUtil.getCountSQL(sql.toString());
int _total = sjt.queryForInt(count_sql);
//sql.append(” ORDER BY t.SCSJ DESC”);
String page_sql = SjPageUtil.getPageSQL(sql.toString(), pageIndex);
QueryUtil qu = new QueryUtil();
qu.setViewClazz(QtSqjlb.class);
qu.setWholeSQL(page_sql);
List list =qu.list();
return new SjPagination(list, _total, pageIndex);
}
controller
@RequestMapping(value = “gosyhdgl”,method = RequestMethod.GET)
public String syshSys(HttpServletRequest request, HttpServletResponse response) {
//ModelAndView modelAndView = new ModelAndView(Constants.RESPONSE_VIEW);
//获得客户端提交页码
String pageIndex =request.getParameter(“pageIndex”);
String pageSize =request.getParameter(“pageSize”);
//获取user的session
Object obj = request.getSession().getAttribute(“qtUser”);
//强转
QtUser qtUser =(QtUser) obj;
//获得复选框的条件
String huodongstate= request.getParameter(“hdzt”);
//获取输入框的条件
String huodongtitle =request.getParameter(“sousuo”);
pageIndex = (pageIndex==null)?”1”:pageIndex;
pageSize = (pageSize==null)?”5”:pageSize;
//获取dbid
if(qtUser!=null){
Integer id=qtUser.getDbid();
SjPagination page =sjzxService.selectPage(Integer.parseInt(pageIndex), Integer.parseInt(pageSize),id,huodongstate,huodongtitle);
request.setAttribute(“page”,page);
return “swl/qt/sj/syhdgl”;
}
return “swl/qt/sj/syhdgl”;
}
page.jsp