Spring Boot之JdbcTemplate多數據源配置與使用

以前在介紹使用JdbcTemplate和Spring-data-jpa時,都使用了單數據源。在單數據源的狀況下,Spring Boot的配置很是簡單,只須要在application.properties文件中配置鏈接參數便可。可是每每隨着業務量發展,咱們一般會進行數據庫拆分或是引入其餘數據庫,從而咱們須要配置多個數據源,下面基於以前的JdbcTemplate和Spring-data-jpa例子分別介紹兩種多數據源的配置方式。java

多數據源配置

建立一個Spring配置類,定義兩個DataSource用來讀取application.properties中的不一樣配置。以下例子中,主數據源配置爲spring.datasource.primary開頭的配置,第二數據源配置爲spring.datasource.secondary開頭的配置。mysql

 

package com.wls.diypro.util.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;
 
import javax.sql.DataSource;
 
 
@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);
    }
}
View Code

JdbcTemplate支持對應的application-dev.yml配置以下:spring

spring:
  datasource:
    primary:
      driver-class-name: com.mysql.jdbc.Driver
  #    url: jdbc:mysql://192.168.159.128:3306/mydb
      url: jdbc:mysql://192.168.11.131:3306/mydb
      username: wls
      password: Wls141215!
    secondary:
      driver-class-name: com.mysql.jdbc.Driver
  #    url: jdbc:mysql://192.168.159.128:3306/mydb
      url: jdbc:mysql://192.168.11.131:3306/shopmall
      username: wls
      password: Wls141215!
View Code

對JdbcTemplate的支持比較簡單,只須要爲其注入對應的datasource便可,以下例子,在建立JdbcTemplate的時候分別注入名爲primaryDataSourcesecondaryDataSource的數據源來區分不一樣的JdbcTemplate。sql

 

@Autowired
@Qualifier("primaryJdbcTemplate")
protected JdbcTemplate primaryJdbcTemplate;
 
@Autowired
@Qualifier("secondaryJdbcTemplate")
protected JdbcTemplate secondaryJdbcTemplate;
package com.wls.diypro.test.jdbcTemplateTest;
 
import com.wls.diypro.model.OrderInfo;
import com.wls.diypro.service.IOrderInfoService;
import junit.framework.Assert;
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.SpringJUnit4ClassRunner;
 
import java.util.Date;
 
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class JdbcTemplateTest {
 
    @Autowired
    @Qualifier("primaryJdbcTemplate")
    protected JdbcTemplate primaryJdbcTemplate;
 
    @Autowired
    @Qualifier("secondaryJdbcTemplate")
    protected JdbcTemplate secondaryJdbcTemplate;
 
    @Autowired
    private IOrderInfoService iOrderInfoService;
 
    @Test
    public void addOrder() throws Exception {
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setAddressDetail("廣平大街");
        orderInfo.setArea("大興區");
        orderInfo.setCity("北京市");
        orderInfo.setOrderNumber("10000001");
        orderInfo.setOrderStatus("2");
        orderInfo.setOrderTime(new Date());
        orderInfo.setProvince("北京");
        orderInfo.setReceiver("王老師");
        orderInfo.setStreet("ces");
        iOrderInfoService.addOrder(orderInfo);
    }
 
 
 
    @Before
    public void setUp() {
        primaryJdbcTemplate.update("DELETE  FROM  order_info ");
        secondaryJdbcTemplate.update("DELETE  FROM  order_info ");
    }
 
    @Test
    public void test() throws Exception {
 
        // 往第一個數據源中插入兩條數據
        primaryJdbcTemplate.update("insert into order_info(order_flag,order_number,order_status,street) values(?, ?, ?, ?)", "test", "10001", "S01","廣平大街");
        primaryJdbcTemplate.update("insert into order_info(order_flag,order_number,order_status,street) values(?, ?, ?, ?)", "test", "10001", "S01","廣平大街");
 
        // 往第二個數據源中插入一條數據,若插入的是第一個數據源,則會主鍵衝突報錯
        secondaryJdbcTemplate.update("insert into order_info(order_flag,order_number,order_status,street) values(?, ?, ?, ?)", "test", "10003", "S02","廣平大街");
 
        // 查一下第一個數據源中是否有兩條數據,驗證插入是否成功
        Assert.assertEquals("2", primaryJdbcTemplate.queryForObject("select count(1) from order_info", String.class));
 
        // 查一下第一個數據源中是否有兩條數據,驗證插入是否成功
        Assert.assertEquals("1", secondaryJdbcTemplate.queryForObject("select count(1) from order_info", String.class));
 
    }
}

  

相關文章
相關標籤/搜索