ImageVerifierCode 换一换
格式:DOC , 页数:22 ,大小:29.48KB ,
资源ID:6416697      下载积分:10 金币
快捷注册下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

开通VIP
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.zixin.com.cn/docdown/6416697.html】到电脑端继续下载(重复下载【60天内】不扣币)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

开通VIP折扣优惠下载文档

            查看会员权益                  [ 下载后找不到文档?]

填表反馈(24小时):  下载求助     关注领币    退款申请

开具发票请登录PC端进行申请

   平台协调中心        【在线客服】        免费申请共赢上传

权利声明

1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。

注意事项

本文(struts2 hibernate分页.doc)为本站上传会员【xrp****65】主动上传,咨信网仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知咨信网(发送邮件至1219186828@qq.com、拔打电话4009-655-100或【 微信客服】、【 QQ客服】),核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载【60天内】不扣币。 服务填表

struts2 hibernate分页.doc

1、1). 针对网上一些 struts2分页的例子 不完整 ,代码混乱繁琐 ,我特意写了一个分页的小例子,供大家参考,如果程序有什么纰漏, 请指出。 开发环境: eclipse3.2 + myeclipse5.5 mysql5.0 tomcat5.5 Dreamweaver2004 2)建库,并导入脚本 这里给大家介绍一个mysql客户端工具 -- SQLyog ,非常好用。 ----------------------------------------------------admin.sql-------------------------------------------

2、 /* SQLyog Community Edition- MySQL GUI v6.12 MySQL - 5.0.37-community-nt : Database - test 把这段脚本导入到mysql的test库里面(你可以先建一个test库 ,这样会省去很多麻烦)  ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; create databa

3、se if not exists `test`; USE `test`; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*Table structure for table `admin` */ DROP TABLE IF EXISTS `admin`; CREATE TABLE `admin` ( `id`

4、 int(11) NOT NULL auto_increment, `user` char(20) default NULL, `pass` char(20) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=gb2312; /*Data for the table `admin` */ insert into `admin`(`id`,`user`,`pass`) values (786432,'天使','#####'),(786433,'天使','#####'),(786434,'天使','####

5、'),(786435,'天使','#####'),(786436,'天使','#####'),(819200,'天使','#####'),(819201,'天使','#####'),(819202,'天使','#####'),(819203,'天使','#####'),(819204,'天使','#####'),(819205,'wangbacheng','#####'),(851968,'天使','#####'),(851969,'天使','#####'),(851970,'天使','#####'),(851971,'天使','#####'),(851972,'天使','#####');

6、 /*!40101 SET SQL_MODE=@OLD_SQL_MO */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; DE 3)建立数据库连接程序 ,也就是hibernate相关程序,都可以用eclipse自动生成,这个不用多说 在eclipse 中只要引入 hibernate核心包和struts2 包 就可以了 -------------------------------------------------HibernateSessionFactory.java--------------

7、 package action; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Configuration; /** * Configures and provides access to Hibernate sessions, tied to the * current thread of execution. Follows the Thread Local Session * pat

8、tern, see {@link http://hibernate.org/42.html }. */ public class HibernateSessionFactory {     /**       * Location of hibernate.cfg.xml file.      * Location should be on the classpath as Hibernate uses       * #resourceAsStream style lookup for its configuration file.       * The default cl

9、asspath location of the hibernate config file is       * in the default package. Use #setConfigFile() to update       * the location of the configuration file for the current session.         */     private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml"; private static final ThreadLo

10、cal threadLocal = new ThreadLocal();     private static Configuration configuration = new Configuration();     private static org.hibernate.SessionFactory sessionFactory;     private static String configFile = CONFIG_FILE_LOCATION; static {     try {     configuration.configu

11、re(configFile);     sessionFactory = configuration.buildSessionFactory();    } catch (Exception e) {     System.err       .println("%%%% Error Creating SessionFactory %%%%");     e.printStackTrace();    }     }     private HibernateSessionFactory() {     } /**      * Returns the ThreadL

12、ocal Session instance. Lazy initialize      * the SessionFactory if needed.      *      * @return Session      * @throws HibernateException      */     public static Session getSession() throws HibernateException {         Session session = (Session) threadLocal.get();    if (se

13、ssion == null || !session.isOpen()) {     if (sessionFactory == null) {      rebuildSessionFactory();     }     session = (sessionFactory != null) ? sessionFactory.openSession()       : null;     threadLocal.set(session);    }         return session;     } /**      * Rebuild hibernate ses

14、sion factory      *      */ public static void rebuildSessionFactory() {    try {     configuration.configure(configFile);     sessionFactory = configuration.buildSessionFactory();    } catch (Exception e) {     System.err       .println("%%%% Error Creating SessionFactory %%%%");     e.pr

15、intStackTrace();    } } /**      * Close the single hibernate session instance.      *      * @throws HibernateException      */     public static void closeSession() throws HibernateException {         Session session = (Session) threadLocal.get();         threadLocal.set(null);        

16、if (session != null) {             session.close();         }     } /**      * return session factory      *      */ public static org.hibernate.SessionFactory getSessionFactory() {    return sessionFactory; } /**      * return session factory      *      * session factory will be rebu

17、ilded in the next call      */ public static void setConfigFile(String configFile) {    HibernateSessionFactory.configFile = configFile;    sessionFactory = null; } /**      * return hibernate configuration      *      */ public static Configuration getConfiguration() {    return configur

18、ation; } } --------------------------------------------------Admin.java-------------------------------------------------- package action;   /** * Admin generated by MyEclipse Persistence Tools */ public class Admin implements java.io.Serializable {     // Fields         private Integer

19、id;      private String user;      private String pass;     // Constructors     /** default constructor */     public Admin() {     } /** minimal constructor */     public Admin(Integer id) {         this.id = id;     }          /** full constructor */     public Admin(Integer id, Str

20、ing user, String pass) {         this.id = id;         this.user = user;         this.pass = pass;     }         // Property accessors     public Integer getId() {         return this.id;     }          public void setId(Integer id) {         this.id = id;     }     public String getUs

21、er() {         return this.user;     }          public void setUser(String user) {         this.user = user;     }     public String getPass() {         return this.pass;     }          public void setPass(String pass) {         this.pass = pass;     }    } --------------------------

