SSM框架下注解配置事务-@Transactional使用

一、开启事务流程

1)在spring整合mybatis的配置文件中追加以下内容(spring.xml)

    <!-- 配置事务 -->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:annotation-driven transaction-manager="dataSourceTransactionManager" />

2)在业务的实现层追加 @Transactional注解

前方高能
1)若对于业务层(service)及持久的层(mapper)的扫描配置在SpringMVC的配置文件中时,一定要将这两个类的扫描配置在spring的配置的配置文件中
2)业务层的方法一定要时public方法

ps:如果这时spring的配置文件报错,一般都是名称空间缺少,追加一下对应的名称空间即可,下面我自己的spring配置文件,仅供参考

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

<!--    <context:component-scan base-package="work.chenc.mapper"/>-->
    <context:component-scan base-package="work.chenc.service"/>

    <!-- 整合MyBatis -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="root"></property>
        <property name="password" value="你的密码"></property>
        <property name="jdbcUrl" value="jdbc:mysql://47.102.143.25/test_dev?useUnicode=true&amp;characterEncoding=UTF-8"></property>
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="initialPoolSize" value="5"></property>
        <property name="maxPoolSize" value="10"></property>
    </bean>

    <!-- 配置MyBatis SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 对应的Sql文件路径 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
        <!-- Mybaties配置文件路劲 -->
        <property name="configLocation" value="classpath:config.xml"></property>
    </bean>

    <!--
        扫描自定义的Mapper接口
        在sql中返resultType parameterType的中直接写类名 相当于 work.chenc.mapper.UserEntity
     -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="work.chenc.mapper"></property>
    </bean>

    <!-- 配置事务 -->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:annotation-driven transaction-manager="dataSourceTransactionManager" />

</beans>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容