spring的事物

这是我第三次接触事物了,mysql一次,以前的也看过一次。

事物的四大特点:

原子性:多条sql保证同时成功,同时失败。

一致性:在事物的开启和结束,数据库的完整性不被破坏。

隔离性:多线程时,要保证数据的安全,也就是并发安全。

持久性:事物处理后,对数据的修改是永久的,一旦执行事物成功,就刷新到数据库。

其实主要还是第一句:同时成功,同时失败! 

所以,我们开启事物,就是为了利用上面的四个特点,解决问题。

那么,spring是如何实现的,归根结底,我们很多时候,只需要开启事物就行了,底层已经被spring完成了:

使用这种方式的前提是,已经集成了spring-mybatis

第一种方法:

声明式事物(通过xml声明事物,然后利用spring的AOP织入到方法中)

使用DataSourceTransactionManager类进行事物托管。

<?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: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.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
">
    <!--   连接池-->
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/user?UseSSL=false"></property>
        <property name="username" value="root"></property>
        <property name="password" value="qx@123456"></property>
    </bean>
    <!--sqlsessin工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="datasource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="mapperLocations" value="classpath:*.xml"></property>
    </bean>
    <!--spring的模板-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>


    <bean id="userMapper2" class="com.quxiao.mapper.UserMapper2">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>
    <!--    声明事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"></property>
    </bean>
    <!--       配置事物管理,比如全部方法加上事物-->
    <tx:advice id="interceptor" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    <!--    aop织入-->
    <aop:config>
        <aop:pointcut id="point" expression="execution(* com.quxiao.mapper.*.*(..))"/>
        <aop:advisor advice-ref="interceptor" pointcut-ref="point"></aop:advisor>
    </aop:config>

    <!--    开启注解Apo-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    <!--    开启注解事物代理-->
    <tx:annotation-driven></tx:annotation-driven>
    <!--    刚刚的错误是因为没有开启注解事物代理-->
    <!--    aop模式下的注解失败没搞懂,显示获取不到bean-->
</beans>

不知道为啥,重新写了一遍声明式事物,就可以使用了,原来的报错找不到Bean。

至于集成mybatis的代码,在前面写过博客(8条消息) 踩大坑mevan静态资源第二次害死我,以及自己的马虎,又又又害死自己,spring-mybatis_小肖在路上的博客-CSDN博客

打代码还是太马虎了。。。

注解式事物: 通过使用@Transactional注解,底层当是使用AOP环绕,然后进行开启事物。

要开启同时还要在配置文件xml中开启注解事物的支持:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值