springMvc +Jdbctemplate+分页

--------spring jdbcTemplate mysql oracle 分页工具类

public class Pagination extends JdbcDaoSupport{


public static final int NUMBERS_PER_PAGE = 10;
//一页显示的记录数
 private int numPerPage; 
//记录总数
 private int totalRows; 
//总页数
 private int totalPages; 
//当前页码
 private int currentPage; 
 //起始行数
 private int startIndex;
 //结束行数
 private int lastIndex;
 //结果集存放List
 private List resultList;
 //JdbcTemplate jTemplate
 private JdbcTemplate jTemplate;
 
 /**
  * 每页显示10条记录的构造函数,使用该函数必须先给Pagination设置currentPage,jTemplate初值
  * @param sql Oracle语句
  */
 public Pagination(String sql){
   if(jTemplate == null){
     throw new IllegalArgumentException("com.deity.ranking.util.Pagination.jTemplate is null,please initial it first. ");
   }else if(sql.equals("")){
     throw new IllegalArgumentException("com.deity.ranking.util.Pagination.sql is empty,please initial it first. ");
   }
   new Pagination(sql,currentPage,NUMBERS_PER_PAGE,jTemplate);
 }
 
 /**分页构造函数
  * @param sql 根据传入的sql语句得到一些基本分页信息
  * @param currentPage 当前页
  * @param numPerPage 每页记录数
  * @param jTemplate JdbcTemplate实例
  */
 public Pagination(String sql,int currentPage,int numPerPage,JdbcTemplate jTemplate){
   if(jTemplate == null){
     throw new IllegalArgumentException("com.deity.ranking.util.Pagination.jTemplate is null,please initial it first. ");
   }else if(sql == null || sql.equals("")){
     throw new IllegalArgumentException("com.deity.ranking.util.Pagination.sql is empty,please initial it first. ");
   }
   //设置每页显示记录数
   setNumPerPage(numPerPage);
   //设置要显示的页数
   setCurrentPage(currentPage);
   //计算总记录数
   StringBuffer totalSQL = new StringBuffer(" SELECT count(*) FROM ( ");
   totalSQL.append(sql);
   totalSQL.append(" ) totalTable ");
   //给JdbcTemplate赋值
   setJdbcTemplate(jTemplate);
   //总记录数
   setTotalRows(getJdbcTemplate().queryForInt(totalSQL.toString()));
   //计算总页数
   setTotalPages();
   //计算起始行数
   setStartIndex();
   //计算结束行数
   setLastIndex();
   system.out.println("lastIndex="+lastIndex);//////////////////
   
   //构造oracle数据库的分页语句
  /** StringBuffer paginationSQL = new StringBuffer(" SELECT * FROM ( ");
   paginationSQL.append(" SELECT temp.* ,ROWNUM num FROM ( ");
   paginationSQL.append(sql);
   paginationSQL.append(" ) temp where ROWNUM <= " + lastIndex);
   paginationSQL.append(" ) WHERE num > " + startIndex); 
    */ 
   
   
   //装入结果集
   setResultList(getJdbcTemplate().queryForList(getMySQLPageSQL(sql,startIndex,numPerPage)));
 }
 
  
 
   /**
* 构造MySQL数据分页SQL 
* @param queryString
* @param startIndex
* @param pageSize
* @return
*/
public String getMySQLPageSQL(String queryString, Integer startIndex, Integer pageSize)
{
String result = "";
if (null != startIndex && null != pageSize)
{
result = queryString + " limit " + startIndex + "," + pageSize;
} else if (null != startIndex && null == pageSize)
{
result = queryString + " limit " + startIndex;
} else
{
result = queryString;
}
return result;
}


 
 public int getCurrentPage() {
   return currentPage;
 } 


 public void setCurrentPage(int currentPage) {
   this.currentPage = currentPage;
 }


 public int getNumPerPage() {
   return numPerPage;
 }


 public void setNumPerPage(int numPerPage) {
   this.numPerPage = numPerPage;
 }


 public List getResultList() {
   return resultList;
 }


 public void setResultList(List resultList) {
   this.resultList = resultList;
 }


 public int getTotalPages() {
   return totalPages;
 }
//计算总页数
 public void setTotalPages() {
   if(totalRows % numPerPage == 0){
     this.totalPages = totalRows / numPerPage;
   }else{
     this.totalPages = (totalRows / numPerPage) + 1;
   }
 }


 public int getTotalRows() {
   return totalRows;
 }


 public void setTotalRows(int totalRows) {
   this.totalRows = totalRows;
 }


 public int getStartIndex() {
   return startIndex;
 }


