struts+spring+ibatis
dao层用的是spring对ibatis的包装类,如下:
ScientistDAOImpl extends SqlMapClientDaoSupport implements ScientistDAO
当然事务管理由spring来处理。
以下是spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- ========================= GENERAL DEFINITIONS ========================= -->
<!-- Configurer that replaces ${...} placeholders with values from properties files -->
<!-- (in this case, mail and JDBC related properties) -->
<!-- serviceBean -->
<bean id="changingService" class="cn.edu.ysu.spring.service.ChangingService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<bean id="contractService" class="cn.edu.ysu.spring.service.ContractService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<bean id="projectService" class="cn.edu.ysu.spring.service.ProjectService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<bean id="reportService" class="cn.edu.ysu.spring.service.ReportService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<bean id="scientistService" class="cn.edu.ysu.spring.service.ScientistService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/kyproject"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
<!-- 管理ibatis的事务,加载sql-map-config.xml文件 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" >
<value>
classpath:cn\edu\ysu\dao\sqlmap\sql-map-config.xml
</value>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- daoBean -->
<bean id="changingDAOImpl" class="cn.edu.ysu.dao.daoImplement.ChangingDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="contractDAOImpl" class="cn.edu.ysu.dao.daoImplement.ContractDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="projectDAOImpl" class="cn.edu.ysu.dao.daoImplement.ProjectDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="reportDAOImpl" class="cn.edu.ysu.dao.daoImplement.ReportDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="scientistDAOImpl" class="cn.edu.ysu.dao.daoImplement.ScientistDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<!-- 代理,事务管理器,定义事务 -->
<bean id="daoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"></property>
<property name="target">
<list>
<ref local="scientistService"/>
<ref local="changingService"/>
<ref local="contractService"/>
<ref local="projectService"/>
<ref local="reportService"/>
</list>
</property>
<property name="transactionAttributes" >
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="to*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- 配置事务特性 -->
<!--
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
-->
<!-- 配置哪些类的方法需要事务 -->
<!--
<aop:config>
<aop:pointcut id="allMethod" expression="execution(* cn.edu.ysu.spring.service.ServiceManagerImpl.getScientistService(..))"/>
<aop:aspect id="other" ref="">
<aop:before pointcut-ref="allMethod" method="other"/>
</aop:aspect>
</aop:config>
-->
</beans>
现在的问题是:
好像spring把事务管理放在dao层了,应该放在service层,我也想这么做
例如:
public class ScientistService extends BaseService{
.....部分代码
public void createScientist(Scientist scientist)
{
scientistDao.kyInsert(scientist);
scientist.setBirthday("111");//把日期改成非法数据
scientistDao.kyInsert(scientist);
}
}
如实,在执行下一个插入操作,应抛出数据库异常,单数据库中存有第一条记录
我想把上面那条记录也会滚,即整个createScientist()方法放在一个事务里
应怎样改?
dao层用的是spring对ibatis的包装类,如下:
ScientistDAOImpl extends SqlMapClientDaoSupport implements ScientistDAO
当然事务管理由spring来处理。
以下是spring配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Application context definition for JPetStore's business layer.
- Contains bean references to the transaction manager and to the DAOs in
- dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<!-- ========================= GENERAL DEFINITIONS ========================= -->
<!-- Configurer that replaces ${...} placeholders with values from properties files -->
<!-- (in this case, mail and JDBC related properties) -->
<!-- serviceBean -->
<bean id="changingService" class="cn.edu.ysu.spring.service.ChangingService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<bean id="contractService" class="cn.edu.ysu.spring.service.ContractService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<bean id="projectService" class="cn.edu.ysu.spring.service.ProjectService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<bean id="reportService" class="cn.edu.ysu.spring.service.ReportService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<bean id="scientistService" class="cn.edu.ysu.spring.service.ScientistService">
<property name="changingDao" ref="changingDAOImpl"></property>
<property name="contractDao" ref="contractDAOImpl"></property>
<property name="reportDao" ref="reportDAOImpl"></property>
<property name="projectDao" ref="projectDAOImpl"></property>
<property name="scientistDao" ref="scientistDAOImpl"></property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/kyproject"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
<!-- 管理ibatis的事务,加载sql-map-config.xml文件 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" >
<value>
classpath:cn\edu\ysu\dao\sqlmap\sql-map-config.xml
</value>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- daoBean -->
<bean id="changingDAOImpl" class="cn.edu.ysu.dao.daoImplement.ChangingDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="contractDAOImpl" class="cn.edu.ysu.dao.daoImplement.ContractDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="projectDAOImpl" class="cn.edu.ysu.dao.daoImplement.ProjectDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="reportDAOImpl" class="cn.edu.ysu.dao.daoImplement.ReportDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean id="scientistDAOImpl" class="cn.edu.ysu.dao.daoImplement.ScientistDAOImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<!-- 代理,事务管理器,定义事务 -->
<bean id="daoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"></property>
<property name="target">
<list>
<ref local="scientistService"/>
<ref local="changingService"/>
<ref local="contractService"/>
<ref local="projectService"/>
<ref local="reportService"/>
</list>
</property>
<property name="transactionAttributes" >
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="to*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- 配置事务特性 -->
<!--
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
-->
<!-- 配置哪些类的方法需要事务 -->
<!--
<aop:config>
<aop:pointcut id="allMethod" expression="execution(* cn.edu.ysu.spring.service.ServiceManagerImpl.getScientistService(..))"/>
<aop:aspect id="other" ref="">
<aop:before pointcut-ref="allMethod" method="other"/>
</aop:aspect>
</aop:config>
-->
</beans>
现在的问题是:
好像spring把事务管理放在dao层了,应该放在service层,我也想这么做
例如:
public class ScientistService extends BaseService{
.....部分代码
public void createScientist(Scientist scientist)
{
scientistDao.kyInsert(scientist);
scientist.setBirthday("111");//把日期改成非法数据
scientistDao.kyInsert(scientist);
}
}
如实,在执行下一个插入操作,应抛出数据库异常,单数据库中存有第一条记录
我想把上面那条记录也会滚,即整个createScientist()方法放在一个事务里
应怎样改?