请教!springmvc+mybatis 为什么我的事务无法回滚?

配置文件如下:
spring-bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
        
    <!-- 导入配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>

<!-- 配置注解扫描 -->
<context:component-scan base-package="com.yc.springblog">
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RequestMapping" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ResponseBody" />
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController" />
</context:component-scan>

<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>

<!-- 让mybatis从指定的数据源中获得连接 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>

<!-- mybatis映射文件路径配置 -->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property>

<!-- 别名配置 -->
<property name="typeAliasesPackage" value="com.yc.springblog.entity"></property>
</bean>

<!-- 配置mybatis映射接口的代理实现,bean中id属性值必须跟映射接口中的方法名一致 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置mapper映射文件对应的接口和包路径 -->
<property name="basePackage" value="com.yc.springblog.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
    
    <!-- 给数据源配置事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 启动配置好的事务管理 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
<!-- 文件上传的配置 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="1048576000"></property>
<property name="maxInMemorySize" value="4096"></property>
</bean> 
</beans>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        
    <!-- 扫描IoC注解 -->
<context:component-scan base-package="com.yc.springblog" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RequestMapping"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ResponseBody"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController"/>
</context:component-scan>
    
    <!-- 放过静态资源 -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>

<!-- 启动mvc注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
        
</beans>

项目结构:

需要开启事务的类或方法上加@Transaction注解了吗

数据库事务管理需要在类上或者方法上加上@Transaction,并需要手动抛出异常。不然不会生效

事务回滚注解要加到主干方法上,同一个资源代下载网类中,A方法调用B方法,那么注解要加在A方法上面,否则注解不会生效的,如果对异常进行try catch捕获了,一定要抛出异常,不然注解接受不到,就不会回滚

必须加上

可能原因
1.方法必须为public
2.事务方法和调用者在同一个类中

我相信已楼主的聪明才智,是不会忘记加上@Transaction这个注解的,看看Transaction导入的包是不是javax.transaction?
如果是请换为:org.springframework.transaction.annotation

谬赞谬赞,已经知道了,我把事务管理放在了controller层,应该放在service层!

发布了122 篇原创文章 · 获赞 2 · 访问量 5217

猜你喜欢

转载自blog.csdn.net/liuji0517/article/details/105113860