野子中国好歌曲下载:spring struts hibernate分页实例

来源:百度文库 编辑:九乡新闻网 时间:2024/04/28 18:15:58
  1. package com.yizhou.common.test;   
  2.   
  3. /**  
  4.  *     
  5.  * Copyright @ 2008 YIZHOU SOFTWARE Co. Ltd.  
  6.  * All right reserved.  
  7.  *  
  8.  * @author xuxinlong  
  9.  *  
  10.  *  mail: longxx888@163.com  
  11.  *    
  12.  */  
  13. public class PageBean {   
  14.     private int count = 0; // 记录总数   
  15.   
  16.     private int pageSize = 20; // 每页显示记录数   
  17.   
  18.     private int pageCount = 0; // 总页数   
  19.   
  20.     private int page = 1; // 当前页数   
  21.   
  22.     private String totalCountSQL;// 得到总记录数sql语句   
  23.   
  24.     private String listSQL;// 得到查询记录sql语句   
  25.   
  26.     public int getCount() {   
  27.         return count;   
  28.     }   
  29.   
  30.     public void setCount(int count) {   
  31.         if (pageSize != 0) {   
  32.             pageCount = count / pageSize;   
  33.             if (count % pageSize != 0) {   
  34.                 pageCount++;   
  35.             }   
  36.         }   
  37.         this.count = count;   
  38.     }   
  39.   
  40.     public String getListSQL() {   
  41.         return listSQL;   
  42.     }   
  43.   
  44.     public void setListSQL(String listSQL) {   
  45.         this.listSQL = listSQL;   
  46.     }   
  47.   
  48.     public int getPage() {   
  49.         return page;   
  50.     }   
  51.   
  52.     public void setPage(int page) {   
  53.         this.page = page;   
  54.     }   
  55.   
  56.     public int getPageCount() {   
  57.         return pageCount;   
  58.     }   
  59.   
  60.     public void setPageCount(int pageCount) {   
  61.         this.pageCount = pageCount;   
  62.     }   
  63.   
  64.     public int getPageSize() {   
  65.         return pageSize;   
  66.     }   
  67.   
  68.     public void setPageSize(int pageSize) {   
  69.         this.pageSize = pageSize;   
  70.     }   
  71.   
  72.     public String getTotalCountSQL() {   
  73.         return totalCountSQL;   
  74.     }   
  75.   
  76.     public void setTotalCountSQL(String totalCountSQL) {   
  77.         this.totalCountSQL = totalCountSQL;   
  78.     }   
  79.   
  80. }   

