通常建議采用聲明式事務(wù)管理。聲明式事務(wù)管理的優(yōu)勢(shì)非常明顯:代碼中無需關(guān)于關(guān)注事務(wù)邏輯,讓Spring聲明式事務(wù)管理負(fù)責(zé)事務(wù)邏輯,聲明式事務(wù)管理無需與具體的事務(wù)邏輯耦合,可以方便地在不同事務(wù)邏輯之間切換。 聲明式事務(wù)管理的配置方式,通常有如下三種: 1.使用TransactionProxyFactoryBean為目標(biāo)bean生成事務(wù)代理的配置。此方式是最傳統(tǒng),配置文件最臃腫、難以閱讀的方式。 2.采用bean繼承的事務(wù)代理配置方式,比較簡(jiǎn)潔,但依然是增量式配置。 3.使用BeanNameAutoProxyCreator,根據(jù)bean name自動(dòng)生成事務(wù)代理的方式,這是直接利用Spring的AOP框架配置事務(wù)代理的方式,需要對(duì)Spring的AOP框架有所理解。但這種方式避免了增量式配置,效果非常不錯(cuò)。 4.DefaultAdvisorAutoProxyCreator:這也是直接利用Spring的AOP框架配置事務(wù)代理的方式,效果也非常不多,只是這種配置方式的可讀性不如第三種方式。
一. 利用TransactionProxyFactoryBean生成事務(wù)代理 采用這種方式的配置時(shí)候,配置文件的增加非???,每個(gè)bean有需要兩個(gè)bean配置,一個(gè)目標(biāo),另外還需要使用TransactionProxyFactoryBean配置一個(gè)代理bean。 這是一種最原始的配置方式,下面是使用TransactionProxyFactoryBean的配置文件:
<?xml version="1.0" encoding="gb2312"?>
<!-- Spring配置文件的文件頭,包含DTD等信息-->
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www./dtd/spring-beans.dtd">
<beans>
<!--定義數(shù)據(jù)源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 定義數(shù)據(jù)庫驅(qū)動(dòng)-->
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
<!-- 定義數(shù)據(jù)庫url-->
<property name="url"><value>jdbc:mysql://localhost:3306/spring</value></property>
<!-- 定義數(shù)據(jù)庫用戶名-->
<property name="username"><value>root</value></property>
<!-- 定義數(shù)據(jù)庫密碼-->
<property name="password"><value>32147</value></property>
</bean>
<!--定義一個(gè)hibernate的SessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 定義SessionFactory必須注入DataSource-->
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<!--以下用來列出所有的PO映射文件-->
<value>Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!--此處用來定義hibernate的SessionFactory的屬性:
不同數(shù)據(jù)庫連接,啟動(dòng)時(shí)選擇create,update,create-drop-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- 定義事務(wù)管理器,使用適用于Hibernte的事務(wù)管理器-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- HibernateTransactionManager bean需要依賴注入一個(gè)SessionFactory bean的引用-->
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<!--定義DAO Bean , 作為事務(wù)代理的目標(biāo)-->
<bean id="personDaoTarget" class="lee.PersonDaoHibernate">
<!-- 為DAO bean注入SessionFactory引用-->
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
<!-- 定義DAO bean的事務(wù)代理-->
<bean id="personDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 為事務(wù)代理bean注入事務(wù)管理器-->
<property name="transactionManager"><ref bean="transactionManager"/></property>
<!-- 設(shè)置事務(wù)屬性-->
<property name="transactionAttributes">
<props>
<!-- 所有以find開頭的方法,采用required的事務(wù)策略,并且只讀-->
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<!-- 其他方法,采用required的事務(wù)策略 ->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<!-- 為事務(wù)代理bean設(shè)置目標(biāo)bean -->
<property name="target">
<ref local="personDaoTarget"/>
</property>
</bean>
</beans>
在上面的配置文件中,personDao需要配置兩個(gè)部分,一個(gè)是personDao的目標(biāo)bean,該目標(biāo)bean是實(shí)際DAO bean,以實(shí)際的DAO bean為目標(biāo),建立事務(wù)代理。一個(gè)組件,需要來個(gè)bean組成,一個(gè)目標(biāo)bean,一個(gè)事務(wù)代理。 這種配置方式還有一個(gè)壞處:目標(biāo)bean直接暴露在Spring容器中,可以直接引用,如果目標(biāo)bean被誤引用,將導(dǎo)致業(yè)務(wù)操作不具備事務(wù)性。 為了避免這種現(xiàn)象,可將目標(biāo)bean配置成嵌套bean,下面是目標(biāo)bean和事務(wù)代理的配置片段:
<!-- 定義DAO bean的事務(wù)代理-->
<bean id="personDao" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 為事務(wù)代理bean注入事務(wù)管理器-->
<property name="transactionManager"><ref bean="transactionManager"/></property>
<!-- 設(shè)置事務(wù)屬性-->
<property name="transactionAttributes">
<props>
<!-- 所有以find開頭的方法,采用required的事務(wù)策略,并且只讀-->
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<!-- 其他方法,采用required的事務(wù)策略 ->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<!-- 為事務(wù)代理bean設(shè)置目標(biāo)bean -->
<property name="target">
<!-- 采用嵌套bean配置目標(biāo)bean-->
<bean class="lee.PersonDaoHibernate">
<!-- 為DAO bean注入SessionFactory引用-->
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
</property>
</bean>
====================================================================================
使用HibernateTemplate的方法進(jìn)行CRUD操作,其中查詢操作通??煞譃閮煞N,一種為固定條件查詢,另一種為動(dòng)態(tài)多條件查詢(如查詢界面
的實(shí)現(xiàn)),固定條件查詢可以很方便地通過createQuery,find()等方法實(shí)現(xiàn),但是我在動(dòng)態(tài)條件查詢的實(shí)現(xiàn)過程中,hibernate3.0
可以實(shí)現(xiàn)英文的條件查詢,而中文條件則會(huì)出現(xiàn)亂碼。現(xiàn)將具體的過程描述如下:
1.固定條件查詢
可以使用常規(guī)的方法,如getHibernateTemplate().find(),getHibernateTemplate().createQuery()等
2.動(dòng)態(tài)多條件查詢
由于查詢條件的不確定性,我曾嘗試用拼參數(shù)的方法將拼好的sql語句傳入find(qlStr),但是查詢時(shí)hibernate會(huì)將中文的條件報(bào)為亂碼。不過如果條件全部是英文參數(shù)的話拼sql是可以的。亂碼報(bào)錯(cuò)如下: 3:49,946 INFO [STDOUT] Hibernate: select
incometype0_.id as id, incometype0_.name as name0_,
incometype0_.type_comment as type3_0_ from income_type incometype0_
where 1=1 and incometype0_.type_comment='·á????×?????' 因此這種方法無法使用。另外find()的另一種find(String arg0,Object[] arg1),采用數(shù)組參數(shù)將sql的條件參數(shù)傳入的方式只是適合固定條件參數(shù)的查詢,不適合這種動(dòng)態(tài)多條件的中文查詢,因此也無法使用。
說明:由于find(String arg0,Object[] arg1)采用數(shù)組參數(shù)的方式可以使用中文條件查詢,因此可以確定不是我的編碼問題。而是Hibernate3.0的find(sqlStr)方法本身的問題。
為此只能換成另一種實(shí)現(xiàn)途徑,如下:
實(shí)現(xiàn)途徑: 得到session ,用Query q =
session.createQuery(sql);該方法返回一個(gè)Query 類型,利用q.setString(String
arg0,String arg1)將參數(shù)賦值給sql的參數(shù)條件。在sql語句中拼一次參數(shù),在setString()中也拼一次賦值。 如: ①
StringBuffer sql = new StringBuffer();
sql.append("from IncomeType where 1=1 ");
if(id!=null&&id.length()>0)
sql.append(" and id = :id ");
if(name!=null&&name.length()>0)
sql.append(" and name = :name ");
if(typecomment!=null&&typecomment.length()>0)
sql.append(" and typeComment = :tc ");