22、Admin.xml------------------------------------------

23、pping>                                                                

24、length="20" />                                         -------------------------------------------------hibernate.cfg.xml------------------------------------

25、 root    jdbc:mysql://localhost:3306/test    org.hibernate.dialect.MySQLDialect Mysql r

27、oot    com.mysql.jdbc.Driver 4).建个工具类,把一些方法封装到里面 --------------------------------------------------PagerHelper.java----------------------

28、 package action; import java.util.Collection; import java.util.Iterator; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; /** *  * @author like_dark * 注意:java包命名的时候不能以 ”java“命名,例如 jav

29、a.my.xxx ; 真是奇怪啊 * */ public class PagerHelper { public Collection findWithPage(int pageSize, int startRow) throws HibernateException {    Collection admins = null;    Transaction tx = null;    try {     Session session = HibernateSessionFactory.getSession();     tx =   session.beginTransa

30、ction() ;     Query q = session.createQuery("from Admin");         q.setMaxResults(pageSize);     q.setFirstResult(startRow);     admins = q.list();     mit();    } catch (HibernateException he) {     if (tx != null) {      tx.rollback();     }     throw he;    } finally {     Hibernate

31、SessionFactory.closeSession();    }    return admins; } public int getRows(String query) throws HibernateException {    //query : select count(*) from Admin    int totalRows = 0;    Transaction tx = null;    try {     Session session = HibernateSessionFactory.getSession();     tx = sessio

32、n.beginTransaction();     totalRows = ((Long) session.createQuery(query).list().iterator().next()).intValue() ;     mit();    } catch (HibernateException he) {     if (tx != null) {      tx.rollback();     }     throw he;    } finally {     HibernateSessionFactory.closeSession();    }   

33、return totalRows; } public static void main(String[] args) { PagerHelper ph = new PagerHelper() ; System.out.println("rows:"+ph.getRows("select count(*) from Admin")) ; } } 5) .struts2相关程序和配置 --------------------------------------------------struts.xml----------------------------------------

