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");
	            }
		});
	}

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值