Ajax+Struts2+Spring+Hibernate整合

我们沿用Spring+Hibernate+DWR的例子,做一个Ajax+Struts2.0+Spring+Hibernate整合,许多步骤大体相似,
我就直接搬过来了:
首先,建一个web project,然后添加对hibernate和spring的支持,我们使用的是hibernate3.2和spring2.0,然后导入xwork-2.0.3.jar struts2-core-2.0.8.jar struts2-spring-plugin-2.0.8.jar ognl-2.6.11.jar freemarker-2.3.8.jar commons-logging-api-1.1.jar和commons-pool-1.3.jar(不知道myeclipse怎么搞的,添加Spring功能支持的时候就有了commons-dbcp.jar,居然没有其依赖的commons-pool-x.jar,只好单独添加了,另外,需要将Spring2.0 AOP Liberaries里的asm2.2.3.jar删除,因为好像和Hiberate中的生成代理用的asm.jar冲突,我把Spring2.0 AOP Liberaries排到最后仍然有冲突,所以只好删掉了,不知道大家遇到过这种情况么)。
我们使用myeclise自带的Derby数据库,在里面建一个表BOOK:
ID bigint primary key,autoincrement
NAME varchar(20)
ISBM varchar(20)
AUTHOR varchar(15)
然后利用myeclipse的hibernate反向工程生成领域模型:Book.java, DAO:BookDAO.jar, Book 的映射文件Book.hbm.xml:
生成的代码及配置文件如下:

Book.java:

package edu.jlu.fuliang.domain;

/***/ /**
*BookgeneratedbyMyEclipsePersistenceTools
*/


public class Book implements java.io.Serializable {

//Fields

privatelongid;
privateStringname;
privateStringisbm;
privateStringauthor;

//Constructors

/***//**defaultconstructor*/
publicBook(){
}


/***//**minimalconstructor*/
publicBook(Stringname,Stringisbm){
this.name=name;
this.isbm=isbm;
}


/***//**fullconstructor*/
publicBook(Stringname,Stringisbm,Stringauthor){
this.name=name;
this.isbm=isbm;
this.author=author;
}


//Propertyaccessors

publiclonggetId(){
returnthis.id;
}


publicvoidsetId(longid){
this.id=id;
}


publicStringgetName(){
returnthis.name;
}


publicvoidsetName(Stringname){
this.name=name;
}


publicStringgetIsbm(){
returnthis.isbm;
}


publicvoidsetIsbm(Stringisbm){
this.isbm=isbm;
}


publicStringgetAuthor(){
returnthis.author;
}


publicvoidsetAuthor(Stringauthor){
this.author=author;
}


}

BookDAO.java:

package edu.jlu.fuliang.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import edu.jlu.fuliang.domain.Book;

/***/ /**
*Dataaccessobject(DAO)fordomainmodelclassBook.
*
*
@seeedu.jlu.fuliang.domain.Book
*
@authorMyEclipsePersistenceTools
*/


public class BookDAO extends HibernateDaoSupport ... {
privatestaticfinalLoglog=LogFactory.getLog(BookDAO.class);
//propertyconstants
publicstaticfinalStringNAME="name";
publicstaticfinalStringISBM="isbm";
publicstaticfinalStringAUTHOR="author";

protectedvoidinitDao()...{
//donothing
}


publicvoidsave(BooktransientInstance)...{
log.debug(
"savingBookinstance");
try...{
getHibernateTemplate().save(transientInstance);
log.debug(
"savesuccessful");
}
catch(RuntimeExceptionre)...{
log.error(
"savefailed",re);
throwre;
}

}


publicvoiddelete(BookpersistentInstance)...{
log.debug(
"deletingBookinstance");
try...{
getHibernateTemplate().delete(persistentInstance);
log.debug(
"deletesuccessful");
}
catch(RuntimeExceptionre)...{
log.error(
"deletefailed",re);
throwre;
}

}


publicBookfindById(Longid)...{
log.debug(
"gettingBookinstancewithid:"+id);
try...{
Bookinstance
=(Book)getHibernateTemplate().get(
"edu.jlu.fuliang.domain.Book",id);
returninstance;
}
catch(RuntimeExceptionre)...{
log.error(
"getfailed",re);
throwre;
}

}


publicListfindByExample(Bookinstance)...{
log.debug(
"findingBookinstancebyexample");
try...{
Listresults
=getHibernateTemplate().findByExample(instance);
log.debug(
"findbyexamplesuccessful,resultsize:"
+results.size());
returnresults;
}
catch(RuntimeExceptionre)...{
log.error(
"findbyexamplefailed",re);
throwre;
}

}


publicListfindByProperty(StringpropertyName,Objectvalue)...{
log.debug(
"findingBookinstancewithproperty:"+propertyName
+",value:"+value);
try...{
StringqueryString
="fromBookasmodelwheremodel."
+propertyName+"=?";
returngetHibernateTemplate().find(queryString,value);
}
catch(RuntimeExceptionre)...{
log.error(
"findbypropertynamefailed",re);
throwre;
}

}


publicListfindByName(Objectname)...{
returnfindByProperty(NAME,name);
}


publicListfindByIsbm(Objectisbm)...{
returnfindByProperty(ISBM,isbm);
}


publicListfindByAuthor(Objectauthor)...{
returnfindByProperty(AUTHOR,author);
}


publicListfindAll()...{
log.debug(
"findingallBookinstances");
try...{
StringqueryString
="fromBook";
returngetHibernateTemplate().find(queryString);
}
catch(RuntimeExceptionre)...{
log.error(
"findallfailed",re);
throwre;
}

}


publicBookmerge(BookdetachedInstance)...{
log.debug(
"mergingBookinstance");
try...{
Bookresult
=(Book)getHibernateTemplate().merge(detachedInstance);
log.debug(
"mergesuccessful");
returnresult;
}
catch(RuntimeExceptionre)...{
log.error(
"mergefailed",re);
throwre;
}

}


publicvoidattachDirty(Bookinstance)...{
log.debug(
"attachingdirtyBookinstance");
try...{
getHibernateTemplate().saveOrUpdate(instance);
log.debug(
"attachsuccessful");
}
catch(RuntimeExceptionre)...{
log.error(
"attachfailed",re);
throwre;
}

}
</span
分享到:
评论

你可能感兴趣的:(spring,AOP,Ajax,Hibernate,MyEclipse)