日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Spring boot配置多數據源代碼實例

瀏覽:101日期:2023-08-28 15:30:14

因項目需要在一個應用里從兩個數據庫取數,所以需要配置多數據源,網上找了好多方法才啟動成功,整理如下。注意兩個數據源的repository文件名不能相同,即使在不同的文件夾下,否則系統啟動會報錯。

配置文件

spring.datasource.primary.url=***spring.datasource.primary.username=***spring.datasource.primary.password=***spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driverspring.datasource.second.url=***spring.datasource.second.username=***spring.datasource.second.password=***spring.datasource.second.driver-class-name=com.mysql.jdbc.Driver

通用數據源配置

import com.alibaba.druid.pool.DruidDataSource;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import javax.sql.DataSource;/** * @author ruanshuai * @date 2020/5/13 */@Configurationpublic class DataSourceConfig { /** * 第一個數據連接,默認優先級最高 * @return */ @Primary @Bean(name = 'primaryDataSource') //數據源1配置名 @Qualifier('primaryDataSource') //數據源1配置名 @ConfigurationProperties(prefix='spring.datasource.primary') //見配置文件 public DataSource PrimaryDataSource() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); } /** * 第二個數據源 * @return */ @Bean(name = 'secondDataSource') //數據源2配置名 @Qualifier('secondDataSource') //數據源2配置名 @ConfigurationProperties(prefix='spring.datasource.second') //見配置文件 public DataSource secondaryDataSource() { return DataSourceBuilder.create().type(DruidDataSource.class).build(); }}

數據源1配置

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import org.springframework.orm.jpa.JpaTransactionManager;import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.persistence.EntityManager;import javax.sql.DataSource;import java.util.HashMap;import java.util.Map;/** * @author ruanshuai * @date 2020/5/13 */@Configuration@EnableTransactionManagement@EnableJpaRepositories( entityManagerFactoryRef='entityManagerFactoryPrimary', transactionManagerRef='transactionManagerPrimary', basePackages= { '***此處為數據源1 repository的存放文件夾***' })public class PrimaryConfig { @Autowired @Qualifier('primaryDataSource') private DataSource primaryDataSource; @Primary @Bean(name = 'entityManagerPrimary') public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactoryPrimary(builder).getObject().createEntityManager(); } @Primary @Bean(name = 'entityManagerFactoryPrimary') public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) { return builder.dataSource(primaryDataSource).properties(getVendorProperties()).packages('***實體類所在文件夾,兩個數據源的實體類可相同***').persistenceUnit('primaryPersistenceUnit').build(); } private Map<String, String> getVendorProperties() { Map<String, String> jpaProperties = new HashMap<>(16); jpaProperties.put('hibernate.hbm2ddl.auto', 'update'); jpaProperties.put('hibernate.show_sql', System.getProperty('spring.jpa.show-sql')); jpaProperties.put('hibernate.dialect', System.getProperty('spring.jpa.properties.hibernate.dialect')); jpaProperties.put('hibernate.current_session_context_class', 'org.springframework.orm.hibernate5.SpringSessionContext'); return jpaProperties; } @Primary @Bean(name = 'transactionManagerPrimary') public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject()); }}

數據源2配置

