欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring boot JdbcTemplate使用多数据源配置

程序员文章站 2024-03-17 20:00:22
...

注:使用上篇的Mac Spring boot(1.5.9) 使用JDBCTemplate操作数据库,现在我们使用两个数据库,都有个user表,对user表进行相关数据库操作

1、看下项目目录
Spring boot JdbcTemplate使用多数据源配置

2、我们配置下application.properties,连接的两个数据库new_schema和new_test

spring.datasource.primary.url =jdbc:mysql://localhost:3306/new_schema?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.primary.username = root
spring.datasource.primary.password = 123456
spring.datasource.primary.driverClassName = com.mysql.jdbc.Driver


spring.datasource.secondary.url =jdbc:mysql://localhost:3306/new_test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.secondary.username = root
spring.datasource.secondary.password = 123456
spring.datasource.secondary.driverClassName = com.mysql.jdbc.Driver

# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming.strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

3、定义个配置类DataSourceConfig类

package com.yuna.demo;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
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 org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class DataSourceConfig {

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


    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @Primary
    @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);
    }

}

4、在ApplicationTests测试类中,写几个测试方法

package com.yuna.demo;


import org.junit.Before;
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.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;



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

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

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



    @Before
    public void setUp() {
        jdbcTemplate1.update("delete from user");
        jdbcTemplate2.update("delete from user");
    }



    @Test
    public void test() throws Exception {

        jdbcTemplate1.update("insert into user(name,age) values(?,?)", "yuna", 18);

        jdbcTemplate1.update("insert into user(name,age) values(?,?)", "sam", 23);

        jdbcTemplate2.update("insert into user(name, age) values(?,?)", "jackson", 3);

        System.out.println("count 1 = " + jdbcTemplate1.queryForObject("select count(1) from user", String.class));

        System.out.println("count 2 = " + jdbcTemplate2.queryForObject("select count(1) from user", String.class));

    }

}

5、我们往new_schema数据库的user表插入了2条记录,往new_test数据库中插入1条记录
Spring boot JdbcTemplate使用多数据源配置

6、在数据库中查询相关
(1)、new_test数据库的数据查询

Spring boot JdbcTemplate使用多数据源配置

(2)、new_schema数据库的数据查询

Spring boot JdbcTemplate使用多数据源配置

参考链接这里写链接内容