spring注解驱动@Transactional使用

package com.annotation.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

/**
 *  使用基于注解版的事务
 *  1、想要使用基于注解版的事务管理,需要添加 @EnableTransactionManagement 注解开启事务管理功能
 *      但是只添加注解不配置事务管理器会报 NoSuchBeanDefinitionException,所以需要配置事务管理器
 *      org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.transaction.PlatformTransactionManager'
 *  2、在容器中添加事务管理器 PlatformTransactionManager
 */

@EnableTransactionManagement
@PropertySource(value = "classpath:jdbc.properties",encoding = "UTF-8")
@ComponentScan(value = "com.annotation.transaction")
@Configuration
public class MainConfigTransaction {

    @Value("${db.user}")
    private String user;

    @Value("${db.password}")
    private String password;

    @Value("${db.url}")
    private String url;

    @Value("${db.driverClass}")
    private String driverClass;

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    public DataSource dataSource() throws PropertyVetoException {
        System.out.println("URL-->" + url);
        System.out.println("PASSWORD-->" + password);
        System.out.println("DRIVER_CLASS-->" + driverClass);
        System.out.println("USER-->" + user);
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser(user);
        dataSource.setPassword(password);
        dataSource.setJdbcUrl(url);
        dataSource.setDriverClass(driverClass);
        return dataSource;
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return jdbcTemplate;
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource){
        return  new DataSourceTransactionManager(dataSource);
    }

}
package com.annotation.transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Transactional
    public void insertUser(){
        userDao.insertUser();
        System.out.println("插入完成...");
        //添加异常,看事务是否起作用
        int error = 10/0;
    }

}
package com.annotation.transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Random;
import java.util.UUID;

@Repository
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insertUser(){
        Random random = new Random();
        String sql = "INSERT INTO tab_user(username,age) values(?,?)";
        String username = UUID.randomUUID().toString().substring(0,5);
        int age = random.nextInt(100) + 1;
        jdbcTemplate.update(sql,username,age);
    }

}

 

你可能感兴趣的:(spring注解驱动)