spring boot实现连接多数据源

方法1

1.先配置各数据库的连接信息

在application.properties配置文件下,配置好driver、url、username、password等,配置名自定义,后面配置类获取对应的就好!

#数据库1
first.jdbc.dbType=mssql
first.jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
first.jdbc.url=jdbc:sqlserver://111.11.1.11:1433;databaseName=aaa;user=aa;password=aa
first.jdbc.username=aa
first.jdbc.password=aa

#数据库2
second.jdbc.dbType=mssql
second.jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
second.jdbc.url=jdbc:sqlserver://111.11.1.11:1433;databaseName=bbb;user=bb;password=bb
second.jdbc.username=bb
second.jdbc.password=bb

2.创建spring配置类,创建事务管理器PlatformTransactionManager

@Configuration
public class DataSourceConfiguration {
	@Bean(name = "first.dataSource")
	public DataSource createDataSource1(
			@Value("${first.jdbc.driver}") String driver,
			@Value("${first.jdbc.url}") String url, 
			@Value("${first.jdbc.username:}") String username,
			@Value("${first.jdbc.password:}") String password, 
			@Value("${ds.maxActive:5}") Integer maxActive,
			@Value("${ds.maxIdle:2}") Integer maxIdle, 
			@Value("${cqweight.jdbc.validationQuery:}") String validationQuery) {
		PooledDataSource dataSource = new PooledDataSource(driver, url, username, password);
		dataSource.setPoolMaximumActiveConnections(maxActive);
		dataSource.setPoolMaximumIdleConnections(maxIdle);
		if (StringUtils.isEmpty(validationQuery)) {
			if (url.contains("oracle")) {
				validationQuery = "select 1 from dual";
			} else {
				validationQuery = "select 1";
			}
		}
		dataSource.setPoolPingConnectionsNotUsedFor(5 * 60 * 1000);
		dataSource.setPoolPingEnabled(true);
		dataSource.setPoolPingQuery(validationQuery);
		return dataSource;
	}
	@Bean(name = "second.dataSource")
	public DataSource createDataSource2(
			@Value("${second.jdbc.driver}") String driver,
			@Value("${second.jdbc.url}") String url, 
			@Value("${second.jdbc.username:}") String username,
			@Value("${second.jdbc.password:}") String password, 
			@Value("${ds.maxActive:5}") Integer maxActive,
			@Value("${ds.maxIdle:2}") Integer maxIdle, 
			@Value("${cqweight.jdbc.validationQuery:}") String validationQuery) {
		PooledDataSource dataSource = new PooledDataSource(driver, url, username, password);
		dataSource.setPoolMaximumActiveConnections(maxActive);
		dataSource.setPoolMaximumIdleConnections(maxIdle);
		if (StringUtils.isEmpty(validationQuery)) {
			if (url.contains("oracle")) {
				validationQuery = "select 1 from dual";
			} else {
				validationQuery = "select 1";
			}
		}
		dataSource.setPoolPingConnectionsNotUsedFor(5 * 60 * 1000);
		dataSource.setPoolPingEnabled(true);
		dataSource.setPoolPingQuery(validationQuery);
		return dataSource;
	}

	@Bean(name = "first.sqlSessionFactory")
	public SqlSessionFactoryBean createSqlSessionFactory1(
			@Qualifier("first.dataSource") DataSource dataSource
			) {
		SqlSessionFactoryBean bf = new SqlSessionFactoryBean();
		bf.setDataSource(dataSource);
		return bf;
	}
	@Bean(name = "second.sqlSessionFactory")
	public SqlSessionFactoryBean createSqlSessionFactory2(
			@Qualifier("second.dataSource") DataSource dataSource
			) {
		SqlSessionFactoryBean bf = new SqlSessionFactoryBean();
		bf.setDataSource(dataSource);
		return bf;
	}

	@Bean(name = "first.transactionManager")
	public PlatformTransactionManager createTransactionManager1(
			@Qualifier("first.sqlSessionFactory") SqlSessionFactory sessionFactory) {
		MybatisTransactionManager txManager = new MybatisTransactionManager(sessionFactory);
		return txManager;
	}
	@Bean(name = "second.transactionManager")
	public PlatformTransactionManager createTransactionManager2(
			@Qualifier("second.sqlSessionFactory") SqlSessionFactory sessionFactory) {
		MybatisTransactionManager txManager = new MybatisTransactionManager(sessionFactory);
		return txManager;
	}
}

3.在service层是有@Transactional注解,配置好事务管理器就可以使用

@Transactional(transactionManager = "first.transactionManager")
public interface Service1 {
	//访问数据库1
}

@Transactional(transactionManager = "first.transactionManager")
public interface Service2 {
	//访问数据库2
}

方法2 JdbcTemplate

1.跟方法1 一样配置好数据库信息

spring.datasource.first.url=jdbc:mysql://localhost:3306/aa
spring.datasource.first.username=root
spring.datasource.first.password=root
spring.datasource.first.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.second.url=jdbc:mysql://localhost:3306/bb
spring.datasource.second.username=root
spring.datasource.second.password=root
spring.datasource.second.driver-class-name=com.mysql.jdbc.Driver

2 创建DataSource配置类


@Configuration
public class DataSourceConfig {
    @Bean(name = "firstDataSource")
    @Qualifier("firstDataSource")
    @ConfigurationProperties(prefix="spring.datasource.first")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Bean(name = "secondDataSource")
    @Qualifier("secondDataSource")
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.second")
    public DataSource secondaryDataSource() {
        return DataSourceBuilder.create().build();
    }
    
	@Bean(name = "firstJdbcTemplate")
	public JdbcTemplate primaryJdbcTemplate(
	        @Qualifier("firstDataSource") DataSource dataSource) {
	    return new JdbcTemplate(dataSource);
	}
	@Bean(name = "secondJdbcTemplate")
	public JdbcTemplate secondaryJdbcTemplate(
	        @Qualifier("secondDataSource") DataSource dataSource) {
	    return new JdbcTemplate(dataSource);
	}
}

3.使用

@service
public class Service {
	@Autowired
	@Qualifier("firstJdbcTemplate")
	private JdbcTemplate jdbcTemplate1;
	@Autowired
	@Qualifier("secondJdbcTemplate")
	private JdbcTemplate jdbcTemplate2;
	
	public void test() {
		jdbcTemplate1.update(sql);
		jdbcTemplate2.update(sql);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值