3
回答
spring mvc4.0 + mybatis mysql 事物配置不回滚
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

applicationContext-web.xml

<context:component-scan base-package="cn.np.*">  
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan> 
    <bean id="dataSource" class = "org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
   <property name="driverClassName" value="${db.driver.class}" />   
   <property name="url" value="${db.url}" /> 
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>
    <!-- 定义Mybatis模板 -->
    <bean id="sqlMapClientTemplate" class="org.mybatis.spring.SqlSessionTemplate">
       <constructor-arg ref="sqlSessionFactory"/>
    </bean> 
    <!-- Spring整合Mybatis:创建 sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="configLocation" value="classpath:/mybatis/SqlMapConfig.xml">
        </property>  
        <property name="mapperLocations" value="classpath:cn/db/*.xml"></property>  
    </bean>
 <!-- jdbc事物 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
<!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务  -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

---------------------------------------------------------------------------------------------------------
springMVC.xml

  
     <context:component-scan base-package="cn.np.*">  
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan> 
 
<mvc:annotation-driven />
<!-- 开启注解 -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> -->
<!--加载页面文件位置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!--加载静态文件 -->
<mvc:resources location="/status/" mapping="/status/**" />
<!--dao层工具类 -->
<bean name="commonDao" class="cn.core.CommonDao" />
<!--工具类 -->
<bean name="util" class="cn.core.Util" />
...

----------------------------------------------------------------------------------------------------

testServiceImpl

@Transactional
@Service(value="testServiceImpl")
public class TestServiceImpl implements TestService{

@Resource(name="commonDao")
private CommonDao commonDao;


@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor=Exception.class) 
public List<Map<String, Object>> select_np_user() throws Exception {
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
Map<String,Object> inParamMap = new HashMap<String,Object>();
inParamMap.put("test1", "test1");
commonDao.insert("insert_test1", inParamMap);
if(1==1){
throw new RuntimeException();
}
Map<String,Object> inParamMap1 = new HashMap<String,Object>();
inParamMap1.put("test2", "test2");
commonDao.insert("insert_test2", inParamMap1);
return list;
}


}


数据库mysql表类型也改了innoDb

可是就是不回滚,求大神指导啊!
在线等。。。急啊。。。。。。

举报
程_小猪
发帖于2年前 3回/1K+阅
共有3个答案 最后回答: 2年前

同事帮忙解决的。

在springMvc里改动后

<context:annotation-config />
 
     <context:component-scan base-package="cn.*">  
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan> 
 

springMVC.xml 里面添加红色部分即可

     <context:component-scan base-package="cn.np.*" use-default-filters="false">  
      <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan> 

参考:http://jinnianshilongnian.iteye.com/blog/1762632

顶部