av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

瀏覽:14日期: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
相關文章:
主站蜘蛛池模板: 成人网av | 免费 视频 1级| 国产黄色大片在线免费观看 | www.国产| 色网站在线免费观看 | a级黄色片视频 | 成人亚洲网站 | 午夜伊人 | 国产精品一区二区久久久久 | 欧美视频在线看 | 午夜国产羞羞视频免费网站 | 羞羞网站免费观看 | 超碰成人在线观看 | 91精品国产综合久久香蕉麻豆 | 精品国产色 | 久久久综合精品 | 欧美精品在欧美一区二区少妇 | 在线观看www高清视频 | 欧美舔穴 | 成人国产免费视频 | 成人在线免费观看视频 | 久久99蜜桃综合影院免费观看 | 亚洲人人舔人人 | 国产精品一区二区欧美黑人喷潮水 | www.日本在线播放 | 国产精品久久久久久久久久久久冷 | 欧美理论片在线观看 | 欧美激情一区二区三级高清视频 | 在线看av的网址 | 国产高清一区二区 | 四虎在线播放 | 国产精品婷婷 | 精品久久久久久亚洲国产800 | a视频在线观看 | 免费看片在线播放 | 一区二区中文 | 精品欧美色视频网站在线观看 | 亚洲xx在线 | 久久精品亚洲精品国产欧美kt∨ | 香蕉一区| 日韩成人影院在线观看 |