SpringBoot + MyBatis配置连接MySQL、SQL Server、Oracle数据库_springboot 同时连接sql server与oracle

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Golang全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注go)
img

正文

com.alibaba
druid
1.1.13

(5)mysql 驱动

mysql mysql-connector-java runtime

(6)sqlserver 驱动

com.microsoft.sqlserver mssql-jdbc 7.4.1.jre8 runtime

(7)Oracle数据库驱动

com.oracle ojdbc14 10.2.0.4.0

(8)lombok主要用来记录日志

org.projectlombok lombok true

(9)tomcat

org.springframework.boot spring-boot-starter-tomcat provided

二、配置

2.1 application.properties配置文件

server.port= 9000
server.servlet.context-path= /multiDatabase

#mybatis
mybatis.mapper-locations=classpath:mapper/*/*.xml
#将扫描到的mapper.xml显示在控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#MaBatis在控制台打印执行的SQL语句 注意其中dump.xxx.xxxx.dao是Dao层的包名,注意不是XML文件的包名=日志等级
logging.level.dump.xxx.xxxx.dao=debug

#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.url=jdbc:mysql://localhost:3306/MySQL数据库名?useUnicode=true&characterEncoding=utf8&useSSL=false&useTimezone=true&serverTimezone=GMT%2B8
#spring.datasource.username=root
#spring.datasource.password=root

################################# 多数据源配置 #########################################
#参考:https://www.cnblogs.com/viwofer/p/13149863.html (Oracle数据源的配置使用默认的即可)
#如果同时配置了MySQL和SqlServer的数据源,并同时启动MySQL和SqlServer的数据源配置,注意是两个数据源的都同时启动,可能会出现如下错误并给出提示信息:
#错误:Field transactionManager in dump.pspaceTodb.base.aspect.TransactionAdviceConfig required a single bean, but 2 were found:
#提示信息:Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

#问题的原因:MySQL和SqlServer数据库的配置类没有分主次,所以导致启动失败;
#解决方法:在MySQL或SqlServer配置类的每个方法上都加上@Primary,注意只能在其中一个数据源上添加@Primary注解,在加了@Primary注解的配置类上,

执行的sql也会优先使用加了@Primary注解的配置类,所以数据只会优先出现在有@Primary注解对应的数据库中;

#MySQL数据库连接配置
spring.datasource.mysql.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.mysql.driver-class-name=com.mysql.cj.jdbc.Driver
#注意是jdbc-url不是url,否则报jdbcUrl is required with driverClassName错误
spring.datasource.mysql.jdbc-url=jdbc:mysql://localhost:3306/mysql数据库名?useUnicode=true&characterEncoding=utf8&useSSL=false&useTimezone=true&serverTimezone=GMT%2B8
spring.datasource.mysql.username=root
spring.datasource.mysql.password=root

#SqlServer数据库连接配置
spring.datasource.mssql.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.mssql.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
#注意是jdbc-url不是url,否则报jdbcUrl is required with driverClassName错误
spring.datasource.mssql.jdbc-url=jdbc:sqlserver://localhost:1433;databasename=SqlServer数据库名
spring.datasource.mssql.username=sa
spring.datasource.mssql.password=admin123

#Oracle数据库连接配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@192.168.18.200:1521:orcl
spring.datasource.username=root
spring.datasource.password=admin123

通过application.properties里的配置可以看出,项目默认的数据库配置是Oracle连接,所以在连接使用Oracle数据库时,需要把MySQL和SqlServer的 配置类 暂时注释掉,MySQL和SqlServer在application.properties里的配置信息不用注释。

2.2 dao、Mapper配置 省略

需要注意的是因为连接Oracle数据库是使用的SpringBoot默认的配置,所以需要在启动类上加上
@MapperScan扫描dao层接口

@MapperScan({“com.demo.test1.dao”,“com.demo.test2.dao”})

2.3 MySQL和SqlServer的数据源配置类

2.3.1 MySql数据源配置类

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;

/**
* MySql数据源配置类
* @author chenlc
* @created 2020-11-06 15:03
*/
@Configuration(value=“mysql”)
@MapperScan(basePackages = “com.demo.test1.dao”, sqlSessionFactoryRef = “MysqlSqlSessionFactory”)//此处@MapperScan扫的是dao层接口
public class MySqlDataSourceConfig {

@Primary
@Bean(name = “MysqlDataSource”)
@ConfigurationProperties(prefix = “spring.datasource.mysql”)
public DataSource getDateSource() {
return DataSourceBuilder.create().build();
}

/**
* 配置事务管理
*/
@Primary
@Bean(name = “MysqlTransactionManager”)
public DataSourceTransactionManager mysqlTransactionManager(@Qualifier(“MysqlDataSource”) DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}

/**
* 配置工厂
* @param datasource
* @return
* @throws Exception
*/
@Primary
@Bean(name = “MysqlSqlSessionFactory”)
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier(“MysqlDataSource”) DataSource datasource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
//此处 classpath:mapper/*/*.xml 对应的是Mapper文件
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“classpath:mapper/*/*.xml”));
return bean.getObject();
}

/**
* 配置会话
* @param sessionFactory
* @return
*/
@Primary
@Bean(“MysqlSqlSessionTemplate”)
public SqlSessionTemplate mysqlSessionTemplate(@Qualifier(“MysqlSqlSessionFactory”) SqlSessionFactory sessionFactory) {
return new SqlSessionTemplate(sessionFactory);
}
}

2.3.2 SqlServer数据源配置类

配置同MySQL数据源配置类的差不多一致,区别如下:
(1)MySQL数据源配置类里每个方法上都加了@Primary

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;

/**
* SqlServer数据源配置类
* @author chenlc
* @created 2020-11-06 15:05
*/
@Configuration(value = “mssql”)
@MapperScan(basePackages = “com.demo.test1.dao”, sqlSessionFactoryRef = “MssqlSqlSessionFactory”)
public class SqlServerDataSourceConfig {

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Go)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
class SqlServerDataSourceConfig {

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Go)
[外链图片转存中…(img-G8sfPeVj-1713161684539)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 28
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值