34、             /index.jsp              /index.jsp        ----------------------------- PageAction.java ---------------------- package action; import java.util.ArrayList; import ja

36、va.util.Collection; import java.util.Map; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class PageAction extends ActionSupport { private int totalRows; //总行数 private int pageSize = Constants.PAGE_SIZE; //每页显示的行数 private int currentPage;

37、 //当前页号 private int totalPages; //总页数 private boolean hasNext ; //是否有下一页 private boolean hasPrevious ; //是否有上一页 private Collection CurrentList; //当前数据集 private Collection indexList ; //页码集合 public Collection getCurrentList() {    return CurrentList; } public void setCurrentList(Collection c

38、urrentList) {    CurrentList = currentList; } /** * 这里最好用一个引用参数化 */ public Collection getIndexList() {       ArrayList result = new ArrayList();    for(int i = 1;i<=getTotalPages();i++){                result.add(new Integer(i));    }       return result ;  } public void setIndexList(C

39、ollection indexList) {    this.indexList = indexList; } public String execute() throws Exception {    int totalRows = 0 ;          try{     PagerHelper ph = new PagerHelper() ;     totalRows =   ph.getRows("select count(*) from Admin") ;    //总行数     totalPages = (totalRows + pageSize - 1)

40、 /pageSize; //总页数     setIndexList(getIndexList()) ; //生成页码集合         if (currentPage==0 || currentPage<1) {      first();     }             if(currentPage > totalPages){             last();             }             if(currentPage >= 1 && currentPage < totalPages){             hasNext = t

41、rue ;             }             if(currentPage > 1 && currentPage <= totalPages){             hasPrevious = true ;             }         /** * 为了利用session 可以实现SessionAware接口 然后重载 void setSession(Map session) 方法 * action implementation SessionAware{  *      private Map session ; *      void

42、 setSession(Map session){ *         this.session = session ; *      }  *      session.put(...); session.get(..) *      或者 继承import com.opensymphony.xwork2.ActionContext; *      ActionContext ctx = ActionContext.getContext();     Map session = ctx.getSession() ; */              CurrentList =

43、ph.findWithPage(Constants.PAGE_SIZE, getStartRow()) ;     return SUCCESS ;    }catch(Exception e){     e.printStackTrace() ;     return ERROR ;    } } public int getCurrentPage() {    return currentPage; } public void setCurrentPage(int currentPage) {    this.currentPage = currentPage; }

44、 public int getPageSize() {    return pageSize; } public void setPageSize(int pageSize) {    this.pageSize = pageSize; } public int getStartRow() {    if(getCurrentPage() == 1)     return 0;    else       return (getCurrentPage()-1) * pageSize;  } public int getTotalPages() {    retu

45、rn totalPages; } public void setTotalPages(int totalPages) {    this.totalPages = totalPages; } public int getTotalRows() {    return totalRows; } public void setTotalRows(int totalRows) {    this.totalRows = totalRows; } public void first() { //首页    currentPage = 1;      } /*public

46、void previous() { //上一页    if (currentPage == 1) {     return;    }    currentPage--; } public void next() {   //下一页    if (currentPage < totalPages) {     currentPage++;    } }*/ public void last() { //尾页    currentPage = totalPages; } public boolean isHasNext() {    return hasNext;

47、 } public void setHasNext(boolean hasNext) {    this.hasNext = hasNext; } public boolean isHasPrevious() {    return hasPrevious; } public void setHasPrevious(boolean hasPrevious) {    this.hasPrevious = hasPrevious; } } ----------------------------struts.properties-----------------

48、 #struts.custom.i18n.resources=globalMessages struts.action.extension=do 5) web相关配置和程序 --------------------- web.xml -----------------------------------

49、a-instance" xsi:schemaLocation="      Struts Blank             struts2         org.apache.struts2.dispatcher.FilterDispatcher                 struts2         /*     <

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服