0
点赞
收藏
分享

微信扫一扫

Buf 教程 - 使用 Protobuf 生成 Golang 代码和 Typescript 类型定义

J简文 2023-06-05 阅读 117
数据库

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

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

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

那么,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注解,底层当是使用Java的反射,然后进行开启事物。

 

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

 

 

举报

相关推荐

0 条评论