背景:原来一直都是使用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. 前端框架VUE----指令

    一.什么是VUE? 它是构建用户界面的JavaScript框架(让它自动生成js,css,html等) 二.怎么使用VUE? 1.引入vue.js 2.展示HTML <div id=" ...

  2. 如何通过 Vue+Webpack 来做通用的前端组件化架构设计

    目录:   1. 架构选型     2. 架构目录介绍     3. 架构说明     4. 招聘消息 目前如果要说比较流行的前端架构哪家强,屈指可数:reactjs.angularjs.emberj ...

  3. truncate table很慢之enq: RO - fast object reuse和local write wait等待分析

    使用ASSM表空间(默认模式)的时候,在dss系统中确实会出现truncate很慢的现象,但是他不会100%重现,得看概率.通过sql trace(对任何v$sysstat看起来资源消耗很低的情况,都 ...

  4. ORA-12052: cannot fast refresh materialized view

    SQL> execute dbms_mview.refresh ('TX_FAIL_LOG_DAY_MV', 'f'); BEGIN DBMS_MVIEW.REFRESH ('TX_FAIL_L ...

  5. 【题解】Luogu P3740 [HAOI2014]贴海报

    woc,今天已经是day -1了 再写一颗珂朵莉树来++rp吧 否则就要AFO了qaq 这有可能是我最后一篇题解/博客qaq 原题传送门:P3740 [HAOI2014]贴海报 考前刷水题到底是对还是 ...

  6. intel webrtc 部署

    org link conference server Configure the MCU server 1. set the maximum numbers of open files, runnin ...

  7. redhat7.4+shell离线安装docker

    首先要准备离线安装包 1.docker官网下载 2.docker-compose gitbub官网下载 3.我的docker-compose install-docker.sh #!/bin/sh u ...

  8. 图片上传前 压缩,base64图片压缩 Exif.js处理ios拍照倒置等问题

    曾写过在前端把图片按比例压缩不失真上传服务器的前端和后台,可惜没有及时做总结保留代码,只记得js利用了base64位压缩和Exif.js进行图片处理,还有其中让我头疼的ios拍照上传后会倒置等诸多问题 ...

  9. Bootstrap3基础 btn-primary/warning... 三类按钮的六种样式

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  10. Ubuntu18.04 一些好用的扩展

    原文:https://www.lulinux.com/archives/2589 一些好用的扩展: United:https://www.gnome-look.org/p/1167950/ dynam ...