springboot整合mybatis实现多数据库的切换

springboot整合mybatis:

  • 第一步配置数据源 
    在application.properties文件中配置数据源,如下配置:

spring.datasource.jdbcUrl=jdbc:oracle:thin:@10.21.16.140:1521:ora11g 
spring.datasource.username=brmuser 
spring.datasource.password=123456

Spring Boot 集成MyBatis有两种方式,一种简单的方式就是使用MyBatis官方提供的

mybatis-spring-boot-starter

在pom.xml中添加依赖:

这里写图片描述

在application.properties增加配置:

mybatis.typeAliasesPackage=com.entity:实体放置目录 
mybatis.configLocation=classpath:mybatis-config.xml:mybatis-config.xml放置目录 
mybatis.mapperLocations=classpath:mapper/*.xml: mapper的放置路径


mybatis配置实现多数据库切换:

在mybatis-config.xml文件中配置如下: 
这里写图片描述

配置VendorDatabaseIdProvider,mybatis会根据数据源连接返回的databaseId识别出当前数据源

  • 第三步配置全部完成,下面进行测试

一 .编写mapper.xml文件,如下图: 
这里写图片描述

通过databaseId标签识别出执行不通厂商的sql

二 .编写mapper.java文件,如下图: 
这里写图片描述

三 .进行测试 
这里写图片描述

执行效果:

数据源为mysql:执行select COUNT(title) from SYS_MENU 
数据源为oracle:执行select count(table_name) from user_tables

猜你喜欢

转载自blog.csdn.net/lldouble/article/details/80682178
今日推荐