 public void setStartIndex() {
   this.startIndex = (currentPage - 1) * numPerPage;
 }


 public int getLastIndex() {
   return lastIndex;
 }


 public JdbcTemplate getJTemplate() {
   return jTemplate;
 }


 public void setJTemplate(JdbcTemplate template) {
   jTemplate = template;
 }
  
//计算结束时候的索引
 public void setLastIndex() {
   System.out.println("totalRows="+totalRows);///////////
   System.out.println("numPerPage="+numPerPage);///////////
   if( totalRows < numPerPage){
     this.lastIndex = totalRows;
   }else if((totalRows % numPerPage == 0) || (totalRows % numPerPage != 0 && currentPage < totalPages)){
     this.lastIndex = currentPage * numPerPage;
   }else if(totalRows % numPerPage != 0 && currentPage == totalPages){//最后一页
     this.lastIndex = totalRows ;
   }
 }


}



/**
 * 业务处理
 * @author Administrator
 *
 */
@Service("BusinessService")   
public class BusinessService { 


@Resource
    private BusinessDao businessDao;




/**
* 分页查询  
* @param currentPage  当前页 
* @param numPerPage   每页记录数
* @return 
*/
public Pagination queryPageBusiness(Integer currentPage,Integer numPerPage) {   
return businessDao.queryPageBusiness(currentPage, numPerPage); 
}
}




@Controller
@RequestMapping(value = http://blog.csdn.net/huahuagongzi9999/article/details/"/business")
public class BusinessController {


@Resource
private  BusinessService businessService;


private final static String uploadURL=propertiesUtil.getUrl("uploadURL");   
/**  
* 分页查询所有
* @param request
* @param response 
*/ 
@RequestMapping(value = "queryPageBusiness.do") 
public void queryPageBusiness(HttpServletRequest request,HttpServletResponse response) {
String message = "";
String status = ""; 
PrintWriter out = null;
List> businessList =null;
Pagination page=null; 
  Map map = new HashMap();
try {
out = response.getWriter();
String currentPage  = URLDecoder.decode(request.getParameter("currentPage"));
String numPerPage  = URLDecoder.decode(request.getParameter("numPerPage"));
if("".equals(currentPage)||"".equals(numPerPage)){ 
page =businessService.queryPageBusiness(1, 10);  
}else{ 
page =businessService.queryPageBusiness(StringUtil.getInteger(currentPage), StringUtil.getInteger(numPerPage));  

List list=page.getResultList();
businessList=new ArrayList>(); 
for (int i = 0,len=list.size();iMap maps=new HashMap();
Map mapRe=(Map)list.get(i);       
maps.put("businessPic", StringUtil.nullOrBlank(mapRe.get("businessPic")+"")?"?"":uploadURL+mapRe.get("businessPic"));
maps.put("businessName", mapRe.get("businessName"));
maps.put("businessId", mapRe.get("businessId"));
maps.put("businessEname", mapRe.get("businessEname"));  
maps.put("createTime", FormatDateTime.formatDateTime("yyyy-MM-dd", mapRe.get("createTime")+""));
businessList.add(maps); 
}
message="success"; 
status = Constants.RETURN_STATUS_0;
} catch (Exception e1) { 
e1.printStackTrace();
message="failure";
status = Constants.RETURN_STATUS_1;
}finally{ 
map.put("message", message); 
map.put("totalPage", page.getTotalPages());  
map.put("currentPage", page.getCurrentPage());  
map.put("totalRows", page.getTotalRows());  
map.put("numPerPage", page.getNumPerPage());  
map.put("status", status);  
map.put("businessList", businessList);  
//必须设置字符编码,否则返回json会乱码 
response.setContentType("text/html;charset=UTF-8");     
out.write(JSONSerializer.toJSON(map).toString());
out.flush();
out.close();
}  


}




@Repository("businessDao")
public class BusinessDao extends AbstractDao{ 
/**
* 分页查询  
* @param currentPage  当前页 
* @param numPerPage   每页记录数
* @return 
*/
public Pagination queryPageBusiness(Integer currentPage,Integer numPerPage) {  
String sql="SELECT * FROM business ORDER BY businessId ASC ";
Pagination page=new Pagination(sql, currentPage, numPerPage,  getJdbcTemplate());
return page;    
}
}





<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 



 

分页列表


 

 


 
 

  


  


 
   
 

  
  
 


商圈列表
 


 

 
     

 
 
 
 

 
 
   
   
logo名称英文名注册日期
 
   

     

 
 
   
   
   

 
 
    
   







你可能感兴趣的:(springMvc +Jdbctemplate+分页)