`
Clayz
  • 浏览: 293821 次
  • 性别: Icon_minigender_1
  • 来自: 东京
社区版块
存档分类
最新评论

struts+hibernate分页

阅读更多
新建表
sql 代码
 
  1. DROP DATABASE IF EXISTS `wjcms`;  
  2. CREATE DATABASE `wjcms` /*!40100 DEFAULT CHARACTER SET gb2312 */;  
  3. USE `wjcms`;  
  4.   
  5. #  
  6. Table structure for table t_article  
  7. #  
  8.   
  9. CREATE TABLE `t_article` (  
  10.   `a_id` int(11) NOT NULL auto_increment,  
  11.   `a_sort` int(11) NOT NULL default '0',  
  12.   `a_title` varchar(50) default NULL,  
  13.   `a_body` text,  
  14.   `a_author` varchar(11) default '',  
  15.   `a_hit` int(11) NOT NULL default '0',  
  16.   `c_id` int(11) default '0',  
  17.   `a_date` varchar(20) default NULL,  
  18.   PRIMARY KEY  (`a_id`)  
  19. )   


实体
java 代码
 
  1. public class articleVO {  
  2.     private int a_id;  
  3.     private int a_sort;  
  4.     private int a_hit;  
  5.     private int c_id;  
  6.     private String a_title;  
  7.     private String a_body;  
  8.     private String a_author;  
  9.     private String a_date;  
  10.     // getter setter  


新建page.java
java 代码
 
  1. package page.dal;  
  2.   
  3. public class page {  
  4.     private int totalRows; //总行数  
  5.     private int pageSize = 10//每页显示的行数  
  6.     private int currentPage; //当前页号  
  7.     private int totalPages; //总页数  
  8.     private int startRow; //当前页在数据库中的起始行  
  9.   
  10.     public page(int _totalRows) {  
  11.      totalRows = _totalRows;  
  12.      totalPages=totalRows/pageSize;  
  13.      int mod=totalRows%pageSize;  
  14.      if(mod>0){  
  15.        totalPages++;  
  16.      }  
  17.      currentPage = 1;  
  18.      startRow = 0;  
  19.    }  
  20.   
  21.    public int getStartRow() {  
  22.      return startRow;  
  23.    }  
  24.   
  25.    public int getTotalPages() {  
  26.      return totalPages;  
  27.    }  
  28.   
  29.    public int getCurrentPage() {  
  30.      return currentPage;  
  31.    }  
  32.   
  33.    public int getPageSize() {  
  34.      return pageSize;  
  35.    }  
  36.   
  37.    public void setTotalRows(int totalRows) {  
  38.      this.totalRows = totalRows;  
  39.    }  
  40.   
  41.    public void setStartRow(int startRow) {  
  42.      this.startRow = startRow;  
  43.    }  
  44.   
  45.    public void setTotalPages(int totalPages) {  
  46.      this.totalPages = totalPages;  
  47.    }  
  48.   
  49.    public void setCurrentPage(int currentPage) {  
  50.      this.currentPage = currentPage;  
  51.    }  
  52.   
  53.    public void setPageSize(int pageSize) {  
  54.      this.pageSize = pageSize;  
  55.    }  
  56.   
  57.    public int getTotalRows() {  
  58.      return totalRows;  
  59.    }  
  60.   
  61.    public void first() {  
  62.      currentPage = 1;  
  63.      startRow = 0;  
  64.    }  
  65.   
  66.    public void previous() {  
  67.      if (currentPage == 1) {  
  68.        return;  
  69.      }  
  70.      currentPage--;  
  71.      startRow = (currentPage - 1) * pageSize;  
  72.    }  
  73.   
  74.    public void next() {  
  75.      if (currentPage < totalPages) {  
  76.        currentPage++;  
  77.      }  
  78.      startRow = (currentPage - 1) * pageSize;  
  79.    }  
  80.   
  81.    public void last() {  
  82.      currentPage = totalPages;  
  83.      startRow = (currentPage - 1) * pageSize;  
  84.    }  
  85.   
  86.    public void refresh(int _currentPage) {  
  87.      currentPage = _currentPage;  
  88.      if (currentPage > totalPages) {  
  89.        last();  
  90.      }  
  91.    }  
  92.   
  93.  }  


新建 pageHelp.java
java 代码
 
  1. package page.dal;  
  2. import javax.servlet.http.*;  
  3.   
  4. public class PagerHelp {  
  5.     public static page getPager(HttpServletRequest httpServletRequest,int totalRows) {  
  6.   
  7.      //定义pager对象,用于传到页面  
  8.      page pager = new page(totalRows);  
  9.   
  10.      //从Request对象中获取当前页号  
  11.      String currentPage = httpServletRequest.getParameter("currentPage");  
  12.   
  13.      //如果当前页号为空,表示为首次查询该页  
  14.      //如果不为空,则刷新page对象,输入当前页号等信息  
  15.      if (currentPage != null) {  
  16.        pager.refresh(Integer.parseInt(currentPage));  
  17.      }  
  18.   
  19.      //获取当前执行的方法,首页,前一页,后一页,尾页。  
  20.      String pagerMethod = httpServletRequest.getParameter("pageMethod");  
  21.   
  22.      if (pagerMethod != null) {  
  23.        if (pagerMethod.equals("first")) {  
  24.          pager.first();  
  25.        } else if (pagerMethod.equals("previous")) {  
  26.          pager.previous();  
  27.        } else if (pagerMethod.equals("next")) {  
  28.          pager.next();  
  29.        } else if (pagerMethod.equals("last")) {  
  30.          pager.last();  
  31.        }  
  32.      }  
  33.      return pager;  
  34.    }  
  35. }  


新建 util.java
java 代码
 
  1. package page.dal;  
  2. import net.sf.hibernate.Query;  
  3. import net.sf.hibernate.cfg.Configuration;  
  4. import java.util.List;  
  5. import net.sf.hibernate.HibernateException;  
  6. import net.sf.hibernate.SessionFactory;  
  7. import net.sf.hibernate.Session;  
  8. import java.util.*;  
  9. public class util {  
  10.     public util() {  
  11.     }  
  12.      private Session ss=null;  
  13.     public Session getSession()  
  14.   {  
  15.       //  Configuration config=null;  
  16.       SessionFactory sessionFactory;  
  17.       try {  
  18.           Configuration cfg = new Configuration();  
  19.           sessionFactory = cfg.addClass(articleVO.class).  
  20.                            buildSessionFactory();  
  21.           // SessionFactory sessionFactory=config.buildSessionFactory();  
  22.           ss = sessionFactory.openSession();  
  23.           return ss;  
  24.       } catch (HibernateException ex) {  
  25.           System.out.print("getsession出错了。。" + ex.getMessage());  
  26.           return null;  
  27.       }  
  28.   }  
  29.   
  30.   public int getCount()  
  31.   {  
  32.       String sql="select count(*) from articleVO" ;  
  33.       this.getSession();  
  34.   
  35.     try {  
  36.      // ss.createQuery("select count(a)as cont from articleVO a ");  
  37.       int rows= ((Integer) ss.iterate(sql).next()).intValue();  
  38.       ss.flush();  
  39.       return rows;  
  40.   
  41.     } catch (HibernateException ex) {  
  42.         System.out.print("ex::"+ex.getMessage());  
  43.         return 0;  
  44.     }  
  45.   }  
  46.   
  47.   public Collection  getList(int pagesize,int currow) throws HibernateException {  
  48.       Collection vehicleList = null;  
  49.       this.getSession();  
  50.       Query q=ss.createQuery("from articleVO");  
  51.       q.setFirstResult(currow);  
  52.       q.setMaxResults(pagesize);  
  53.       vehicleList=q.list();  
  54.       ss.flush();  
  55.       return vehicleList;  
  56.   }  
  57. }  


新建 struts  PageAction.java
java 代码
 
  1. package page.dal;  
  2.   
  3. import org.apache.struts.action.ActionMapping;  
  4. import org.apache.struts.action.ActionForm;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpServletResponse;  
  7. import org.apache.struts.action.ActionForward;  
  8. import org.apache.struts.action.Action;  
  9. import page.dal.*;  
  10. import java.util.*;  
  11. import net.sf.hibernate.*;  
  12.   
  13. public class pageAction extends Action {  
  14.     public ActionForward execute(ActionMapping mapping, ActionForm form,  
  15.                                  HttpServletRequest request,  
  16.                                  HttpServletResponse response) {  
  17.         Collection clInfos = null;//用于输出到页面的记录集合  
  18.         int totalRows;//记录总行数  
  19.         util dal=new util();  
  20.         totalRows=dal.getCount();  
  21.         System.out.print("总行数=="+totalRows);  
  22.         page p=PagerHelp.getPager(request,totalRows);  
  23.         try {  
  24.             clInfos = dal.getList(p.getPageSize(), p.getStartRow());  
  25.   
  26.         } catch (HibernateException ex) {  
  27.             System.out.print("action里的错误="+ex.getMessage());  
  28.         }  
  29.         request.setAttribute("page",p);  
  30.         request.setAttribute("list",clInfos);  
  31.         return mapping.findForward("page");  
  32.         //pageForm pageForm = (pageForm) form;  
  33.       //  throw new java.lang.UnsupportedOperationException(  
  34.               //  "Method $execute() not yet implemented.");  
  35.     }  
  36. }  


前台页面
xml 代码
 
  1. <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>  
  2. <%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>  
  3. <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>  
  4. <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>  
  5. <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>  
  6. <%@ page contentType="text/html; charset=GBK" %>  
  7. <html:html>  
  8. <head>  
  9. <title>  
  10. page  
  11. </title>  
  12. </head>  
  13. <body>  
  14. <table align="center" border="2">  
  15. <tr>  
  16. <th>a_title</th>  
  17. <th>a_body</th>  
  18. <th>a_a_date</th>  
  19. <th>a_author</th>  
  20. </tr>  
  21.   
  22. <logic:iterate id="listd" name="list">  
  23. <tr>  
  24. <td>  
  25. <bean:write name="listd" property="a_title"/>  
  26. </td>  
  27. <td>  
  28. <bean:write name="listd" property="a_author"/>  
  29. </td>  
  30. <td>  
  31. <bean:write name="listd" property="a_date"/>  
  32. </td>  
  33. <td>  
  34. <bean:write name="listd" property="a_date"/>  
  35. </td>  
  36. </tr>  
  37. </logic:iterate>  
  38.   
  39. </table>  
  40.   
  41. <bean:write name="page" property="currentPage"/>页  
  42. <bean:write name="page" property="totalPages" />页  
  43. <html:link action="/pageAction.do?pageMethod=first"  paramName="page" paramProperty="currentPage" paramId="currentPage">首页</html:link>  
  44.    <html:link action="/pageAction.do?pageMethod=previous"  paramName="page" paramProperty="currentPage" paramId="currentPage">上一页</html:link>  
  45.    <html:link action="/pageAction.do?pageMethod=next"  paramName="page" paramProperty="currentPage" paramId="currentPage">下一页</html:link>   
  46.    <html:link action="/pageAction.do?pageMethod=last"  paramName="page" paramProperty="currentPage" paramId="currentPage">尾页</html:link>  
  47. </body>  
  48. </html:html>  


启动浏览 pageAction.do  运行OK。



配置文件
xml 代码
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <!DOCTYPE hibernate-mapping PUBLIC  
  4.     "-//Hibernate/Hibernate Mapping DTD 2.0//EN"  
  5.     "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">  
  6. <hibernate-mapping>  
  7.   
  8.     <class name="page.dal.articleVO" table="t_article" >  
  9.     <id name="a_id" column="a_id" unsaved-value="0" >  
  10.       <generator class="native"/>  
  11.  </id>  
  12.      <property name="c_id"    column="c_id"/>  
  13.      <property name="a_title" column="a_title"/>  
  14.      <property name="a_sort"  column="a_sort"/>  
  15.      <property name="a_date"  column="a_date"/>  
  16.      <property name="a_body"  column="a_body"/>  
  17.      <property name="a_hit"   column="a_hit"/>  
  18.      <property name="a_author" column="a_author"/>  
  19.   </class>  
  20.   
  21. </hibernate-mapping>  

xml 代码
  1. hibernate.dialect net.sf.hibernate.dialect.MySQLDialect  
  2. hibernate.connection.driver_class org.gjt.mm.mysql.Driver  
  3. hibernate.connection.url jdbc:mysql://localhost:3306/wjcms  
  4. hibernate.connection.username root  
  5. hibernate.connection.password  
  6. hibernate.connection.pool_size 1  
  7. hibernate.proxool.pool_alias pool1  
  8. hibernate.show_sql true  
  9. hibernate.max_fetch_depth 1  
  10. hibernate.cache.use_query_cache true  
分享到:
评论
1 楼 aa00aa00 2010-08-24  
用pager-taglib 分页标签,更方便!!

相关推荐

Global site tag (gtag.js) - Google Analytics