新建个接口
  1. package com.yizhou.common.test;   
  2.   
  3. import java.io.Serializable;   
  4. import java.util.List;   
  5.   
  6. /**  
  7.  *   
  8.  * Copyright @ 2008 NANJING YIZHOU SOFTWARE Co. Ltd.  
  9.  * All right reserved.  
  10.  *  
  11.  * @author xuxinlong  
  12.  *  
  13.  *  mail: longxx888@163.com  
  14.  *    
  15.  */  
  16. public interface PaginateInterface extends Serializable {   
  17.     public List getList(PageBean page);   
  18.   
  19.     public String getToolsMenu(PageBean page);   
  20.   
  21.     public Long getTotalCount(PageBean p, String str[], Object ob2[])   
  22.             throws Exception;   
  23.   
  24.     public Long getTotalCount(PageBean page) throws Exception;   
  25.   
  26.     public List getList(PageBean page, String str[], Object ob2[])   
  27.             throws Exception;   
  28. }   

  1. package com.yizhou.common.test;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.List;   
  5.   
  6. import org.hibernate.Query;   
  7. import org.hibernate.Session;   
  8. import org.springframework.orm.hibernate3.HibernateCallback;   
  9. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;   
  10.   
  11. /**  
  12.  *分页核心类  
  13.  * Copyright @ 2008 NANJING YIZHOU SOFTWARE Co. Ltd.  
  14.  * All right reserved.  
  15.  *  
  16.  * @author xuxinlong  
  17.  *  
  18.  *  mail: longxx888@163.com  
  19.  *    
  20.  */  
  21. public class Paginate extends HibernateDaoSupport implements PaginateInterface {   
  22.     /**     
  23.       * 显示用的分页信息     
  24.       */      
  25.      public String getToolsMenu(PageBean p) {      
  26.       StringBuffer str = new StringBuffer("");      
  27.       int next, prev;      
  28.       prev = p.getPage() - 1;      
  29.       next = p.getPage() + 1;      
  30.           
  31.       if (p.getPage() > 1) {      
  32.        str      
  33.          .append("首页 ");      
  34.       } else {      
  35.           //str.append("首页 ");   
  36.           str.append("首页 ");   
  37.       }      
  38.       if (p.getPage() > 1) {      
  39.        str.append("
  40.          + prev + ";document.forms(0).submit();'>上页 ");      
  41.       } else {      
  42.           //str.append("上页 ");      
  43.           str.append("上页 ");      
  44.       }      
  45.       if (p.getPage() < p.getPageCount()) {      
  46.        str.append("
  47.          + next + ";document.forms(0).submit();'>下页 ");      
  48.       } else {      
  49.         //str.append("下页 ");      
  50.           str.append("下页 ");   
  51.       }      
  52.       if (p.getPageCount() > 1 && p.getPage() != p.getPageCount()) {      
  53.        str.append("
  54.          + p.getPageCount()      
  55.          + ";document.forms(0).submit();'>末页  ");      
  56.       } else {      
  57.        //str.append("末页  ");   
  58.          str.append("末页  ");   
  59.       }      
  60.       str.append(" 共" + p.getCount() + "条记录");      
  61.       str      
  62.         .append("  每 页");      
  63.           
  64.       if (p.getPageSize() == 3) {      
  65.        str.append("3");      
  66.       } else {      
  67.        str.append("3");      
  68.       }      
  69.           
  70.       if (p.getPageSize() == 10) {      
  71.        str.append("10");      
  72.       } else {      
  73.        str.append("10");      
  74.       }      
  75.       if (p.getPageSize() == 20) {      
  76.        str.append("20");      
  77.       } else {      
  78.        str.append("20");      
  79.       }      
  80.       if (p.getPageSize() == 50) {      
  81.        str.append("50");      
  82.       } else {      
  83.        str.append("50");      
  84.       }      
  85.       if (p.getPageSize() == 100) {      
  86.        str.append("100");      
  87.       } else {      
  88.        str.append("100");      
  89.       }      
  90.       str.append("");      
  91.       str.append("条 分" + p.getPageCount() + "页显示 转到");      
  92.       str      
  93.         .append("");      
  94.       for (int i = 1; i < p.getPageCount() + 1; i++) {      
  95.        if (i == p.getPage()) {      
  96.         str.append("" + i      
  97.           + "");      
  98.        } else {      
  99.         str.append("" + i + "");      
  100.        }      
  101.       }      
  102.       str.append("页");      
  103.       str.append("
  104.         + " name=\"pages\" > ");      
  105.       str.append("
  106.         + " name=\"pageSize\"> ");      
  107.       return str.toString();      
  108.      }      
  109.           
  110.      /**  
  111.       * 获取总条数  
  112.       */  
  113.      public Long getTotalCount(PageBean p) throws Exception {   
  114.       List list = getHibernateTemplate().find(p.getTotalCountSQL());      
  115.       long count = 0;      
  116.       if (list.size() > 0) {      
  117.        count = new Long(""+list.get(0));      
  118.       }      
  119.       return count;      
  120.      }      
  121.          
  122.      /**  
  123.       * 查询信息进行分页  
  124.       */  
  125.      public List getList(final PageBean p) {   
  126.           return this.getHibernateTemplate().executeFind(new HibernateCallback(){   
  127.              public Object doInHibernate(Session session){   
  128.                  Query q = session.createQuery(p.getListSQL());   
  129.                   q.setFirstResult((p.getPage() - 1) * p.getPageSize());      
  130.                   q.setMaxResults(p.getPageSize());      
  131.                        return q.list();   
  132.             }   
  133.         });   
  134.      }      
  135.          
  136.      /**  
  137.       * 查询信息进行分页  带有参数的  
  138.       */  
  139.      public List getList(final PageBean p,final String str[], final Object ob2[]) {      
  140.          return this.getHibernateTemplate().executeFind(new HibernateCallback(){   
  141.              public Object doInHibernate(Session session){   
  142.                  Query q = session.createQuery(p.getListSQL());   
  143.                  if(str!=null){   
  144.                       for (int i = 0; i < str.length; i++) {      
  145.                        q.setParameter(str[i], ob2[i]);      
  146.                       }      
  147.                  }   
  148.                  q.setFirstResult((p.getPage() - 1) * p.getPageSize());      
  149.                  q.setMaxResults(p.getPageSize());      
  150.                  return q.list();     
  151.             }   
  152.         });        
  153.      }      
  154.         
  155.      /**  
  156.       * 获取总条数   带有参数的  
  157.       */    
  158.      public Long getTotalCount(PageBean p, String str[], Object ob2[])throws Exception {    
  159.          List list=new ArrayList();   
  160.          if(str!=null && str.length>0){   
  161.              list = getHibernateTemplate().findByNamedParam(p.getTotalCountSQL(), str, ob2);   
  162.          }else{   
  163.              list=this.getHibernateTemplate().find(p.getTotalCountSQL());   
  164.          }   
  165.          long count = 0;      
  166.          if (list.size() > 0) {      
  167.            count = (Long)list.get(0);      
  168.           }      
  169.           return count;      
  170.     }      
  171. }   

