zoukankan      html  css  js  c++  java
  • spring下春注解的声明式事务控制

    package com.hope.test;

    import com.hope.domain.Account;
    import com.hope.service.IAccountService;
    import config.SpringConfiguration;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.ComponentScans;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

    import java.util.List;

    /**
    * @author newcityman
    * @date 2019/11/20 - 17:05
    */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = SpringConfiguration.class)
    public class AccountTest {
    @Autowired
    private IAccountService accountService;

    @Test
    public void testTranfer(){
    accountService.transfer("aaa","bbb",100f);
    }
    }

    package config;

    import org.springframework.context.annotation.*;
    import org.springframework.transaction.annotation.EnableTransactionManagement;

    /**
    * @author newcityman
    * @date 2019/11/25 - 20:22
    * spring的配置类,相当于bean.xml
    */
    @Configuration
    @ComponentScan("com.hope")
    @Import({JdbcConfig.class,TransactionConfig.class})
    @PropertySource("jdbcConfig.properties")
    @EnableTransactionManagement
    public class SpringConfiguration {
    }

    package config;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;

    import javax.sql.DataSource;

    /**
    * @author newcityman
    * @date 2019/11/25 - 20:24
    * 与连接数据库相关的配置类
    */

    public class JdbcConfig {

    @Value("${jdbc.driver}")
    private String driver;

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

    @Value("${jdbc.username}")
    private String username;

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

    /**
    * 创建jdbcTemplate对象
    *
    * @param dataSource
    * @return
    */
    @Bean(name = "jdbcTemplate")
    public JdbcTemplate createJdbcTemplate(DataSource dataSource) {
    return new JdbcTemplate(dataSource);
    }

    /**
    * 创建数据源
    *
    * @return
    */
    @Bean(name = "dataSource")
    public DataSource createDataSource() {
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName(driver);
    ds.setUrl(url);
    ds.setUsername(username);
    ds.setPassword(password);
    return ds;
    }
    }


    package config;

    import org.springframework.context.annotation.Bean;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.transaction.PlatformTransactionManager;

    import javax.sql.DataSource;

    /**
    * @author newcityman
    * @date 2019/11/25 - 20:48
    */
    public class TransactionConfig {
    /**
    * 用于创建事务管理器对象
    * @param dataSource
    * @return
    */
    @Bean(name="transactionManager")
    public PlatformTransactionManager createTransactionManager(DataSource dataSource){
    return new DataSourceTransactionManager(dataSource);
    }
    }

    //jdbcConfig.properties文件
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/easy
    jdbc.username=root
    jdbc.password=123
     
     
     
  • 相关阅读:
    DGbroker三种保护模式的切换
    oracle11G使用DGbroker创建dg
    oracle数据泵示例
    oracle DB_LINK
    oracle数据库rman备份计划及恢复
    oracle查看对象信息
    mybatis学习:mybatis的注解开发和编写dao实现类的方式入门
    mybatis学习:mybatis的环境搭建与入门
    mybatis框架学习:
    入门servlet:request请求转发和共享数据
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11930593.html
Copyright © 2011-2022 走看看