SpringBoot系列--Mysql、Sqlserver 双数据源配置

在最近的项目开发中,需要用到Mysql和Sqlserverl两种数据库,也就是要进行双数据源的配置。网上看了下,大多比较繁琐,且不够明确。今天分享一个在SpringBoot 中简洁高效配置双数据源的方案。
项目结构如下:

application.properties配置文件
spring.datasource.mysql.username=root
spring.datasource.mysql.password=123456
spring.datasource.mysql.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.mysql.url=jdbc:mysql://localhost:3306/test

spring.datasource.sqlserver.username=root
spring.datasource.sqlserver.password=123456
spring.datasource.sqlserver.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.sqlserver.url=jdbc:sqlserver://localhost:1433;DatabaseName=test
连接池配置
package com.tcwong.demo.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {

	@Primary
	@Bean
	@ConditionalOnProperty(prefix = "spring.datasource.mysql")
	public DataSource mysqlDataSource() {
		return DruidDataSourceBuilder.create().build();
	}

	Bean
	@ConditionalOnProperty(prefix = "spring.datasource.sqlserver")
	public DataSource sqlserverDataSource() {
		return DruidDataSourceBuilder.create().build();
	}

}

或者

package com.tcwong.demo.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig1 {

	@Value("${spring.datasource.mysql.username}")
	private String mysqlUserName;
	@Value("${spring.datasource.mysql.password}")
	private String mysqlPassword;
	@Value("${spring.datasource.mysql.url}")
	private String mysqlUrl;
	@Value("${spring.datasource.mysql.driver-class-name}")
	private String mysqlDriverClass;

	@Value("${spring.datasource.sqlserver.username}")
	private String sqlserverPassword;
	@Value("${spring.datasource.sqlserver.password}")
	private String sqlserverUserName;
	@Value("${spring.datasource.sqlserver.url}")
	private String sqlserverUrl;
	@Value("${spring.datasource.sqlserver.driver-class-name}")
	private String sqlserverDriverClass;

	@Primary
	@Bean
	public DataSource mysqlDataSource() {
		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setUsername(mysqlUserName);
		druidDataSource.setPassword(mysqlPassword);
		druidDataSource.setUrl(mysqlUrl);
		druidDataSource.setDriverClassName(mysqlDriverClass);
		return druidDataSource;
	}

	@Bean
	public DataSource sqlserverDataSource() {
		DruidDataSource druidDataSource = new DruidDataSource();
		druidDataSource.setUsername(sqlserverUserName);
		druidDataSource.setPassword(sqlserverPassword);
		druidDataSource.setUrl(sqlserverUrl);
		druidDataSource.setDriverClassName(sqlserverDriverClass);
		return druidDataSource;
	}

}
MyBatis配置

Mysql配置

package com.tcwong.demo.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.tcwong.demo.dao.mysql"
		,sqlSessionFactoryRef = "mysqlSqlSessionFactory",sqlSessionTemplateRef = "mysqlSqlSessionTemplate")
public class MysqlMapperConfig {

	@Resource
	private DataSource mysqlDataSource;

	@Primary
	@Bean
	SqlSessionFactory mysqlSqlSessionFactory() {
		SqlSessionFactory sqlSessionFactory = null;
		try {
			SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
			sqlSessionFactoryBean.setDataSource(mysqlDataSource);
			sqlSessionFactoryBean.setTypeAliasesPackage("com.tcwong.demo.bean");
			sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
					.getResource("classpath*:mapper/**/*.xml"));
			sqlSessionFactory = sqlSessionFactoryBean.getObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sqlSessionFactory;
	}

	@Primary
	@Bean
	SqlSessionTemplate mysqlSqlSessionTemplate() {
		return new SqlSessionTemplate(mysqlSqlSessionFactory());
	}
}

Sqlserver配置

package com.tcwong.demo.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.tcwong.demo.dao.sqlserver"
		,sqlSessionFactoryRef = "sqlserverSqlSessionFactory", sqlSessionTemplateRef = "sqlserverSqlSessionTemplate")
public class sqlserverMapperConfig {

	@Resource
	private DataSource sqlserverDataSource;

	@Bean
	SqlSessionFactory sqlserverSqlSessionFactory() {
		SqlSessionFactory sqlSessionFactory = null;
		try {
			SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
			sqlSessionFactoryBean.setDataSource(sqlserverDataSource);
			sqlSessionFactoryBean.setTypeAliasesPackage("com.tcwong.demo.bean");
			sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
					.getResource("classpath*:mapper/**/*.xml"));
			sqlSessionFactory = sqlSessionFactoryBean.getObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sqlSessionFactory;
	}

	@Bean
	SqlSessionTemplate sqlserverSqlSessionTemplate() {
		return new SqlSessionTemplate(sqlserverSqlSessionFactory());
	}
}

这里指定了xml的文件路径 和 数据库映射的JavaBean路径。Mysql 和 Sqlserver对应的Mapper 放在对应的Dao即可。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值