spring boot springjpa 支持多個數(shù)據(jù)源的實例代碼
1.SpringBoot的程序啟動類
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.web.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; @ComponentScan @EnableAutoConfiguration //@EnableJpaRepositories(basePackages = "com.sonychina.backend.repository") public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication app = new SpringApplication(Application.class); app.run(args); //SpringApplication.run(Application.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } }
2.雙數(shù)據(jù)源配置類
import java.util.Map; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; 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 org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import com.test.entity.statistic.SysUser; import com.test.repository.system.SystemRepository; @Configuration @EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactoryPrimary", basePackageClasses= {SystemRepository.class}) public class GlobalDataConfiguration { // @Autowired // private DBConfig dbConfig; @Autowired private JpaProperties jpaProperties; @Bean(name="primaryDataSource") @Primary @ConfigurationProperties(prefix="datasource.primary") public DataSource primaryDataSource() { System.out.println("-------------------- primaryDataSource init ---------------------"); return DataSourceBuilder.create().build(); } @Bean(name="secondaryDataSource") @ConfigurationProperties(prefix="datasource.secondary") public DataSource secondaryDataSource() { System.out.println("-------------------- secondaryDataSource init ---------------------"); // DataSourceBuilder factory = DataSourceBuilder // .create(DBConfig.class.getClassLoader()) // .driverClassName(dbConfig.getDriver()) // .url(dbConfig.getUrl()) // .username(dbConfig.getUser()) // .password(dbConfig.getPassword()); // return factory.build(); return DataSourceBuilder.create().build(); } // @Bean(name = "entityManagerPrimary") // @Primary // public EntityManager entityManager(EntityManagerFactoryBuilder builder) { // return customerEntityManagerFactory(builder).getObject().createEntityManager(); // } @Bean(name="entityManagerFactoryPrimary") @Primary public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(EntityManagerFactoryBuilder builder) { return builder.dataSource(primaryDataSource()) .properties(getVendorProperties(primaryDataSource())) .packages(SysUser.class) .persistenceUnit("system") .build(); } private Map<String, String> getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } }
3.第二個jpa實體管理器
import java.util.Map; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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 com.test.entity.manage.Banner; import com.test.repository.manage.BannerRepository; @Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactorySecondary", transactionManagerRef="transactionManagerSecondary", basePackageClasses= {BannerRepository.class}) public class SecondEMFBConfig { @Autowired private JpaProperties jpaProperties; @Autowired@Qualifier("secondaryDataSource") private DataSource dataSource; // @Bean(name = "entityManagerPrimary") // @Primary // public EntityManager entityManager(EntityManagerFactoryBuilder builder) { // return customerEntityManagerFactory(builder).getObject().createEntityManager(); // } @Bean(name="entityManagerFactorySecondary") public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(EntityManagerFactoryBuilder builder) { return builder.dataSource(dataSource) .properties(getVendorProperties(dataSource)) .packages(Banner.class) .persistenceUnit("customers") .build(); } private Map<String, String> getVendorProperties(DataSource dataSource) { return jpaProperties.getHibernateProperties(dataSource); } @Bean(name = "transactionManagerSecondary") PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(customerEntityManagerFactory(builder).getObject()); } }
4.repository類舉例
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import com.test.entity.manage.Banner; public interface BannerRepository extends JpaRepository<Banner, Long> { @Modifying @Query("update Banner m set m.name=?1 where m.id=?2") public void update(String bannerName, Long id); }
1.5.注意:對@Primary修飾的LocalContainerEntityManagerFactoryBean可以不用指定TransactionManager,spring上下文自動使用默認的JpaTransactionManager,但是對于第二個或第三個等等必須指定TransactionManager??梢詤⒖約pringboot官方文檔中的相關(guān)章節(jié)。
總結(jié)
以上所述是小編給大家介紹的spring boot springjpa 支持多個數(shù)據(jù)源的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Spring Boot+Jpa多數(shù)據(jù)源配置的完整步驟
- springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫
- Spring boot中使用Spring-data-jpa方便快捷的訪問數(shù)據(jù)庫(推薦)
- Spring boot jpa 刪除數(shù)據(jù)和事務(wù)管理的問題實例詳解
- 詳解基于Spring Boot與Spring Data JPA的多數(shù)據(jù)源配置
- SpringBoot連接MYSQL數(shù)據(jù)庫并使用JPA進行操作
- Spring Boot 添加MySQL數(shù)據(jù)庫及JPA實例
- Spring Boot中使用Spring-data-jpa實現(xiàn)數(shù)據(jù)庫增刪查改
- SpringBoot整合JPA數(shù)據(jù)源方法及配置解析
相關(guān)文章
基于Comparator對象集合實現(xiàn)多個條件按照優(yōu)先級的比較
這篇文章主要介紹了基于Comparator對象集合實現(xiàn)多個條件按照優(yōu)先級的比較,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07Java 高并發(fā)三:Java內(nèi)存模型和線程安全詳解
本文主要介紹Java高并發(fā)內(nèi)存模型和線程安全的資料,這里整理詳細的資料及1.原子性 2.有序性 3.可見性 4.Happen-Before 5.線程安全的概念,有需要的小伙伴可以參考下2016-09-09深入淺析Java中Static Class及靜態(tài)內(nèi)部類和非靜態(tài)內(nèi)部類的不同
上次有朋友問我,java中的類可以是static嗎?我給他肯定的回答是可以的,在java中我們可以有靜態(tài)實例變量、靜態(tài)方法、靜態(tài)塊。當(dāng)然類也可以是靜態(tài)的,下面小編整理了些關(guān)于java中的static class相關(guān)資料分享在腳本之家平臺供大家參考2015-11-11Spring Boot中使用Activiti的方法教程(二)
工作流(Workflow),就是“業(yè)務(wù)過程的部分或整體在計算機應(yīng)用環(huán)境下的自動化”,下面這篇文章主要給大家介紹了關(guān)于Spring Boot中使用Activiti的相關(guān)資料,需要的朋友可以參考下2018-08-08SpringCloud網(wǎng)關(guān)組件zuul實例解析
這篇文章主要介紹了SpringCloud網(wǎng)關(guān)組件zuul實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03