SpringBoot 配置多数据源

有时候一个工程需要多个数据源,SpringBoot配置多数据源如下:

1、配置文件application.properties:

spring.thymeleaf.cache=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html

#DB2配置
spring.datasource.primary.driver-class-name=com.ibm.db2.jcc.DB2Driver
spring.datasource.primary.jdbc-url=jdbc:db2://127.0.0.1:50000/CENDATA
spring.datasource.primary.username=db2
spring.datasource.primary.password=db2

#oracle配置
spring.jpa.database=oracle
spring.datasource.secondary.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.secondary.jdbc-url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
spring.datasource.secondary.username=orcl
spring.datasource.secondary.password=orcl

2、创建Spring配置类,创建两个datasource来读取application.properties的配置:

package com.example.invoice.config;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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();
    }
对JDBCTemplate支持 注入对应的datasource
@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);
    }
}

3、Repository层使用数据源:

package com.example.invoice.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.example.invoice.dao.QuantityDao;

@Repository
public class QuantityDaoImpl implements QuantityDao {

	@Autowired
	@Qualifier("primaryJdbcTemplate")
        private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<Map<String,Object>> getByType(String sql) {
		return this.jdbcTemplate.queryForList(sql);
	}

	@Override
	@Qualifier("primaryJdbcTemplate")
	public int getCountBySql(String sql) {
		return this.jdbcTemplate.queryForObject(sql,new RowMapper<Integer>() {
	            @Override
	            public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
	                return rs.getInt("count");
	            }
		});
	}

}

package com.example.invoice.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.example.invoice.dao.LogViewDao;

@Repository
public class LogViewDaoImpl implements LogViewDao {

	@Autowired
	@Qualifier("secondaryJdbcTemplate")
        private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<Map<String,Object>> getLogView(String sql) {
		return this.jdbcTemplate.queryForList(sql);
	}

	@Override
	@Qualifier("secondaryJdbcTemplate")
	public int getCountBySql(String sql) {
		return this.jdbcTemplate.queryForObject(sql,new RowMapper<Integer>() {
	            @Override
	            public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
	                return rs.getInt("count");
	            }
		});
	}

}


猜你喜欢

转载自blog.csdn.net/luuvyjune/article/details/80986226