• 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
     
     
     
  • 相关阅读:
    1066 Bash 游戏
    1070 Bash 游戏 V4
    codevs 2928 你缺什么
    分块、线段树练习
    Father Christmas flymouse
    codevs 2494 Vani和Cl2捉迷藏
    codevs 2144 砝码称重2
    国王游戏
    codevs 1664 清凉冷水
    2015ACM/ICPC亚洲区沈阳站 Pagodas
  • 原文地址:https://www.cnblogs.com/newcityboy/p/11930593.html
Copyright © 2020-2023  润新知