SSH一个简单的STRUTSHIBERNATE例子以及STRUTSHIBERNATE集成.docx

上传人:b****8 文档编号:10957585 上传时间:2023-02-24 格式:DOCX 页数:12 大小:18.30KB
下载 相关 举报
SSH一个简单的STRUTSHIBERNATE例子以及STRUTSHIBERNATE集成.docx_第1页
第1页 / 共12页
SSH一个简单的STRUTSHIBERNATE例子以及STRUTSHIBERNATE集成.docx_第2页
第2页 / 共12页
SSH一个简单的STRUTSHIBERNATE例子以及STRUTSHIBERNATE集成.docx_第3页
第3页 / 共12页
SSH一个简单的STRUTSHIBERNATE例子以及STRUTSHIBERNATE集成.docx_第4页
第4页 / 共12页
SSH一个简单的STRUTSHIBERNATE例子以及STRUTSHIBERNATE集成.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

SSH一个简单的STRUTSHIBERNATE例子以及STRUTSHIBERNATE集成.docx

《SSH一个简单的STRUTSHIBERNATE例子以及STRUTSHIBERNATE集成.docx》由会员分享,可在线阅读,更多相关《SSH一个简单的STRUTSHIBERNATE例子以及STRUTSHIBERNATE集成.docx(12页珍藏版)》请在冰豆网上搜索。

SSH一个简单的STRUTSHIBERNATE例子以及STRUTSHIBERNATE集成.docx

SSH一个简单的STRUTSHIBERNATE例子以及STRUTSHIBERNATE集成

(ssh)一个简单的struts,hibernate例子以及struts,hibernate集成

最近在学几个常用框架的集成,下面是一个关于struts+hibernate的集成的例子,大家都知道,任何一个复杂的东西,都是从简单开始的,所以我先说一个struts+hibernate集成,然后再进一步了解,希望对这方面爱好的能有所帮助

一、新建一个项目(如ssh)

二、加入该项目对struts的支持,就是相关struts包,(右击你新建的项目->MyEclipse,选addstruts.........)

三、添加了对struts支持后,然后在struts-config.xml文件里右击,新建->formactionandjsp选项,那样就可以把这三个文件新建好register1.jsp,Register1Action.java,Register1Form.java(你可以把这三个文件发布到tomcat进行测试),测试成功就可以执行下一步

四、新建一个表sstest表,有字段id,username,password,id为主健和自动增1

五,同样的方法添加对hibernate支持

六、对新建的数据表进行hibernate进行映射,打开myhibernate视图,右击你sstest表->选hibernatereversengineering选项,然后把Sstest.hbm.xml文件映射到hibernateDao包中,同时选中工具hibernatemappingfile..., javadataobject....., javadataaccessobjectdao...选项,先中这几个文件就可以产生下列几个.java文件:

BaseHibernateDAO.,HibernateSessionFactory,IBaseHibernateDAO,Sstest,SstestDAO,最后你对你的代码进行测试,测试成功,进行下一步

七、好了,关于struts和hibernate代码基本上差不好了,下面来看一直代码,代码为蓝色的就是集成的地方,主要是看Register1Action代码,其它的代码都是自动生成的

相关代码如下:

register1.jsp

<%@pagelanguage="java"pageEncoding="ISO-8859-1"%>

<%@tagliburi="http:

//struts.apache.org/tags-bean"prefix="bean"%>

<%@tagliburi="http:

//struts.apache.org/tags-html"prefix="html"%> 

 

  JSPforRegister1Formform

 

 

  

formaction="/register1">

   password:

passwordproperty="password"/>

errorsproperty="password"/>

   username:

textproperty="username"/>

errorsproperty="username"/>

   

submit/>

cancel/>

  

form>

 

Register1Action.java 

packagecom.yourcompany.struts.action;

importhibernateDao.Sstest;

importhibernateDao.SstestDAO;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importorg.apache.struts.action.Action;

importorg.apache.struts.action.ActionForm;

