springboot項目連接多種數(shù)據(jù)庫該如何操作詳析
前言
在項目的開發(fā)中,經(jīng)常會遇到需要連接多個多種數(shù)據(jù)庫的情況,mysql、oracle等等,下面詳細(xì)講解如何在一個服務(wù)中進行多種數(shù)據(jù)庫的配置。
第一步:
在yml配置文件中配置多個數(shù)據(jù)源,如下,根據(jù)實際情況更改自己的配置即可。
spring: datasource: # 配置多個數(shù)據(jù)源 primary: type: com.alibaba.druid.pool.DruidDataSource jdbc-url: jdbc:oracle:thin:@171.28.7.55:1521:example username: root password: root driver-class-name: oracle.jdbc.OracleDriver #數(shù)據(jù)庫鏈接驅(qū)動 secondary: type: com.alibaba.druid.pool.DruidDataSource jdbc-url: jdbc:mysql://127.0.0.1:3306/exinfo?useUnicode=true&characterEncoding=utf-8 username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver #數(shù)據(jù)庫鏈接驅(qū)動
第二步:
創(chuàng)建多個配置類,以配置oracle和mysql兩個數(shù)據(jù)庫為例,可參考代碼進行延展。
1.在配置類中需要進行數(shù)據(jù)源配置
@Bean @ConfigurationProperties(prefix = "spring.datasource.primary") @Primary public DataSource db1DataSource() { return DataSourceBuilder.create().build(); }
@ConfigurationProperties(prefix = "spring.datasource.primary")用于綁定yml中的第一個數(shù)據(jù)源配置,這些配置項會被自動映射到db1DataSource所創(chuàng)建的數(shù)據(jù)源實例中。通過DataSourceBuilder. create()創(chuàng)建一個新的數(shù)據(jù)源構(gòu)建器,并調(diào)用.build()方法來完成數(shù)據(jù)源實例的創(chuàng)建。 如果有多個相同類型的Bean,使用@Primary注解可以標(biāo)記出一個優(yōu)先(默認(rèn))使用的Bean。所以使用最多的數(shù)據(jù)庫可以使用@Primary注解。
2.配置MyBatis的SqlSessionFactory,它是MyBatis操作數(shù)據(jù)庫的核心組件,負(fù)責(zé)創(chuàng)建SqlSession對象,執(zhí)行SQL語句等。使用名稱為db1DataSource的數(shù)據(jù)源Bean作為構(gòu)造SqlSessionFactory的依賴。
@Bean @Primary public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);//設(shè)置下劃線轉(zhuǎn)駝峰式 return bean.getObject(); }
3.配置事務(wù)管理器(DataSourceTransactionManager),它基于數(shù)據(jù)源(DataSource)的事務(wù)管理實現(xiàn),專門用于JDBC事務(wù)處理。注解明確指定使用名為db1DataSource的數(shù)據(jù)源Bean。
@Bean @Primary public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); }
4.配置SqlSessionTemplate實例,它是MyBatis與Spring集成的關(guān)鍵組件,提供了線程安全的SQL會話執(zhí)行環(huán)境。
@Bean @Primary public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); }
5.類注解@MapperScan
basePackages= "com.example.mapper":指定了Mapper接口所在的包路徑。Spring會掃描這個包及其子包下的所有接口,如果接口符合MyBatis Mapper的規(guī)范,Spring會自動生成代理對象來處理SQL調(diào)用。
sqlSessionTemplateRef = "db1SqlSessionTemplate":指定了與Mapper接口綁定的sqlSessionTemplate的名稱。在執(zhí)行Mapper接口的方法時,Spring會使用這個指定的sqlSessionTemplate來管理SQL會話。這里的db1SqlSessionTemplate是之前通過@Bean方法定義的sqlSessionTemplate Beam的名稱。
DataSourcePrimaryConfig完整代碼如下:
package com.example.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.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.example.mapper", sqlSessionTemplateRef = "db1SqlSessionTemplate") public class DataSourcePrimaryConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource.primary") @Primary public DataSource db1DataSource() { return DataSourceBuilder.create().build(); } @Bean @Primary public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); //bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);//設(shè)置下劃線轉(zhuǎn)駝峰式 return bean.getObject(); } @Bean @Primary public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean @Primary public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); } }
DataSourceSecondaryConfig代碼如下:
package com.example.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.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration @MapperScan(basePackages = "com.example.mapper2", sqlSessionTemplateRef = "db2SqlSessionTemplate") public class DataSourceSecondaryConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSource db2DataSource() { return DataSourceBuilder.create().build(); } @Bean public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);//設(shè)置下劃線轉(zhuǎn)駝峰式 return bean.getObject(); } @Bean public DataSourceTransactionManager db2TransactionManager(@Qualifier("db2DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){ return new SqlSessionTemplate(sqlSessionFactory); } }
第三步:
根據(jù)配置文件,創(chuàng)建兩個mapper包如下:
在不同的mapper包下進行不同數(shù)據(jù)庫的交互即可。
總結(jié)
到此這篇關(guān)于springboot項目連接多種數(shù)據(jù)庫該如何操作的文章就介紹到這了,更多相關(guān)springboot連接多種數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Spring WEB應(yīng)用實例化如何實現(xiàn)
這篇文章主要介紹了Java Spring WEB應(yīng)用實例化如何實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12Java中的SynchronousQueue阻塞隊列及使用場景解析
這篇文章主要介紹了Java中的SynchronousQueue阻塞隊列及使用場景解析,SynchronousQueue 是 Java 中的一個特殊的阻塞隊列,它的主要特點是它的容量為0,這意味著 SynchronousQueue不會存儲任何元素,需要的朋友可以參考下2023-12-12用3個實例從原理到實戰(zhàn)講清楚Log4j史詩級漏洞
最近應(yīng)該很多人都在關(guān)注著一個漏洞Apache Log4j 2遠(yuǎn)程代碼執(zhí)行,該漏洞一旦被攻擊者利用會造成嚴(yán)重危害,這篇文章主要給大家介紹了關(guān)于如何用3個實例從原理到實戰(zhàn)講清楚Log4j史詩級漏洞的相關(guān)資料,需要的朋友可以參考下2021-12-12Spring中Bean創(chuàng)建完后打印語句的兩種方法
這篇文章主要介紹了Spring中Bean創(chuàng)建完后打印語句的兩種方法,一個是實現(xiàn)InitializingBean接口,另一個使用@Bean注解和initMethod屬性,通過代碼示例介紹的非常詳細(xì),感興趣的小伙伴可以參考閱讀2023-07-07Spring Cloud重試機制與各組件的重試總結(jié)
這篇文章主要給大家介紹了關(guān)于Spring Cloud中重試機制與各組件的重試的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11