背景:原来一直都是使用mysql数据库,在application.properties 中配置数据库信息

spring.datasource.url=jdbc:mysql://xxxx/test

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.username=xxx

spring.datasource.password=xxx

这样spring boot会自动化配置,我们用spring boot 约定的配置,现在由于业务的需要,另加一个数据源sqlServer。下面是具体步骤以及遇到的一系列问题。

一、导入依赖
 pom.xml文件:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.4.0.jre8</version>
<scope>runtime</scope>
</dependency>
二.在application.properties中配置
 spring.datasource.second.jdbc-url=jdbc:sqlserver://xxxxx:1433;DatabaseName=test
spring.datasource.second.username=xxx
spring.datasource.second.password=xxxx
spring.datasource.second.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver 注意1:连接数据库的方式不一样,mysql是/test ,sqlServer是;DatabaseName=test
spring.datasource.url=jdbc:mysql://xxxx/test
spring.datasource.second.jdbc-url=jdbc:sqlserver://xxxxx:1433;DatabaseName=test
三.配置sqlServer数据源
 package com.ieou.qmt.common;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource; @Configuration
public class SqlServerDataSourceConfig { @Bean(name = "sqlServerDataSource")
@Qualifier("sqlServerDataSource")
@ConfigurationProperties(prefix="spring.datasource.second")
public DataSource getMyDataSource(){
return DataSourceBuilder.create().build();
} @Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("sqlServerDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}

总结:

配置到这里就可以使用JdbcTemplate来操作sqlServer了,(mysql是spring boot的自动化配置,sqlServer是我们手动配置的)只要在类中注入即可
例如:(JdbcTemplate 的用法自行百度)
public class IEOUMallServiceImpl implements IEOUMallService{
@Autowired
@Qualifier("secondaryJdbcTemplate")
protected JdbcTemplate jdbcTemplate2;
}

四.以上配置完成后在执行带有@Transactional 事务的接口时会发现报错:
 "org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 

 'org.springframework.transaction.PlatformTransactionManager' available\n\tat 

 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:347)\n\tat 

 org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)\n\tat 

 org.springframework.transaction.interceptor.TransactionAspectSupport.determineTransactionManage
所以:我们必须手动分别配置mysql与sqlServer
1.配置文件:application.properties 需要改为如下:
 spring.datasource.first.jdbc-url=jdbc:mysql://xxxx:3306/test
(这里要是jdbc-url,不然会报jdbcUrl is required with driverClassName的错误)
spring.datasource.first.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.first.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.first.username=xxx
spring.datasource.first.password=xxx spring.datasource.second.jdbc-url=jdbc:sqlserver://xxxx:1433;DatabaseName=test
spring.datasource.second.username=xxx
spring.datasource.second.password=xxx
spring.datasource.second.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
2.原来的SqlServerDataSourceConfig改为如下:
 package com.ieou.qmt.common;

 import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource; @Configuration
public class SqlServerDataSourceConfig { private static final String MAPPER_PATH = "classpath:mybatis/mapping/mapper/*.xml"; private static final String ENTITY_PACKAGE = "com.ieou.qmt.mapper"; @Bean(name = "sqlServerDataSource")
@Qualifier("sqlServerDataSource")
@ConfigurationProperties(prefix="spring.datasource.second")
public DataSource getMyDataSource(){
return DataSourceBuilder.create().build();
} @Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("sqlServerDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
} @Bean(name = "second.SqlSessionTemplate")
public SqlSessionFactory devSqlSessionFactory(
@Qualifier("sqlServerDataSource") DataSource ddataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(ddataSource);
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactory.setMapperLocations(resolver.getResources(MAPPER_PATH));
sessionFactory.setTypeAliasesPackage(ENTITY_PACKAGE);
return sessionFactory.getObject();
} @Bean
public PlatformTransactionManager sqlServerTransactionManager(@Qualifier("sqlServerDataSource") DataSource sqlServerDataSource)
{
return new DataSourceTransactionManager(sqlServerDataSource);
} }
3.新建mysql配置文件如下:
 package com.ieou.qmt.common;

 import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource; @Configuration
public class MysqlDataSourceConfig { private static final String MAPPER_PATH = "classpath:mybatis/mapping/*.xml"; private static final String ENTITY_PACKAGE = "com.ieou.qmt.mapper"; @Bean(name = "dataSource")
@Primary
@Qualifier("dataSource")
@ConfigurationProperties(prefix="spring.datasource.first")
public DataSource getMyDataSource(){
return DataSourceBuilder.create().build();
} @Bean(name = "first.SqlSessionTemplate")
@Primary
public SqlSessionFactory devSqlSessionFactory(
@Qualifier("dataSource") DataSource ddataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(ddataSource);
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactory.setMapperLocations(resolver.getResources(MAPPER_PATH));
sessionFactory.setTypeAliasesPackage(ENTITY_PACKAGE);
return sessionFactory.getObject();
} @Bean
@Primary
public PlatformTransactionManager mysqlTransactionManager(@Qualifier("dataSource") DataSource mysqlDataSource)
{
return new DataSourceTransactionManager(mysqlDataSource);
}
}
4.从事务管理器中选择一个事务,在方法上加@Transactional(value = "mysqlTransactionManager",rollbackFor = Exception.class)
 