importorg.apache.struts.action.ActionForward;

importorg.apache.struts.action.ActionMapping;

importorg.hibernate.Transaction;

importcom.yourcompany.struts.form.Register1Form;

publicclassRegister1ActionextendsAction{

 //数据dao

 SstestDAOsd;

 publicRegister1Action(){

  //TODOAuto-generatedconstructorstub

  setSd(newSstestDAO());

 }

 publicActionForwardexecute(ActionMappingmapping,ActionFormform,

   HttpServletRequestrequest,HttpServletResponseresponse){

  Register1Formregister1Form=(Register1Form)form;//TODOAuto-generatedmethodstub

  //输出客户端的用户名

  response.setCharacterEncoding("GB2312");

  System.out.println("register1Form.name\t"+register1Form.getUsername());

  /*

  *数据库操作

  **/

  Sstestst=newSstest(); 

  st.setUsername(register1Form.getUsername());

  st.setPassword(register1Form.getPassword());

  //dao对象

  SstestDAOsd=getSd();

  Transactiontran=sd.getSession().beginTransaction();//开始事务

  sd.save(st);

  mit();

  //请求转发到success

  returnmapping.findForward("success");

 }

 //数据dao,get,set方法

 publicvoidsetSd(SstestDAOsd){

  this.sd=sd;

 }

 publicSstestDAOgetSd(){

  returnsd;

 }

}

Register1Form.java

packagecom.yourcompany.struts.form;

importjavax.servlet.http.HttpServletRequest;

importorg.apache.struts.action.ActionErrors;

importorg.apache.struts.action.ActionForm;

importorg.apache.struts.action.ActionMapping;

publicActionErrorsvalidate(ActionMappingmapping,

   HttpServletRequestrequest){

  //TODOAuto-generatedmethodstub

  returnnull;

 }

 publicvoidreset(ActionMappingmapping,HttpServletRequestrequest){

  //TODOAuto-generatedmethodstub

  this.username="qin";

 }

 publicStringgetPassword(){

  returnpassword;

 }

 publicvoidsetPassword(Stringpassword){

  this.password=password;

 }

 publicStringgetUsername(){

  returnusername;

 }

 publicvoidsetUsername(Stringusername){

  this.username=username;

 }

}

如果你觉得你的代码自动生成有问题的话,你可以参考下列代码

BaseHibernateDAO.java

packagehibernateDao;

importorg.hibernate.Session;

publicclassBaseHibernateDAOimplementsIBaseHibernateDAO{

 

 publicSessiongetSession(){

  returnHibernateSessionFactory.getSession();

 } 

}

HibernateSessionFactory.java

packagehibernateDao;

importorg.hibernate.HibernateException;

importorg.hibernate.Session;

importorg.hibernate.cfg.Configuration;

publicclassHibernateSessionFactory{

   privatestaticStringCONFIG_FILE_LOCATION="/hibernate.cfg.xml";

 privatestaticfinalThreadLocalthreadLocal=newThreadLocal();

   private staticConfigurationconfiguration=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(){

   } 

   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;

   }

 publicstaticvoidrebuildSessionFactory(){

  try{

   configuration.configure(configFile);

   sessionFactory=configuration.buildSessionFactory();

  }catch(Exceptione){

   System.err

     .println("%%%%ErrorCreatingSessionFactory%%%%");

   e.printStackTrace();

  }

 }

   publicstaticvoidcloseSession()throwsHibernateException{

       Sessionsession=(Session)threadLocal.get();

       threadLocal.set(null);

       if(session!

=null){

           session.close();

       }

   }

 publicstaticorg.hibernate.SessionFactorygetSessionFactory(){

  returnsessionFactory;

 }

 publicstaticvoidsetConfigFile(StringconfigFile){

  HibernateSessionFactory.configFile=configFile;

  sessionFactory=null;

 }

 publicstaticConfigurationgetConfiguration(){

  returnconfiguration;

 }

}

IBaseHibernateDAO.java

