喃都不说了,贴代码,意思都在代码里面了 Page.java //分页类。定义分页字段信息,供前台页面使用 package com.core.common; import java.util.List; public class Page<T> { private int pageIndex;//当前页 private int pageSize;//每页条数 private int totalCount;//符合查询条件总条数 private List<T> pageData;//符合查询条件的列表_注意这里是泛型 private int startRow;//数据库起始记录指针 private int totalPage;//总页数 public Page(int pageIndex, int pageSize){ this.pageIndex = pageIndex <= 0 ? 1 : pageIndex; this.pageSize = pageSize <= 0 ? 10 : pageSize; } public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public List<T> getPageData() { return pageData; } public void setPageData(List<T> pageData) { this.pageData = pageData; } public int getStartRow() { startRow = (pageIndex-1) * pageSize; return startRow; } public int getTotalPage() { totalPage = (int) Math.ceil(totalCount/Double.parseDouble(String.valueOf(pageSize))); return totalPage; } } //分页过程————controller @RequestMapping(value = {"/order/list"}) //page当前页 //model 查询参数实体 public ModelAndView get_orders(Integer page,B2cOrderModel model){ ModelAndView mv = new ModelAndView(); mv.setViewName("/order_list"); //传递参数分别是:当前页,每页条数,查询参数 Page<B2cOrderModel> pageData = orderService.getOrders(getPageIndex(page), ConstantUtil.Page.PAGE_NUM, model); mv.addObject("pageData", pageData.getPageData());//列表 mv.addObject("totalCount", pageData.getTotalCount());//总条数 mv.addObject("totalPage", pageData.getTotalPage());//总页数 mv.addObject("currentPage", page);//当前页 mv.addObject("pageNav", PageNavUtil.getBluePageNavHtml(getPageIndex(page), ConstantUtil.Page.PAGE_NUM, pageData.getTotalCount(), ConstantUtil.Page.NAV_NUM));//分页导航 return mv; } //分页过程————service @Override public Page<B2cOrderModel> getOrders(Integer pageIndex, int pageNum, B2cOrderModel model) { Page<B2cOrderModel> page = new Page<B2cOrderModel>(pageIndex, pageNum); //创建查询条件对象 CriteriaCondition condition = new CriteriaCondition();//封装查询条件的类 condition.setDistinct(false); condition.setStart(page.getStartRow()); condition.setPageSize(pageNum); condition.setOrderByClause(" CREATED_DT DESC"); condition.put("postName", model.getPostName()); condition.put("postContactPhone", model.getPostContactPhone()); //查询符合条件的总条数和列表 int totalCount = b2cOrderSourceDao.countByCondition(condition.getCondition()); List<B2cOrderModel> pageData = b2cOrderSourceDao.selectByCondition(condition); //设置分页信息 page.setTotalCount(totalCount); page.setPageData(pageData); //返回分页信息对象 return page; } //分页过程————mybatis语句 查询条数 <select id="countByCondition" parameterType="java.util.Map" resultType="java.lang.Integer"> select count(*) from 表 <include refid="Base_Where_B2cOrder" /> </select> 查询列表 <select id="selectByCondition" resultMap="Base_Result_B2cOrder" parameterType="com.core.common.CriteriaCondition"> select <if test="distinct"> distinct </if> <include refid="Base_Column_B2cOrder" /> from b2c_order <include refid="Base_Where_B2cOrder" /> <if test=" null!= orderByClause"> order by #{orderByClause} </if> <if test="null!=start and null!=pageSize"> limit #{start}, #{pageSize} </if> </select> //结果映射列 <resultMap id="Base_Result_B2cOrder" type="com.source.model.B2cOrderModel"> <result property="model参数名" column="对应的数据库字段"/> </resultMap> //字段列 <sql id="Base_Column_B2cOrder"> 需要查询出的数据库字段 </sql> //查询条件 <sql id="Base_Where_B2cOrder"> <trim prefix="where" prefixOverrides="and|or"> <if test="null!=condition"> <if test="null!=condition.参数字段名 and ''!=condition.参数字段名"> and 数据库字段名= #{condition.参数字段名} </if> </if> </trim> </sql> //分页过程---查询条件封装对象 CriteriaCondition import java.util.HashMap; /** * 公用条件查询类 */ public class CriteriaCondition { /** * 存放条件查询值 */ Map<String, Object> condition; /** * 是否相异 */ boolean distinct; /** * 排序字段 */ String orderByClause; /** * 分页起始页 */ Integer start; /** * 分页数值 */ Integer pageSize; public CriteriaCondition() { super(); condition = new HashMap<String, Object>(); } public CriteriaCondition(String orderByClause, Integer start, Integer pageSize) { this.distinct = true; this.orderByClause = orderByClause; this.start = start; this.pageSize = pageSize; condition = new HashMap<String, Object>(); } public CriteriaCondition(boolean distinct, String orderByClause, Integer start, Integer pageSize) { this.distinct = distinct; this.orderByClause = orderByClause; this.start = start; this.pageSize = pageSize; condition = new HashMap<String, Object>(); } public CriteriaCondition put(String condition, Object value) { this.condition.put(condition, value); return (CriteriaCondition)this; } public Map<String, Object> getCondition() { return condition; } public void setDistinct(boolean distinct) { this.distinct = distinct; } public void setOrderByClause(String orderByClause) { this.orderByClause = orderByClause; } public void setStart(Integer start) { this.start = start; } public void setPageSize(Integer pageSize) { this.pageSize = pageSize; } } //构造前台分页显示 public class PageNavUtil { public static String getBluePageNavHtml(int currentPage, int pageSize, int totalRows, int showNums) { StringBuilder pageNavHtml = new StringBuilder(); if (showNums < 1) { showNums = 5; } // 计算总页数 int totalPage = (int)Math.ceil(totalRows / Double.parseDouble(String.valueOf(pageSize))); // 计算中间页码数字 int midNum = (int)Math.ceil(Double.parseDouble(String.valueOf(showNums)) / 2); int beginNum = currentPage <= midNum ? 1 : currentPage - midNum + 1; int endNum = beginNum + showNums - 1; if (endNum > totalPage) { endNum = totalPage; } // 至少有1页以上 才显示分页导航 if (totalPage > 1) { // 需要显示 首页 if (currentPage > 1) { pageNavHtml.append("<a href='?page=1'> 首页</a>"); } // 如果有上一页 if (currentPage > beginNum) { pageNavHtml.append("<a href='?page=" + (currentPage - 1) + "'> < </a>"); } else { pageNavHtml.append("<a href='javascript:void(0)' class='disabled'> < </a>"); } for (int i = beginNum; i <= endNum; i++) { if (i == currentPage) { pageNavHtml.append("<a href='javascript:void(0)' class='this'>" + currentPage + "</a>"); } else { pageNavHtml.append("<a href='?page=" + i + "'>" + i + "</a>"); } } // 如果有下一页 if (currentPage < endNum) { pageNavHtml.append("<a href='?page=" + (currentPage + 1) + "'> > </a>"); } else { pageNavHtml.append("<a href='javascript:void(0)' class='disabled'> > </a>"); } // 需要显示 尾页 if (currentPage < totalPage) { pageNavHtml.append("<a href='?page=" + totalPage + "'>尾页</a>"); } } return pageNavHtml.toString(); } public static String getPageNavHtml(int currentPage, int pageSize, int totalRows, int showNums) { StringBuilder pageNavHtml = new StringBuilder(); if (showNums < 1) { showNums = 5; } // 计算总页数 int totalPage = (int)Math.ceil(totalRows / Double.parseDouble(String.valueOf(pageSize))); // 计算中间页码数字 int midNum = (int)Math.ceil(Double.parseDouble(String.valueOf(showNums)) / 2); int beginNum = currentPage <= midNum ? 1 : currentPage - midNum + 1; int endNum = beginNum + showNums - 1; if (endNum > totalPage) { endNum = totalPage; } // 至少有1页以上 才显示分页导航 if (totalPage > 1) { // 需要显示 首页 if (currentPage > 1) { pageNavHtml.append("<a href='?page=1'> 首页</a>"); } // 如果有上一页 if (currentPage > beginNum) { pageNavHtml.append("<a href='?page=" + (currentPage - 1) + "'> < </a>"); } else { pageNavHtml.append("<span class='disabled'> < </span>"); } for (int i = beginNum; i <= endNum; i++) { if (i == currentPage) { pageNavHtml.append("<span class='current'>" + currentPage + "</span>"); } else { pageNavHtml.append("<a href='?page=" + i + "'>" + i + "</a>"); } } // 如果有下一页 if (currentPage < endNum) { pageNavHtml.append("<a href='?page=" + (currentPage + 1) + "'> > </a>"); } else { pageNavHtml.append("<span class='disabled'> > </span>"); } // 需要显示 尾页 if (currentPage < totalPage) { pageNavHtml.append("<a href='?page=" + totalPage + "'>尾页</a>"); } } return pageNavHtml.toString(); } }