import org.omg.CORBA.Environment;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import org.springframework.orm.jpa.JpaTransactionManager;import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;import javax.persistence.EntityManager;import javax.sql.DataSource;import java.util.HashMap;import java.util.Map;/** * @author ruanshuai * @date 2020/5/13 */@Configuration@EnableTransactionManagement@EnableJpaRepositories( //實體管理 entityManagerFactoryRef='entityManagerFactorySecond', //事務管理 transactionManagerRef='transactionManagerSecond', //實體掃描,設置Repository所在位置 basePackages= { '***此處為數據源1 repository的存放文件夾***' })public class SecondConfig { @Autowired @Qualifier('secondDataSource') private DataSource secondDataSource; @Bean(name = 'entityManagerSecond') public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactorySecond(builder).getObject().createEntityManager(); } @Bean(name = 'entityManagerFactorySecond') public LocalContainerEntityManagerFactoryBean entityManagerFactorySecond (EntityManagerFactoryBuilder builder) { return builder.dataSource(secondDataSource).properties(getVendorProperties()).packages('***實體類所在文件夾,兩個數據源的實體類可相同***').persistenceUnit('secondPersistenceUnit').build(); } private Map<String, String> getVendorProperties() { Map<String, String> jpaProperties = new HashMap<>(16); jpaProperties.put('hibernate.hbm2ddl.auto', 'update'); jpaProperties.put('hibernate.show_sql', System.getProperty('spring.jpa.show-sql')); jpaProperties.put('hibernate.dialect', System.getProperty('spring.jpa.properties.hibernate.dialect')); jpaProperties.put('hibernate.current_session_context_class', 'org.springframework.orm.hibernate5.SpringSessionContext'); return jpaProperties; } @Bean(name = 'transactionManagerSecond') PlatformTransactionManager transactionManagerSecond(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactorySecond(builder).getObject()); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
动漫av一区| 婷婷成人在线| 国产情侣久久| 老鸭窝一区二区久久精品| 福利视频一区| 免费av一区二区三区四区| 伊人久久亚洲热| 日本中文字幕一区二区| 欧美精品影院| 国产一区二区三区成人欧美日韩在线观看| 欧产日产国产精品视频| 美日韩精品视频| 国产精品17p| 99久久亚洲精品| 日韩手机在线| 欧美二三四区| 亚洲综合福利| 精品亚洲免a| 伊人成人网在线看| 国产日产精品_国产精品毛片 | 综合日韩av| 日韩午夜av| 青草久久视频| 色偷偷色偷偷色偷偷在线视频| 亚洲一区二区免费看| 国产精品日韩精品中文字幕| 久久中文字幕二区| 91欧美日韩在线| 精精国产xxxx视频在线野外| 日韩中文av| 日本高清不卡一区二区三区视频| 亚洲精品一二三**| 精品国产一区二区三区噜噜噜| 99精品视频在线观看免费播放| 日韩国产91| 婷婷成人在线| 国产精品亚洲二区| 欧美成人综合| 久久91视频| 免费在线观看一区二区三区| 精品国产91| 综合激情视频| 91精品一区国产高清在线gif| 欧美日韩1区| 99pao成人国产永久免费视频| 国产精品99久久免费| 亚洲一区中文| 国产不卡精品在线| 日韩三级视频| 婷婷亚洲综合| 中文字幕高清在线播放| 日韩欧美另类中文字幕| 久久久一二三| 国语对白精品一区二区| 亚洲精品第一| 一区二区三区视频免费观看| 免费亚洲婷婷| 日韩黄色免费网站| 国产精品毛片| 香蕉久久99| 国产va在线视频| 国产精品传媒麻豆hd| 亚洲欧美日韩国产| 精精国产xxxx视频在线播放| 蜜桃精品视频| 国产毛片精品| 日本精品在线播放| 蜜臀av一区二区在线免费观看| 久久国产亚洲| 亚洲永久av| 国产成人免费视频网站视频社区| 欧美日韩一区二区三区不卡视频 | 视频一区在线播放| 亚洲天堂免费电影| 精品欧美视频| 国产免费久久| 日韩高清欧美激情| 在线免费观看亚洲| 黄色欧美日韩| bbw在线视频| 精品视频黄色| 欧美aⅴ一区二区三区视频| 亚洲91网站| 亚洲精品大全| 亚洲精品第一| 亚洲精品一级| 亚州av一区| 日韩在线网址| 免费在线看一区| 午夜在线视频观看日韩17c| 精品中文字幕一区二区三区av| 日韩欧美一区二区三区免费观看| 国产伊人久久| 精品美女久久| 老色鬼精品视频在线观看播放| 欧美一区二区三区免费看| 自拍自偷一区二区三区| 国产免费成人| 亚洲制服一区| 日韩美女国产精品| 日韩和欧美一区二区三区| 日韩一二三区在线观看| 亚洲精品影院在线观看| 亚洲青青久久| 日韩精品久久久久久| 日韩精品欧美成人高清一区二区| 亚洲丝袜美腿一区| 日韩一区免费| 国产欧美自拍| 久久精品国产99| 中文字幕人成乱码在线观看| 精品欧美一区二区三区在线观看| 免费高潮视频95在线观看网站| 特黄毛片在线观看| 99精品美女| 亚洲在线一区| 日韩激情精品| 美女视频黄久久| 欧美激情国产在线| 精品捆绑调教一区二区三区| 亚洲二区在线| 美女日韩在线中文字幕| 日韩精品一区二区三区中文在线| 国产欧美一区二区三区国产幕精品| 国产日产精品_国产精品毛片 | av资源亚洲| 欧美福利一区| 免费在线观看视频一区| 青青草视频一区| 麻豆91精品91久久久的内涵| av高清不卡| 丝袜美腿成人在线| 国产欧美日韩| 成人羞羞视频在线看网址| 日韩视频不卡| 日韩av电影一区| 水蜜桃精品av一区二区| 国产精品av久久久久久麻豆网| 影音先锋久久精品| 久久精品福利| 好吊日精品视频| 日韩av中文在线观看| 国精品产品一区| 99国产精品99久久久久久粉嫩| 视频一区二区国产| 国产精品成人一区二区网站软件| 久久久久久久欧美精品| 中文精品在线| 日韩国产高清在线| 国产成人黄色| 亚洲免费高清| 日本91福利区| 国产一区2区| 久热综合在线亚洲精品| 国产精品一卡| 婷婷丁香综合| 国产精品.xx视频.xxtv| 欧美性感美女一区二区| 日韩国产在线不卡视频| 日本美女一区| 婷婷综合福利| 日韩大片在线| 日本一区中文字幕| 久久蜜桃资源一区二区老牛| 日韩成人精品一区二区三区 | 欧美亚洲网站| 天堂日韩电影| 日韩avvvv在线播放| 美女福利一区二区三区| 日韩一区二区三免费高清在线观看| 久久久久免费| 天堂va欧美ⅴa亚洲va一国产| 毛片在线网站| 欧美日韩中出| 日韩视频不卡| 91综合视频| 亚洲免费成人av在线| 色爱综合av| 嫩草伊人久久精品少妇av杨幂| 亚洲欧美激情诱惑| 午夜av不卡| 欧美日本不卡| 老鸭窝亚洲一区二区三区| 在线天堂资源www在线污| 日日夜夜免费精品| 亚洲成人国产| 精品美女久久| 欧美伊人久久| 国产精品普通话对白| 国产一区二区三区四区大秀| 亚洲日产av中文字幕| 久久精品动漫| 麻豆国产精品一区二区三区| 男女男精品视频网| 欧美色图一区| 婷婷综合六月| 国产在线一区不卡| 久久精品999| 亚洲精品亚洲人成在线观看| 91久久久精品国产|