packagehibernateDao;

importorg.hibernate.Session;

/**

 *Dataaccessinterfacefordomainmodel

 *@authorMyEclipsePersistenceTools

 */

publicinterfaceIBaseHibernateDAO{

 publicSessiongetSession();

}

     Sstest.java

packagehibernateDao;

ublicclassSstestimplementsjava.io.Serializable{

 //Fields

 privateIntegerid;

 privateStringusername;

 privateStringpassword;

 //Constructors

 /**defaultconstructor*/

 publicSstest(){

 }

 /**fullconstructor*/

 publicSstest(Stringusername,Stringpassword){

  this.username=username;

  this.password=password;

 }

 //Propertyaccessors

 publicIntegergetId(){

  returnthis.id;

 }

 publicvoidsetId(Integerid){

  this.id=id;

 }

 publicStringgetUsername(){

  returnthis.username;

 }

 publicvoidsetUsername(Stringusername){

  this.username=username;

 }

 publicStringgetPassword(){

  returnthis.password;

 }

 publicvoidsetPassword(Stringpassword){

  this.password=password;

 }

}

SstestDAO.java

packagehibernateDao;

importjava.util.List;

importmons.logging.Log;

importmons.logging.LogFactory;

importorg.hibernate.LockMode;

importorg.hibernate.Query;

importorg.hibernate.criterion.Example;

publicclassSstestDAOextendsBaseHibernateDAO{

 privatestaticfinalLoglog=LogFactory.getLog(SstestDAO.class);

 //propertyconstants

 publicstaticfinalStringUSERNAME="username";

 publicstaticfinalStringPASSWORD="password";

 publicvoidsave(SstesttransientInstance){

  log.debug("savingSstestinstance");

  try{

   getSession().save(transientInstance);

   log.debug("savesuccessful");

  }catch(RuntimeExceptionre){

   log.error("savefailed",re);

   throwre;

  }

 }

 publicvoiddelete(SstestpersistentInstance){

  log.debug("deletingSstestinstance");

  try{

   getSession().delete(persistentInstance);

   log.debug("deletesuccessful");

  }catch(RuntimeExceptionre){

   log.error("deletefailed",re);

   throwre;

  }

 }

 publicSstestfindById(java.lang.Integerid){

  log.debug("gettingSstestinstancewithid:

"+id);

  try{

   Sstestinstance=(Sstest)getSession().get("hibernateDao.Sstest",

     id);

   returninstance;

  }catch(RuntimeExceptionre){

   log.error("getfailed",re);

   throwre;

  }

 }

 publicListfindByExample(Sstestinstance){

  log.debug("findingSstestinstancebyexample");

  try{

   Listresults=getSession().createCriteria("hibernateDao.Sstest")

     .add(Example.create(instance)).list();

   log.debug("findbyexamplesuccessful,resultsize:

"

     +results.size());

   returnresults;

  }catch(RuntimeExceptionre){

   log.error("findbyexamplefailed",re);

   throwre;

  }

 }

 publicListfindByProperty(StringpropertyName,Objectvalue){

  log.debug("findingSstestinstancewithproperty:

"+propertyName

    +",value:

"+value);

  try{

   StringqueryString="fromSstestasmodelwheremodel."

     +propertyName+"=?

";

   QueryqueryObject=getSession().createQuery(queryString);

   queryObject.setParameter(0,value);

   returnqueryObject.list();

  }catch(RuntimeExceptionre){

   log.error("findbypropertynamefailed",re);

   throwre;

  }

 }

 publicListfindByUsername(Objectusername){

  returnfindByProperty(USERNAME,username);

 }

 publicListfindByPassword(Objectpassword){

  returnfindByProperty(PASSWORD,password);

 }

 publicListfindAll(){

  log.debug("findingallSstestinstances");

  try{

   StringqueryString="fromSstest";

   QueryqueryObject=getSession().createQuery(queryString);

   returnqueryObject.list();

  }c

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 自然景观

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1