zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    【WPF】 Prism 框架中,TabControl 作为Region时如何设置Header
    【WPF】将控件事件中的参数,传递到ViewModel中
    WPF 一种带有多个子集的类ComBox 解决方法
    WPF TabControl美化
    【WPF】 问题总结-RaidButton修改样式模板后作用区域的变化
    C# 打开Excel文件
    获取文件夹下所有的文件名
    访问需要HTTP Basic Authentication认证的资源的c#的实现 将账号密码放入url
    第十三章 建造者模式(Builder)
    第十二章 外观模式 (Facade)
  • 原文地址:https://www.cnblogs.com/houchen/p/13272466.html
Copyright © 2011-2022 走看看