SpringBoot中JdbcTemplate和事务的实现(配置类 不用xml文件)

SpringBoot中JdbcTemplate和事务的实现

  • 首先导入pom依赖
  • 配置类
@Configuration
@ComponentScan(basePackages = "org.ljw")//扫描基准包
@EnableTransactionManagement(proxyTargetClass = true)//开启事务管理 解析注解@Transactional   true开启cglib代理
public class SpringConfig {
   
    //通过druid链接池获取数据源
    @Bean //注解后   交给spring管理的bean
    public DruidDataSource getDruidDataSource(){
   
            //这种方式更通用
            //    Thread.currentThread() 当前线程对象
            //    getContextClassLoader() 是线程对象的方法,可以获取到当期线程的类加载器对象
            //    getResourceAsStream() 直接返回一个流对象
        DruidDataSource dataSource = new DruidDataSource();
        InputStream inputStream = null;
        try {
   
            inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("druid.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            //从properties文件中获取驱动并赋给数据源
            dataSource.setDriverClassName(properties.getProperty("driver"));
            dataSource.setUrl(properties.getProperty("url"));
            dataSource.setUsername(properties.getProperty("user"));
            dataSource.setPassword(properties.getProperty("pwd"));
        } catch (IOException e) {
   
            e
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值