在spring+hibernate中使用tomcat55的jndi数据源文档格式.docx
- 文档编号:22518500
- 上传时间:2023-02-04
- 格式:DOCX
- 页数:15
- 大小:190.75KB
在spring+hibernate中使用tomcat55的jndi数据源文档格式.docx
《在spring+hibernate中使用tomcat55的jndi数据源文档格式.docx》由会员分享,可在线阅读,更多相关《在spring+hibernate中使用tomcat55的jndi数据源文档格式.docx(15页珍藏版)》请在冰豆网上搜索。
auth="
Container"
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"
/Context>
编写完成后,保存退出。
再修改在eclipse中建立的web工程的web.xml文件,其代码如下:
?
xmlversion="
1.0"
encoding="
UTF-8"
web-appxmlns="
xmlns:
xsi="
http:
//www.w3.org/2001/XMLSchema-instance"
version="
2.4"
xsi:
schemaLocation="
…
resource-ref>
res-ref-name>
jdbc/pubs<
/res-ref-name>
res-type>
javax.sql.DataSource<
/res-type>
res-auth>
Container<
/res-auth>
/resource-ref>
/web-app>
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);
savesuccessful"
}catch(RuntimeExceptionre){
log.error("
savefailed"
re);
throwre;
publicvoiddelete(EmppersistentInstance){
deletingEmpinstance"
getHibernateTemplate().delete(persistentInstance);
deletesuccessful"
deletefailed"
publicEmpfindById(java.lang.Integerid){
gettingEmpinstancewithid:
"
+id);
Empinstance=(Emp)getHibernateTemplate()
.get("
com.po.Emp"
id);
returninstance;
getfailed"
publicListfindByExample(Empinstance){
findingEmpinstancebyexample"
Listresults=getHibernateTemplate().findByExample(instance);
findbyexamplesuccessful,resultsize:
+results.size());
returnresults;
findbyexamplefailed"
}
publicListfindByProperty(StringpropertyName,Objectvalue){
findingEmpinstancewithproperty:
+propertyName
+"
value:
+value);
StringqueryString="
fromEmpasmodelwheremodel."
+propertyName+"
=?
"
returngetHibernateTemplate().find(queryString,value);
findbypropertynamefailed"
publicListfindByEname(Objectename){
returnfindByProperty(ENAME,ename);
publicListfindByAddress(Objectaddress){
returnfindByProperty(ADDRESS,address);
publicEmpmerge(EmpdetachedInstance){
mergingEmpinstance"
Empresult=(Emp)getHibernateTemplate()
.merge(detachedInstance);
mergesuccessful"
returnresult;
mergefailed"
publicvoidattachDirty(Empinstance){
attachingdirtyEmpinstance"
getHibernateTemplate().saveOrUpdate(instance);
attachsuccessful"
attachfailed"
publicvoidattachClean(Empinstance){
attachingcleanEmpinstance"
getHibernateTemplate().lock(instance,LockMode.NONE);
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="
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文件的代码如下所示:
DOCTYPEstruts-configPUBLIC"
-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.1//EN"
//jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"
struts-config>
data-sources/>
form-beans/>
global-exceptions/>
global-forwards/>
action-mappings>
actionpath="
/emp"
org.springframework.web.struts.DelegatingActionProxy"
/>
/action-mappings>
message-resourcesparameter="
com.struts.ApplicationResources"
plug-inclassName="
org.springframework.web.struts.ContextLoaderPlugIn"
set-propertyproperty="
contextConfigLocation"
value="
/WEB-INF/applicationContext.xml"
/plug-in>
/struts-config>
10,在spring的applicationContext.xml文件中加入数据源dataSource,使用tomcat5.5.9中配置的JNDI数据源,则完整的applicationContext.xml文件如下所示:
DOCTYPEbeansPUBLIC"
-//SPRING//DTDBEAN//EN"
//www.springframework.org/dtd/spring-beans.dtd"
beans>
beanid="
dataSource"
class="
org.springframework.jndi.JndiObjectFactoryBean"
<
propertyname="
jndiName"
<
value>
java:
comp/env/jdbc/pubs<
/value>
/property>
/bean>
sessionFactory"
org.springframework.orm.hibernate3.LocalSessionFactoryBean"
refbean="
hibernateProperties"
props>
<
propkey="
hibernate.dialect"
org.hibernate.dialect.SQLServerDialect<
/prop>
/props>
mappingResources"
list>
com/po/Emp.hbm.xml<
/list>
class="
com.po.EmpDAO"
beanname="
com.struts.action.EmpAction"
edao"
/beans>
配置图如下所示:
最后,启动tomcat5.5.9测试我们的工程,在ie中测试如下所示,若果没有出现错误提示,则说明测试通过,否则,按上述步骤,重新检查配置过程。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- spring hibernate 使用 tomcat55 jndi 数据源