您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > struts2 hibernate分页
1).针对网上一些struts2分页的例子不完整,代码混乱繁琐,我特意写了一个分页的小例子,供大家参考,如果程序有什么纰漏,请指出。开发环境:eclipse3.2+myeclipse5.5mysql5.0tomcat5.5Dreamweaver20042)建库,并导入脚本这里给大家介绍一个mysql客户端工具--SQLyog,非常好用。----------------------------------------------------admin.sql--------------------------------------------------/*SQLyogCommunityEdition-MySQLGUIv6.12MySQL-5.0.37-community-nt:Database-test把这段脚本导入到mysql的test库里面(你可以先建一个test库,这样会省去很多麻烦)**********************************************************************//*!40101SETNAMESutf8*/;/*!40101SETSQL_MODE=''*/;createdatabaseifnotexists`test`;USE`test`;/*!40014SET@OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,FOREIGN_KEY_CHECKS=0*/;/*!40101SET@OLD_SQL_MODE=@@SQL_MODE,SQL_MODE='NO_AUTO_VALUE_ON_ZERO'*/;/*Tablestructurefortable`admin`*/DROPTABLEIFEXISTS`admin`;CREATETABLE`admin`(`id`int(11)NOTNULLauto_increment,`user`char(20)defaultNULL,`pass`char(20)defaultNULL,PRIMARYKEY(`id`))ENGINE=InnoDBDEFAULTCHARSET=gb2312;/*Dataforthetable`admin`*/insertinto`admin`(`id`,`user`,`pass`)values(786432,'天使','#####'),(786433,'天使','#####'),(786434,'天使','#####'),(786435,'天使','#####'),(786436,'天使','#####'),(819200,'天使','#####'),(819201,'天使','#####'),(819202,'天使','#####'),(819203,'天使','#####'),(819204,'天使','#####'),(819205,'wangbacheng','#####'),(851968,'天使','#####'),(851969,'天使','#####'),(851970,'天使','#####'),(851971,'天使','#####'),(851972,'天使','#####');/*!40101SETSQL_MODE=@OLD_SQL_MO*/;/*!40014SETFOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS*/;DE3)建立数据库连接程序,也就是hibernate相关程序,都可以用eclipse自动生成,这个不用多说在eclipse中只要引入hibernate核心包和struts2包就可以了-------------------------------------------------HibernateSessionFactory.java-------------------------------------packageaction;importorg.hibernate.HibernateException;importorg.hibernate.Session;importorg.hibernate.cfg.Configuration;/***ConfiguresandprovidesaccesstoHibernatesessions,tiedtothe*currentthreadofexecution.FollowstheThreadLocalSession*pattern,see{@link}.*/publicclassHibernateSessionFactory{/***Locationofhibernate.cfg.xmlfile.*LocationshouldbeontheclasspathasHibernateuses*#resourceAsStreamstylelookupforitsconfigurationfile.*Thedefaultclasspathlocationofthehibernateconfigfileis*inthedefaultpackage.Use#setConfigFile()toupdate*thelocationoftheconfigurationfileforthecurrentsession.*/privatestaticStringCONFIG_FILE_LOCATION=/hibernate.cfg.xml;privatestaticfinalThreadLocalSessionthreadLocal=newThreadLocalSession();privatestaticConfigurationconfiguration=newConfiguration();privatestaticorg.hibernate.SessionFactorysessionFactory;privatestaticStringconfigFile=CONFIG_FILE_LOCATION;static{try{configuration.configure(configFile);sessionFactory=configuration.buildSessionFactory();}catch(Exceptione){System.err.println(%%%%ErrorCreatingSessionFactory%%%%);e.printStackTrace();}}privateHibernateSessionFactory(){}/***ReturnstheThreadLocalSessioninstance.Lazyinitialize*thecodeSessionFactory/codeifneeded.**@returnSession*@throwsHibernateException*/publicstaticSessiongetSession()throwsHibernateException{Sessionsession=(Session)threadLocal.get();if(session==null||!session.isOpen()){if(sessionFactory==null){rebuildSessionFactory();}session=(sessionFactory!=null)?sessionFactory.openSession():null;threadLocal.set(session);}returnsession;}/***Rebuildhibernatesessionfactory**/publicstaticvoidrebuildSessionFactory(){try{configuration.configure(configFile);sessionFactory=configuration.buildSessionFactory();}catch(Exceptione){System.err.println(%%%%ErrorCreatingSessionFactory%%%%);e.printStackTrace();}}/***Closethesinglehibernatesessioninstance.**@throwsHibernateException*/publicstaticvoidcloseSession()throwsHibernateException{Sessionsession=(Session)threadLocal.get();threadLocal.set(null);if(session!=null){session.close();}}/***returnsessionfactory**/publicstaticorg.hibernate.SessionFactorygetSessionFactory(){returnsessionFactory;}/***returnsessionfactory**sessionfactorywillberebuildedinthenextcall*/publicstaticvoidsetConfigFile(StringconfigFile){HibernateSessionFactory.configFile=configFile;sessionFactory=null;}/***returnhibernateconfiguration**/publicstaticConfigurationgetConfiguration(){returnconfiguration;}}--------------------------------------------------Admin.java--------------------------------------------------packageaction;/***AdmingeneratedbyMyEclipsePersistenceTools*/publicclassAdminimplementsjava.io.Serializable{//FieldsprivateIntegerid;privateStringuser;privateStringpass;//Constructors/**defaultconstructor*/publicAdmin(){}/**minimalconstructor*/publicAdmin(Integerid){this.id=id;}/**fullconstructor*/publicAdmin(Integerid,Stringuser,Stringpass){this.id=id;this.user=user;this.pass=pass;}//PropertyaccessorspublicIntegergetId(){returnthis.id;}publicvoidsetId(Integerid){this.id=id;}publicStringgetUser(){returnthis.user;}publicvoidsetUser(Stringuser){this.user=user;}publicStringgetPass(){returnthis.pass;}publicvoidsetPass(Stringpass){this.pass=pass;}}-------------------------------------------------------------Admin.xml------------------------------------------?xmlversion=1.0
本文标题:struts2 hibernate分页
链接地址:https://www.777doc.com/doc-4524537 .html