研究struts和spring也有些日子了,雖然有些地方還不甚瞭解,不過大體已經瞭解struts+spring+hibernate的開發流程了和架構了,對我以後開發.net和java都有不小的協助,感覺收穫不小.下面介紹一下幾個關鍵的地方:
一:利用spring控制事務
1首先在applicationContext.xml配置DataSource
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>sa</value>
</property>
</bean>
以上配置資訊主要是用來串連資料庫的配置.
2其次在applicationContext.xml配置sessionFactory
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>com/java/crud/Hibernate/Curdtest.hbm.xml</value>
<value>com/java/crud/Hibernate/Curdtemp.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
以上主要配置hibernate得session
3配置事務
<bean id="crudtestDao" class="com.java.crud.Dao.CrudtestImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
bean"crudtestDao"是本程式的業務方法用於實現增,刪,改,查,它需要引用session bean.
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
配置hibernate的事務,需要引用session bean.
<bean id="crudtestDaoTarget"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref bean="crudtestDao" />
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean name = "/crud" class = "com.java.curd.struts.action.CrudAction">
<property name="crudTestDao" ref="crudtestDaoTarget"></property>
</bean>
這個配置是spring控制事務的重點首先這個bean是事務工廠代理類,它需要引用三個屬性來配置.
一:transactionManager即hibernate的transactionManager.
二:target你要管理事務的業務bean即要對此bean中的方法進行事務控制.
三:事務工廠屬性.