大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 JPA多數據源配置代碼

JPA多數據源配置代碼

更新時間:2022-06-23 10:20:19 來源:動力節點 瀏覽1763次

package com.ym.zhproservice.config;
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 javax.sql.DataSource;
@Configuration
public class DataSourceConfig {
  @Bean(name = "primaryDataSource")
  @Qualifier("primaryDataSource")
  @ConfigurationProperties(prefix = "spring.datasource.primary")
  public DataSource masterDataSource() {
    return DataSourceBuilder.create().build();
  }
  @Bean(name = "secondaryDataSource")
  @Qualifier("secondaryDataSource")
  @Primary
  @ConfigurationProperties(prefix="spring.datasource.secondary")
  public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
  }
}
package com.ym.zhproservice.config; 
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.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef = "entityManagerFactoryPrimary",
    transactionManagerRef = "transactionManagerPrimary",
    basePackages = {"com.ym.zhproservice.dao.primary"} //設置dao所在位置
)
public class PrimarySourceConfig { 
  @Autowired
  @Qualifier("primaryDataSource")
  private DataSource primaryDataSource; 
  @Primary
  @Bean(name = "entityManagerPrimary")
  public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
  } 
  @Resource
  private Properties jpaProperties; 
  @Primary
  @Bean(name = "entityManagerFactoryPrimary")
  public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = builder
        .dataSource(primaryDataSource)
        .packages("com.ym.zhproservice.bean.primary") //設置實體類所在位置
        .persistenceUnit("primaryPersistenceUnit")
        .build();
    entityManagerFactory.setJpaProperties(jpaProperties);
    return entityManagerFactory;
  } 
  @Primary
  @Bean(name = "transactionManagerPrimary")
  public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
  }
}
package com.ym.zhproservice.config;
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.annotation.Resource;
import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef="entityManagerFactorySecondary",
    transactionManagerRef="transactionManagerSecondary",
    basePackages= { "com.ym.zhproservice.dao.secondary" }) //設置dao所在位置
public class SecondarySourceConfig {
  @Autowired
  @Qualifier("secondaryDataSource")
  private DataSource secondaryDataSource;
  @Bean(name = "entityManagerSecondary")
  public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    return entityManagerFactorySecondary(builder).getObject().createEntityManager();
  }
  @Resource
  private Properties jpaProperties;
  @Bean(name = "entityManagerFactorySecondary")
  public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = builder
        .dataSource(secondaryDataSource)
        .packages("com.ym.zhproservice.bean.secondary") //設置實體類所在位置
        .persistenceUnit("secondaryPersistenceUnit")
        .build();
    entityManagerFactory.setJpaProperties(jpaProperties);
    return entityManagerFactory;
  }
  @Bean(name = "transactionManagerSecondary")
  PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
  }
}

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 欧美一级视频免费观看 | 亚洲天天网综合自拍图片专区 | 成人亚欧网站在线观看 | 日本国产成人精品视频 | 日韩欧美国产中文字幕 | 中文无码久久精品 | 欧美国产精品久久 | 久久综合给会久久狠狠狠 | 91精品国产免费久久久久久青草 | 2023中文字幕在线观看 | 色婷婷综合网 | 国产精品亚洲高清一区二区 | 国产福利视频一区 | 天啪天干在线视频 | 欧美久久网| 91免费网站在线看入口黄 | 国产91在线|亚洲 | 久久精品国产久金国产思思 | 亚洲欧美另类久久久精品能播放的 | 中文字幕日韩女同互慰视频 | 欧美视频在线观看一区二区 | 日韩久久精品一区二区三区 | 黄色网在线 | 国产精品久久久久久爽爽爽 | xxxx国产片| 欧美成人久久久 | 看一级特黄a大片日本片黑人 | 亚洲国产精品久久精品成人 | 西西做人爱免费视频 | 1024国产欧美日韩精品 | 国产成人a在一区线观看高清 | 四虎官方影库 | 欧美5o老妇性xxx | 99热国产这里只有精品9九 | 久久男人资源站 | 中文精品久久久久国产不卡 | 午夜视频福利在线观看 | 中国一级毛片免费观看 | 久久精品国产屋 | 免费国产一级 | www.久久在线|