zoukankan      html  css  js  c++  java
  • springboot通过JdbcTemplate实现多数据库

    pom.xml

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <scope>runtime</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>

    application.properties:

    spring.datasource.primary.jdbc-url=jdbc:sqlserver://localhost:1433;DatabaseName=spiderdb
    spring.datasource.primary.username=root
    spring.datasource.primary.password=root
    spring.datasource.primary.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

    spring.datasource.secondary.jdbc-url=jdbc:sqlserver://localhost:1434;DatabaseName=spiderdb
    spring.datasource.secondary.username=root
    spring.datasource.secondary.password=root
    spring.datasource.secondary.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver

    #注意使用url,可能出现"java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName."错误.

    TwoDataSourceConfig:

    @Configuration
    public class TwoDataSourceConfig {
    @Bean(name = "primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.secondary")
    public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
    }

    @Bean(name = "primaryJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource" ) DataSource dataSource) {
    return new JdbcTemplate(dataSource);
    }

    @Bean(name = "secondaryJdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource" ) DataSource dataSource) {
    return new JdbcTemplate(dataSource);
    }
    }

    测试验证:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class JdbcTest {

    @Autowired
    @Qualifier("primaryJdbcTemplate")
    protected JdbcTemplate pjdbcTemplate;

    @Autowired
    @Qualifier("secondaryJdbcTemplate")
    protected JdbcTemplate sjdbcTemplate;

    @Test
    public void test() throws Exception {
    String a = pjdbcTemplate.queryForObject("select count(*) from xx where tag = '3' ", String.class);
    String b = sjdbcTemplate.queryForObject("select count(*) from xx where tag = '3' ", String.class);
    Assert.assertEquals(a, b);
    }
    }
  • 相关阅读:
    springboot2.0集成RestTemplate
    RestTemplate HttpMessageConverter报错的解决方案no suitable HttpMessageConverter
    vue条件渲染+列表渲染+事件处理器
    vue计算属性+vue中class与style绑定
    vue选项数据+vue模板语法
    vue安装配置+生命周期
    sass案例--实现高仿今日头条H5版新闻列表页
    sass内置函数
    sass继承+嵌套+条件控制
    sass常见的基本数据类型+加减乘除基本运算
  • 原文地址:https://www.cnblogs.com/ylpb/p/9208745.html
Copyright © 2011-2022 走看看