xsi="http:
//www.w3.org/2001/XMLSchema-instance"version="2.4"xsi:
schemaLocation="
…
jdbc/pubs
javax.sql.DataSource
Container
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">
java:
comp/env/jdbc/pubs
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
org.hibernate.dialect.SQLServerDialect
com/po/Emp.hbm.xml
配置图如下所示:
最后,启动tomcat5.5.9测试我们的工程,在ie中测试如下所示,若果没有出现错误提示,则说明测试通过,否则,按上述步骤,重新检查配置过程。