Spring 注解版-事务实现

/**
 *   声明式事务
 *
 *   环境搭建:
 *   1、导入相关依赖
 *          数据源、数据库驱动,spring-jdbc模块
 *   2、配置数据源,JdbcTemplate 来操作数据库
 *   3、给方法上标注 @Transactional 表示当前方法是一个事务方法
 *   4、@EnableTransactionManagement 开启基于注解的事务管理功能
 *   5、配置事务管理器来控制事务
 */

1)导入相关依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.3.12.RELEASE</version>
</dependency>

<!-- c3p0连接池 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.2</version>
</dependency>

<!--mysql-connector mysql驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.44</version>
</dependency>

2)配置数据源,JdbcTemplate 来操作数据库

①配置类
@ComponentScan("com.atguigu.tx") //扫描加了@component的类
@Configuration  //告诉spring这是一个配置类
public class TxConfig {

    @Bean
    public DataSource dataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource =new ComboPooledDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("houchen");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");  //设置mysql的驱动
        return dataSource;
    }


    @Bean
    public JdbcTemplate jdbcTemplate() throws PropertyVetoException {

        // dataSource() : 并不是执行该方法,而是去ioc容器中查找相应的组件
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
        return  jdbcTemplate;
    }


}

② service层

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public void InsertUser(){
        userDao.InsertUser();
        int i= 1/0;
    }
}

③dao层
@Repository
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void InsertUser(){
        String sql ="INSERT INTO tb_user (username,password) values('houchen','123');";
        jdbcTemplate.update(sql);
    }
}

④ 测试下
public class IocTxTest {


    @Test
    public void test01() {
        ApplicationContext ac = new AnnotationConfigApplicationContext(TxConfig.class);
        UserService userService = ac.getBean(UserService.class);
        userService.InsertUser();
    }

}

结果如下:虽然报错了:但是数据还是插入了

1594225553(1)

clipboard

3、给方法上标注 @Transactional 表示当前方法是一个事务方法

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Transactional
    public void InsertUser(){
        userDao.InsertUser();
        int i= 1/0;
    }
}

5、配置事务管理器来控制事务

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    @Transactional
    public void InsertUser(){
        userDao.InsertUser();
        int i= 1/0;
    }
}
4、@EnableTransactionManagement 开启基于注解的事务管理功能
 5、配置事务管理器来控制事务
@EnableTransactionManagement
@ComponentScan("com.atguigu.tx")
@Configuration  //告诉spring这是一个配置类
public class TxConfig {

    @Bean
    public DataSource dataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource =new ComboPooledDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("houchen");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");  //设置mysql的驱动
        return dataSource;
    }


    @Bean
    public JdbcTemplate jdbcTemplate() throws PropertyVetoException {

        // dataSource() : 并不是执行该方法,而是去ioc容器中查找相应的组件
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
        return  jdbcTemplate;
    }


    // 配置事务管理器来控制事务
    @Bean
    public PlatformTransactionManager transactionManager() throws Exception {
        return  new DataSourceTransactionManager(dataSource());
    }

}

测试结果如下:

1594225798(1)

原文地址:https://www.cnblogs.com/houchen/p/13272466.html