spring注解版声明式事务

相对于xml版来说,声明式事务的注解版更得我心。

  1. 用jdbc操作数据库举例,在导入了aop和spring依赖以外,导入sping-jdbc依赖,先创建配置类TxConfig,在类中注册了三个bean
    dataSource
    jdbcTemplate
    platformTransactionManager
    还有@EnableTransactionManagement//开启基于注解的事务管理功能  千万不能忘记!

     1 package crm.mytest.tc;
     2 
     3 import com.mchange.v2.c3p0.ComboPooledDataSource;
     4 import org.springframework.context.annotation.Bean;
     5 import org.springframework.context.annotation.ComponentScan;
     6 import org.springframework.context.annotation.Configuration;
     7 import org.springframework.jdbc.core.JdbcTemplate;
     8 import org.springframework.jdbc.datasource.DataSourceTransactionManager;
     9 import org.springframework.transaction.PlatformTransactionManager;
    10 import org.springframework.transaction.annotation.EnableTransactionManagement;
    11 
    12 import javax.sql.DataSource;
    13 import java.beans.PropertyVetoException;
    14 @EnableTransactionManagement//开启基于注解的事务管理功能,重要!!!
    15 @Configuration
    16 @ComponentScan("crm.mytest.tc")
    17 public class TxConfig {
    18     /**
    19      * DataSource
    20      * @return
    21      * @throws PropertyVetoException
    22      */
    23     @Bean
    24     public DataSource dataSource() throws PropertyVetoException {
    25         ComboPooledDataSource dataSource = new ComboPooledDataSource();
    26         dataSource.setUser("root");
    27         dataSource.setPassword("123");
    28         dataSource.setDriverClass("com.mysql.jdbc.Driver");
    29         dataSource.setJdbcUrl("jdbc:mysql:///crm");
    30         return dataSource;
    31     }
    32     @Bean
    33     public JdbcTemplate jdbcTemplate(DataSource dataSource){
    34         JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    35         return jdbcTemplate;
    36     }
    37     //注册事务管理器在容器中,重要!!!
    38     @Bean
    39     public PlatformTransactionManager platformTransactionManager() throws PropertyVetoException {
    40 return new DataSourceTransactionManager(dataSource());//放入数据源
    41     }
    42 }
  2. 简略的创建一个UserDao和UserService,模拟一下开发的mvc模式,这里就先不用接口了
     1 package crm.mytest.tc;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.jdbc.core.JdbcTemplate;
     5 import org.springframework.stereotype.Repository;
     6 
     7 @Repository
     8 public class UserDao {
     9     
    10     @Autowired
    11     private JdbcTemplate jdbcTemplate;
    12 
    13     public void insert(){
    14         String sql="insert into test values(?,?,?)";
    15          jdbcTemplate.update(sql, null, "liweijun",1);
    16     }
    17 }
     1 package crm.mytest.tc;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Service;
     5 import org.springframework.transaction.annotation.Transactional;
     6 
     7 @Service
     8 public class UserService {
     9     @Autowired
    10     private UserDao userDao;
    11 
    12     @Transactional
    13     public void insert(){
    14         userDao.insert();
    15         System.out.println("插入完成");
    16       //  int i=1/0;
    17     }
    18 }
  3. 最后是测试类
     1 package crm.mytest.tc;
     2 
     3 import org.junit.Test;
     4 import org.junit.runner.RunWith;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.test.context.ContextConfiguration;
     7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
     8 
     9 @RunWith(SpringJUnit4ClassRunner.class)
    10 @ContextConfiguration(classes = crm.mytest.tc.TxConfig.class)
    11 public class Demo {
    12     @Autowired
    13     private UserService userService;
    14     @Test
    15     public void demo1(){
    16         userService.insert();
    17     }
    18 }

    总结:这简单吧?可以自行造出异常测试一下

posted @ 2018-06-23 19:48  AnyProgrammer  阅读(1264)  评论(1编辑  收藏  举报