zoukankan      html  css  js  c++  java
  • 在SpringBoot项目新增一个jdbcTemplate数据源

    //配置application-dev.yml
    spring: datasource1: url: jdbc:sqlserver:
    //127.0.0.1:1433;DatabaseName=Test username: sa password: 123456 driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver


    package
    com.example.test.config; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties("spring.datasource1") DataSource ds1(){ return DataSourceBuilder.create().build(); } }
    package com.example.test.config;
    
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import javax.sql.DataSource;
    
    @Configuration
    public class JdbcTemplateConfig {
        @Bean
        JdbcTemplate jdbcTemplate1(@Qualifier("ds1")DataSource dataSource){
            return new JdbcTemplate(dataSource);
        }
    }

    controller方法调用:

       @Resource(name = "jdbcTemplate1")
        JdbcTemplate jdbcTemplate1;

    等同于:

        @Autowired
        @Qualifier(name = "jdbcTemplate1")
        JdbcTemplate jdbcTemplate1;
  • 相关阅读:
    字符串----不可重叠的最长重复子串
    字符串----最长重复子串
    字符串----HDU-1358
    字符串----hiho字符串(尺取法)
    字符串匹配(二)----KMP算法
    字符串匹配(一)----Rabin-Karp算法
    字符串----最短摘要生成(尺取法)
    【Hibernate 检索策略】
    【Hibernate 多表查询】
    【Hibernate QBC】
  • 原文地址:https://www.cnblogs.com/haciont/p/11603569.html
Copyright © 2011-2022 走看看