SpringBoot項目中同時操作多個數(shù)據(jù)庫的實現(xiàn)方法
在實際項目開發(fā)中可能存在需要同時操作兩個數(shù)據(jù)庫的場景,比如從A庫讀取數(shù)據(jù),進(jìn)行操作后往B庫中寫入數(shù)據(jù),此時就需要進(jìn)行多數(shù)據(jù)庫配置。本文以操作本地和線上的MySQL數(shù)據(jù)庫為例:
一、導(dǎo)入相關(guān)pom文件
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
二、application.yml配置文件編寫
單數(shù)據(jù)源的配置如下:
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/meal_order?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 username: root password: root type: com.alibaba.druid.pool.DruidDataSource
多數(shù)據(jù)源的配置如下:
spring: datasource: dev: driver-class-name: com.mysql.cj.jdbc.Driver jdbcUrl: jdbc:mysql://xxx.xx.xx.xx:3306/meal_order?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 username: root password: root type: com.alibaba.druid.pool.DruidDataSource local: driver-class-name: com.mysql.cj.jdbc.Driver jdbcUrl: jdbc:mysql://127.0.0.1:3306/db2021?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 username: root password: root type: com.alibaba.druid.pool.DruidDataSource
經(jīng)過對比可以發(fā)現(xiàn):
1、多數(shù)據(jù)源的配置中需要指定具體的名稱來區(qū)分不同的數(shù)據(jù)庫(上述配置中的dev和local,名稱可以根據(jù)具體需求自定義)
2、需要使用jdbcUrl代替url
三、數(shù)據(jù)庫連接配置文件
dev數(shù)據(jù)源配置文件:
@Configuration @MapperScan(basePackages = "com.multiple.mapper.dev",sqlSessionFactoryRef = "devSqlSessionFactory") public class DevDataSourceConfig { @Primary @Bean(name = "devDataSource") @ConfigurationProperties("spring.datasource.dev") public DataSource masterDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "devSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("devDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:mapping/dev/*.xml")); return sessionFactoryBean.getObject(); } }
local數(shù)據(jù)源配置文件:
@Configuration @MapperScan(basePackages = "com.multiple.mapper.local",sqlSessionFactoryRef = "localSqlSessionFactory") public class LocalDataSourceConfig { @Primary @Bean(name = "localDataSource") @ConfigurationProperties("spring.datasource.local") public DataSource masterDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "localSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("localDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:mapping/local/*.xml")); return sessionFactoryBean.getObject(); } }
不同配置文件通過@MapperScan注解的內(nèi)容來區(qū)分不同數(shù)據(jù)庫下的mapper文件,通過@ConfigurationProperties注解來加載指定的數(shù)據(jù)源
以DevDataSourceConfig為例
四、主啟動類注解修改
@SpringBootApplication(exclude={<!--{C}%3C!%2D%2D%20%2D%2D%3E-->DataSourceAutoConfiguration.class})
目錄結(jié)構(gòu)如下:
五、測試
從dev庫中查詢數(shù)據(jù),取出字段插入local庫中:
public interface DevMapper { @Select("select * from test") List<Test> getAllTest(); }
public interface LocalMapper { @Insert("insert into payment(serial) values (#{name})") int insertMessage(String name); }
@SpringBootTest class MultipleDatabaseApplicationTests { @Autowired private DevMapper devMapper; @Autowired private LocalMapper localMapper; @Test void contextLoads() { List<com.multiple.pojo.Test> testList = devMapper.getAllTest(); for(com.multiple.pojo.Test test : testList){ localMapper.insertMessage(test.getAa()); } } }
運行測試代碼,從dev庫中查出的數(shù)據(jù)可以成功添加至local庫
該方法也適用需要使用多種不同的數(shù)據(jù)庫的場景,比如MySQL和Oracle,修改數(shù)據(jù)源配置文件即可
到此這篇關(guān)于SpringBoot項目中同時操作多個數(shù)據(jù)庫的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)SpringBoot項目操作多個數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springcloud整合gateway實現(xiàn)網(wǎng)關(guān)的示例代碼
本文主要介紹了springcloud整合gateway實現(xiàn)網(wǎng)關(guān)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01JAVA微信掃碼支付模式二線上支付功能實現(xiàn)以及回調(diào)
本篇文章主要介紹了JAVA微信掃碼支付模式二線上支付功能實現(xiàn)以及回調(diào),這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11Springboot+Bootstrap實現(xiàn)增刪改查實戰(zhàn)
這篇文章主要介紹了Springboot+Bootstrap實現(xiàn)增刪改查實戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Java Thread之Sleep()使用方法總結(jié)
這篇文章主要介紹了Java Thread之Sleep()使用方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Java數(shù)據(jù)結(jié)構(gòu)之哈夫曼樹概述及實現(xiàn)
文中詳細(xì)講了關(guān)于Java哈夫曼樹的概述以及用Java實現(xiàn)的方法,對各位正在學(xué)習(xí)java數(shù)據(jù)結(jié)構(gòu)的小伙伴們有很大的幫助喲,需要的朋友可以參考下2021-05-05SpringBoot項目部署到服務(wù)器上的方法(Jar包)
這篇文章主要介紹了SpringBoot項目部署到服務(wù)器上的方法(Jar包),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀
這篇文章主要給大家介紹了關(guān)于如何解決調(diào)用ftpClient.retrieveFileStream(String?remoteFilePath)第二次讀取為空問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08