两个MySQL做双数据源如何配置_SpringBoot系列--Mysql、Sqlserver 双数据源配置

本文分享如何在SpringBoot项目中简洁高效地配置Mysql和Sqlserver双数据源,包括Druid连接池的使用,以及MyBatis针对不同数据源的配置,详细展示了application.properties配置和相关配置类的代码实现。
摘要由CSDN通过智能技术生成

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

e5a6c17747eebaa0df2f98294f9228fb.png

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](https://my.oschina.net/primary)

[@Bean](https://my.oschina.net/bean)

@ConditionalOnProperty(prefix = "spring.datasource.mysql")

public DataSource mysqlDataSource() {

return DruidDataSourceBuilder.create().build();

}

[@Bean](https://my.oschina.net/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即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值