• spring注解-声明式事务


    实现步骤:

       1.导入依赖包:数据源 c3p0 ,    数据库驱动:mysql-connector-java ,    spring事务包:这时导入spring-jdbc,该包依赖spring-tx, maven会自动导入依赖

    <dependency>
                <groupId>c3p0</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.1.2</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.15</version>
    <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring.version}</version>
            </dependency>

     2.@EnableTransactionManagement开启事务管理器

     3.@Transactional标在方法上(也可以标在类上),表明这时一个事务方法

       这时测试出现错误:NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.transaction.PlatformTransactionManager' available

       解决思路:一看就是找不到bean,那么就配置这个bean 

       配置PlatformTransactionManager: 找到PlatformTransactionManager接口,看它的实现类,选org.springframework.jdbc.datasource包下的

    DataSourceTransactionManager实现类。
    @Bean
        public PlatformTransactionManager transactionManager() throws Exception {
            DataSourceTransactionManager manager=new DataSourceTransactionManager(dataSource());
            return manager;
        }

    到此声明式事务环境搭建好了。。。

  • 相关阅读:
    HDU 5883 欧拉回路
    HDU 5889 Barricade (Dijkstra+Dinic)
    网络流Dinic算法模板 POJ1273
    216. Combination Sum III
    211. Add and Search Word
    973. K Closest Points to Origin
    932. Beautiful Array
    903. Valid Permutations for DI Sequence
    514. Freedom Trail
    312. Burst Balloons
  • 原文地址:https://www.cnblogs.com/dengrong/p/10810718.html
Copyright © 2020-2023  润新知