1. pom文件的驱动jar包加上去,

compile 'com.microsoft.sqlserver:mssql-jdbc:6.2.2.jre8'

2. application.yml

spring:
datasource:
master:
jdbc-url: jdbc:mysql://10.12.49.55:3306/smartcity-01
username: root
password: root
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
other:
jdbc-url: jdbc:sqlserver://10.12.49.35:1433;DatabaseName=LandscapingDB
username: sa
password: Sql123
#使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver

注意不要使用url,要使用jdbc-url

主数据库配置:

package com.pactera.scm.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.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.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource; @Configuration
@MapperScan(basePackages ="com.pactera.scm.mapper", sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MybatisDbMasterConfig { @Primary
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
} @Primary
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setTypeAliasesPackage("com.pactera.scm.entity");
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
return factoryBean.getObject();
} @Primary
@Bean(name = "masterTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean(name = "masterSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

  第二数据库配置:

package com.pactera.scm.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.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.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource; @Configuration
@MapperScan(basePackages = "com.pactera.scm.otherDB", sqlSessionFactoryRef = "otherSqlSessionFactory")
public class MybatisDbOtherConfig { @Bean(name = "otherDataSource")
@ConfigurationProperties(prefix = "spring.datasource.other")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
} @Bean(name = "otherTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("otherDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource); } @Bean(name = "otherSqlSessionFactory")
public SqlSessionFactory basicSqlSessionFactory(@Qualifier("otherDataSource") DataSource basicDataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(basicDataSource);
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:other/*.xml"));
return factoryBean.getObject();
} @Bean(name = "otherSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("otherSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

  

启动类正常些就行了,不需要改动,保证能扫描到配置文件就行

@SpringBootApplication
@EnableConfigurationProperties
@EnableScheduling
public class ScmApplication { public static void main(String[] args) {
SpringApplication.run(ScmApplication.class, args);
}
}
使用:
@Resource
private AccountMapper accountMapper;
直接调用就行

  

springboot配置双数据源 MySQL和SqlServer的更多相关文章

  1. spring boot 配置双数据源mysql、sqlServer

    背景:原来一直都是使用mysql数据库,在application.properties 中配置数据库信息 spring.datasource.url=jdbc:mysql://xxxx/test sp ...

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

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

  3. spring-boot支持双数据源mysql+mongo

    这里,首先想说的是,现在的web应用,处理的数据对象,有结构化的,也有非结构化的.同时存在.但是在spring-boot操作数据库的时候,若是在properties文件中配置数据源的信息,通过默认配置 ...

  4. SpringBoot配置多数据源Mysql+Sqlite

    ​ 配置了一下druid的多数据源配置,尝试了很多方法,Spring boot关于对Mysql和Sqlite多数据源的配置,记录下来: 涉及技术点: Springboot + Druid + Mysq ...

  5. SpringBoot Jpa 双数据源mysql + oracle + liquibase+参考源码

    一.yml文件配置 spring: # 数据库配置 datasource: primary: jdbc-url: jdbc:mysql://localhost:3306/mes-dev?useUnic ...

  6. springboot配置Druid数据源

    springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...

  7. SpringBoot配置多数据源时遇到的问题

    SpringBoot配置多数据源 参考代码:Spring Boot 1.5.8.RELEASE同时配置Oracle和MySQL 原作者用的是1.5.8版本的SpringBoot,在升级到2.0.*之后 ...

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

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

  9. springboot+mybatis集成多数据源MySQL/Oracle/SqlServer

    日常开发中可能时常会遇到一些这样的需求,业务数据库和第三方数据库,两个或多个数据库属于不同数据库厂商,这时候就需要通过配置来实现对数据库实现多源处理.大致说一下我的业务场景,框架本身是配置的sprin ...

随机推荐

  1. ArcGIS 10开发迁移策略(待续)

    1.更改 ESRI.ArcGIS.ADF 程序集 ArcGIS 10 中, ADF 程序集中的功能被分散到不同的程序集中,如果将 ArcGIS 9.3 下 开发的自定义组件迁移到 ArcGIS 10 ...

  2. Codeforces 633F The Chocolate Spree 树形dp

    The Chocolate Spree 对拍拍了半天才知道哪里写错了.. dp[ i ][ j ][ k ]表示在 i 这棵子树中有 j 条链, 是否有链延伸上来. #include<bits/ ...

  3. Codeforces 1136E Nastya Hasn't Written a Legend 线段树

    vp的时候没码出来.. 我们用set去维护, 每一块区域, 每块区域内的元素与下一个元素的差值刚好为ki,每次加值的时候我们暴力合并, 可以发现我们最多合并O(n)次. 然后写个线段树就没了. #in ...

  4. ibatis 多种传参方式

    1,在公司项目yuda遇到的传入in语句,如果直接拼接in语句:in (....),sqlmap中使用#...#输出是不行的. 为需要使用: 第三种:in后面的数据确定,使用string传入      ...

  5. sql查询一个字段不同值并返回

    sql SELECT COUNT(字段),分组字段,SUM(字段),SUM(字段) FROM 表 GROUP BY 分组字段 java EntityWrapper<ProjectEntity&g ...

  6. java分页实现

    虽然现在有很多好用的框架,对分页进行支持,很简单的就把分页的效果做出来,但是如果自己手写是一个怎样的流程的?今天就来说说它,手动实现分页效果. 一.分页的思路 首先我们得知道写分页代码时的思路,保持思 ...

  7. 【python】廖雪峰python教程学习--基础

     No1: 目前,Python有两个版本,一个是2.x版,一个是3.x版,这两个版本是不兼容的 No2: 用r''表示''内部的字符串默认不转义 No3: 以'''开头,敲回车可以换行 No4: 布尔 ...

  8. gradle修改apk包名和apk文件名

    需求1:根据渠道不同给包名添加不同的后缀名 方案: //先定义默认包名,用来复用 def packageName = "xxx.xxxx.xxxx" defaultConfig { ...

  9. ftp服务器搭建流程详解

    随着项目的运行,项目的体积越来越庞大,于是将文件(这里包括所有上传的附件信息等)放在项目中已经显得很臃肿,项目运行的速度会越来越慢,逐步制约了项目的用户体验,为解决这一问题我们可以将项目剥离开来,让多 ...

  10. 课堂练习&课下作业----用户场景分析

    典型用户1 ·小明---一名普通大学生生 名字 小明 性别.年龄 男,19岁 职业 大学生 收入 无 知识层次和能力 石家庄铁道大学学生,善于交际. 生活/工作情况 成绩优异,各方面技能突出 动机,目 ...