Action的调用
  1. package com.yizhou.common.test;   
  2.   
  3. import java.util.List;   
  4.   
  5. import javax.servlet.http.HttpServletRequest;   
  6. import javax.servlet.http.HttpServletResponse;   
  7. import javax.servlet.http.HttpSession;   
  8.   
  9. import org.apache.struts.action.Action;   
  10. import org.apache.struts.action.ActionForm;   
  11. import org.apache.struts.action.ActionForward;   
  12. import org.apache.struts.action.ActionMapping;   
  13.   
  14. /**  
  15.  * 分页代码示例 调用  
  16.  * Copyright @ 2008 NANJING YIZHOU SOFTWARE Co. Ltd.  
  17.  * All right reserved.  
  18.  *  
  19.  * @author xuxinlong  
  20.  *  
  21.  */  
  22. public class SplitPageAction extends Action {   
  23.   
  24.     private PaginateInterface pageinate;   
  25.     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {   
  26.            
  27.         HttpSession session=request.getSession();   
  28.         Object obj=session.getAttribute("KDUser");   
  29.         if(obj!=null){   
  30.             PageBean pb=new PageBean();   
  31.             String jumpPage=request.getParameter("jumpPage");       
  32.             String pageSize=request.getParameter("pageSize");   
  33.             if(jumpPage!=null && !"".equals(jumpPage) && pageSize!=null && !"".equals(pageSize)){       
  34.                 pb.setPageSize(new Integer(pageSize));   
  35.             }else{   
  36.                 jumpPage="1";   
  37.             }   
  38.             String strSqlCnt="select count(*) from TUsertable";   
  39.             String strSqlInfo="select u from TUsertable u";   
  40.                
  41.             pb.setTotalCountSQL(strSqlCnt);   
  42.             pb.setListSQL(strSqlInfo);   
  43.             pb.setPage(new Integer(jumpPage));   
  44.                
  45.             pb.setCount(this.pageinate.getTotalCount(pb).intValue());   
  46.             List listUser=this.pageinate.getList(pb);   
  47.             request.setAttribute("info", this.pageinate.getToolsMenu(pb));   
  48.             request.setAttribute("listUser", listUser);   
  49.                
  50.         return mapping.findForward("pagelist");   
  51.         }   
  52.            
  53.            
  54.         return mapping.findForward("error");   
  55.     }   
  56.     public PaginateInterface getPageinate() {   
  57.         return pageinate;   
  58.     }   
  59.     public void setPageinate(PaginateInterface pageinate) {   
  60.         this.pageinate = pageinate;   
  61.     }   
  62.        
  63.   
  64. }   

spring文件配置
  1.   
  2.       
  3.           
  4.       
  5.   
  6.   
  7.   
  8.       
  9.           
  10.       
  11.   


JSP中使用
  1. <%@ page language="java" pageEncoding="utf-8"%>  
  2. <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>  
  3.     
  4.   
  5.   
  6.   
  7.   
  8.     分页演示  
  9.        
  10.       
  11.   
  12.   
  13.   
  14.      
  15.  
    分页演示
      
  16.    
  17.    
  18.    
  19.    
  20.    
  21.   
  22. 下载此资源的读者还下载过:
    spring+struts+hibernate分页实例