在spring+hibernate中使用tomcat55的jndi数据源.docx
- 文档编号:9382446
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:15
- 大小:190.75KB
在spring+hibernate中使用tomcat55的jndi数据源.docx
《在spring+hibernate中使用tomcat55的jndi数据源.docx》由会员分享,可在线阅读,更多相关《在spring+hibernate中使用tomcat55的jndi数据源.docx(15页珍藏版)》请在冰豆网上搜索。
在spring+hibernate中使用tomcat55的jndi数据源
在spring中使用tomcat提供的JNDI数据源
JNDI是J2EE中一个很重要的标准,通常我们是在J2EE编程中用到,Tomcat中提供了在JSP和Servelt中直接使用JNDI的方法,主要是通过dbcp连接池,下面谈一下我在Tomcat5.5中配置和使用JNDI的方法。
在eclispe中使用myeclipse建立web工程。
然后按照以下步骤,完成此项目的配置流程。
1,首先需要在如下目录中建立工程所在的发布的xml文件
2,编辑如图所示的myprj.xml文件,其内容如下所示:
\myprj\WebRoot" privileged="true"antiResourceLocking="false"antiJARLocking="false"> --Linktotheuserdatabasewewillgetrolesfrom--> type="org.apache.catalina.UserDatabase"/> auth="Container" type="javax.sql.DataSource" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver" url="jdbc: microsoft: sqlserver: //localhost: 1433;DatabaseName=pubs" username="sa" password="123456" maxActive="20" maxIdle="10" maxWait="10000"/> 编写完成后,保存退出。 再修改在eclipse中建立的web工程的web.xml文件,其代码如下: xmlversion="1.0"encoding="UTF-8"? > xsi="http: //www.w3.org/2001/XMLSchema-instance"version="2.4"xsi: schemaLocation=" … 3,在数据库的查询分析其中建立emp表 createtableemp( eidintidentityprimarykey, enamevarchar(50), addressvarchar(100) ) 4,在eclipse中使用myeclipse中先加入Hibernate组件,并使用数据库查看器建立与mssqlserver的连接,要求能够看到数据库的emp表。 5,添加spring组件,并加入Hibernate的sessionFactory,用spring管理Hibernate的数据库连接。 这时系统会自动生成applicationContext.xml文件。 6,使用Myeclipse自带的数据库浏览视图,将emp表生成为相应得持久化类 在生成过程中,选择生成spring的dao类,生成完成后会出现如下的结构: 其中EmpDAO的代码如下: packagecom.po; importjava.util.List; importmons.logging.Log; importmons.logging.LogFactory; importorg.hibernate.LockMode; importorg.springframework.context.ApplicationContext; importorg.springframework.orm.hibernate3.support.HibernateDaoSupport; /** *Dataaccessobject(DAO)fordomainmodelclassEmp. *@seecom.po.Emp *@authorMyEclipse-HibernateTools */ publicclassEmpDAOextendsHibernateDaoSupport{ privatestaticfinalLoglog=LogFactory.getLog(EmpDAO.class); //propertyconstants publicstaticfinalStringENAME="ename"; publicstaticfinalStringADDRESS="address"; protectedvoidinitDao(){ //donothing } publicvoidsave(EmptransientInstance){ log.debug("savingEmpinstance"); try{ getHibernateTemplate().save(transientInstance); log.debug("savesuccessful"); }catch(RuntimeExceptionre){ log.error("savefailed",re); throwre; } } publicvoiddelete(EmppersistentInstance){ log.debug("deletingEmpinstance"); try{ getHibernateTemplate().delete(persistentInstance); log.debug("deletesuccessful"); }catch(RuntimeExceptionre){ log.error("deletefailed",re); throwre; } } publicEmpfindById(java.lang.Integerid){ log.debug("gettingEmpinstancewithid: "+id); try{ Empinstance=(Emp)getHibernateTemplate() .get("com.po.Emp",id); returninstance; }catch(RuntimeExceptionre){ log.error("getfailed",re); throwre; } } publicListfindByExample(Empinstance){ log.debug("findingEmpinstancebyexample"); try{ Listresults=getHibernateTemplate().findByExample(instance); log.debug("findbyexamplesuccessful,resultsize: "+results.size()); returnresults; }catch(RuntimeExceptionre){ log.error("findbyexamplefailed",re); throwre; } } publicListfindByProperty(StringpropertyName,Objectvalue){ log.debug("findingEmpinstancewithproperty: "+propertyName +",value: "+value); try{ StringqueryString="fromEmpasmodelwheremodel." +propertyName+"=? "; returngetHibernateTemplate().find(queryString,value); }catch(RuntimeExceptionre){ log.error("findbypropertynamefailed",re); throwre; } } publicListfindByEname(Objectename){ returnfindByProperty(ENAME,ename); } publicListfindByAddress(Objectaddress){ returnfindByProperty(ADDRESS,address); } publicEmpmerge(EmpdetachedInstance){ log.debug("mergingEmpinstance"); try{ Empresult=(Emp)getHibernateTemplate() .merge(detachedInstance); log.debug("mergesuccessful"); returnresult; }catch(RuntimeExceptionre){ log.error("mergefailed",re); throwre; } } publicvoidattachDirty(Empinstance){ log.debug("attachingdirtyEmpinstance"); try{ getHibernateTemplate().saveOrUpdate(instance); log.debug("attachsuccessful"); }catch(RuntimeExceptionre){ log.error("attachfailed",re); throwre; } } publicvoidattachClean(Empinstance){ log.debug("attachingcleanEmpinstance"); try{ getHibernateTemplate().lock(instance,LockMode.NONE); log.debug("attachsuccessful"); }catch(RuntimeExceptionre){ log.error("attachfailed",re); throwre; } } publicstaticEmpDAOgetFromApplicationContext(ApplicationContextctx){ return(EmpDAO)ctx.getBean("EmpDAO"); } } 7,添加struts组件,并建立EmpAction,用于测试最终的结果。 EmpAction代码如下: /* *GeneratedbyMyEclipseStruts *Templatepath: templates/java/JavaClass.vtl */ packagecom.struts.action; 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.springframework.context.support.*; importorg.springframework.web.context.ContextLoader; importorg.springframework.web.struts.DelegatingActionProxy; importcom.po.*; /** *MyEclipseStruts *Creationdate: 03-05-2007 * *XDocletdefinition: *@struts.actionvalidate="true" */ publicclassEmpActionextendsAction{ /* *GeneratedMethods */ /** *Methodexecute *@parammapping *@paramform *@paramrequest *@paramresponse *@returnActionForward */ privateEmpDAOedao; publicActionForwardexecute(ActionMappingmapping,ActionFormform, HttpServletRequestrequest,HttpServletResponseresponse){ //TODOAuto-generatedmethodstub Empemp=newEmp(); edao=this.getEdao(); emp.setEname("hello"); emp.setAddress("good"); edao.save(emp); returnnull; } publicEmpDAOgetEdao(){ returnedao; } publicvoidsetEdao(EmpDAOedao){ this.edao=edao; } } 8,将action组件注入到spring的applicationContext.xml文件中。 9,修改struts-config.xml文件,加入applicationContext.xml的路径(使用spring的插件来完成对applicationContext.xml文件的加载),并使用spring的struts代理类来代理Action,最终struts-config.xml文件的代码如下所示: xmlversion="1.0"encoding="UTF-8"? > DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.1//EN""http: //jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> 10,在spring的applicationContext.xml文件中加入数据源dataSource,使用tomcat5.5.9中配置的JNDI数据源,则完整的applicationContext.xml文件如下所示: xmlversion="1.0"encoding="UTF-8"? > DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http: //www.springframework.org/dtd/spring-beans.dtd"> class="org.springframework.jndi.JndiObjectFactoryBean"> comp/env/jdbc/pubs class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 配置图如下所示: 最后,启动tomcat5.5.9测试我们的工程,在ie中测试如下所示,若果没有出现错误提示,则说明测试通过,否则,按上述步骤,重新检查配置过程。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- spring hibernate 使用 tomcat55 jndi 数据源