final String typeSql = new String(sql);
②
intyList = (List)getHibernateTemplate().execute(
 new HibernateCallback() ...{
public Object doInHibernate(Session session)
 throws HibernateException, SQLException ...{
Query q = session.createQuery(typeSql);
if(id!=null&&id.length()>0)
q.setString("id",id);
if(name!=null&&name.length()>0)
q.setString("name",name);
if(typecomment!=null&&typecomment.length()>0)
q.setString("tc",typecomment);
return q.list();
}
}
)

以上方法可以實(shí)現(xiàn)動(dòng)態(tài)中文條件查詢,在有的書中看到并不推薦用find()方法,find()只提供一些簡(jiǎn)單的HQL查詢,不具有動(dòng)態(tài)綁定參數(shù)的功
能,在將來的hibernate新版本中,有可能會(huì)淘汰find()方法,而Query接口才是真正的HQL查詢接口,提供更為豐富的功能?;诖?,可能
對(duì)于一個(gè)將被淘汰的方法find()不支持中文也就不足為奇了。^_^畢竟人家重心轉(zhuǎn)移了。而且find()中拼sql字符串的方式雖然是實(shí)現(xiàn)查詢的常用
手段,實(shí)現(xiàn)起來也比較方便,但是不利于hibernate更好地利用緩存,而采用Query接口可以更好地利用緩存,提高程序執(zhí)行效率。
|