至此:配置完毕,如有不足或不对的地方,请补充。

spring boot 配置双数据源mysql、sqlServer的更多相关文章

  1. Spring Boot配置多数据源并实现Druid自动切换

    原文:https://blog.csdn.net/acquaintanceship/article/details/75350653 Spring Boot配置多数据源配置yml文件主数据源配置从数据 ...

  2. 如何通过Spring Boot配置动态数据源访问多个数据库

    之前写过一篇博客<Spring+Mybatis+Mysql搭建分布式数据库访问框架>描述如何通过Spring+Mybatis配置动态数据源访问多个数据库.但是之前的方案有一些限制(原博客中 ...

  3. spring项目配置双数据源读写分离

    我们最早做新项目的时候一直想做数据库的读写分离与主从同步,由于一些原因一直没有去做这个事情,这次我们需要配置双数据源的起因是因为我们做了一个新项目用了另一个数据库,需要把这个数据库的数据显示到原来的后 ...

  4. spring boot 2.0 配置双数据源 MySQL 和 SqlServer

    参考:https://www.cnblogs.com/xiaofengfeng/p/9552816.html 安装 org.mybatis.spring.boot:mybatis-spring-boo ...

  5. springboot配置双数据源 MySQL和SqlServer

    1. pom文件的驱动jar包加上去, compile 'com.microsoft.sqlserver:mssql-jdbc:6.2.2.jre8' 2. application.yml sprin ...

  6. spring+mybatis 配置双数据源

    配置好后,发现网上已经做好的了, 不过,跟我的稍有不同, 我这里再拿出来现个丑: properties 文件自不必说,关键是这里的xml: <?xml version="1.0&quo ...

  7. spring boot 配置多数据源

    https://www.jianshu.com/p/b2e53a2521fc

  8. Spring Boot2.4双数据源的配置

    相较于单数据源,双数据源配置有时候在数据分库的时候可能更加有利 但是在参考诸多博客以及书籍(汪云飞的实战书)的时候,发现对于spring boot1.X是完全没问题的,一旦切换到spring boot ...

  9. Spring boot配置多个Redis数据源操作实例

    原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...

随机推荐

  1. RHEL7 CentOS7 的 firewall命令简单介绍

    firewall 服务介绍 firewall 服务是 redhat7 和 centos7 系统默认安装好的防火墙服务,一个信任级别的概念来管理与之相关联的连接与接口.它支持 ipv4 与 ipv6,并 ...

  2. eval & sleep

    ltp-ddt can_loopback source 'functions.sh'; interface='can0'; bitrate=; do_cmd "do_can_loopback ...

  3. OpenGL: 实现立体显示

    https://blog.csdn.net/augusdi/article/details/19922295 立体显示原理:设没有立体显示的模型视图矩阵ModelView为Mv,投影矩阵为Mp,则.物 ...

  4. 今日总结(linux和plsql)

    #case ...when语句(根据字段不同值显示不同结果) ##1)case ...when语句的使用方法一: 语法格式: case column_name when value1 then res ...

  5. Baidu WebFE(FEX)团队开发 的 文件上传插件 WebUploader

    1.webUploader官网下载地址:http://fex.baidu.com/webuploader/ 直接下载代码,运行examples目录文件即可 2.webUploader上传demo:ht ...

  6. Centos7部署tornado项目

    今天帮一个学生解决tornado的部署问题,在此记录了这其中的过程,其中的tornado项目更换为demo示例. 开发环境: 本地开发环境:Win10 + Python3.5.4 + PyCharm ...

  7. API网关性能比较:NGINX vs. ZUUL vs. Spring Cloud Gateway vs. Linkerd(转)

    前几天拜读了 OpsGenie 公司(一家致力于 Dev & Ops 的公司)的资深工程师 Turgay Çelik 博士写的一篇文章(链接在文末),文中介绍了他们最初也是采用 Nginx 作 ...

  8. oracle 11g亿级复杂SQL优化一例(数量级性能提升)

    自从16年之后,因为工作原因,项目中就没有再使用oracle了,最近最近支持一个项目,又要开始负责这块事情了.最近在跑性能测试,配置全部调好之后,不少sql还存在性能低下的问题,主要涉及执行计划的不合 ...

  9. Net中应用 Redis 扩展类

    GIt地址:https://gitee.com/loogn/stackexchange-redis-typedextensions 1.stackexchange 类调用 using System; ...

  10. 【题解】Luogu P3871 [TJOI2010]中位数

    平衡树板题 原题传送门 这道题要用Splay,我博客里有对Splay的详细介绍 每次加入一个数,把数插入平衡树中 并且要记录一共有多少个数 每次查询就查询平衡树中第(总数-1)/2+1个数 十分暴力 ...