spring shiwu

博客介绍了Struts、Spring和Ibatis的组合应用,DAO层采用Spring对Ibatis的包装类,事务管理由Spring处理,并给出了Spring配置文件。同时提出问题,当前Spring将事务管理放在DAO层,想将其移至Service层,以实现整个方法在一个事务里,询问如何修改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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()方法放在一个事务里
应怎样改?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值