您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)spring boot springjpa如何支持多個(gè)數(shù)據(jù)源,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
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); } }
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); } }
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()); } }
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.注意:對(duì)@Primary修飾的LocalContainerEntityManagerFactoryBean可以不用指定TransactionManager,spring上下文自動(dòng)使用默認(rèn)的JpaTransactionManager,但是對(duì)于第二個(gè)或第三個(gè)等等必須指定TransactionManager。
關(guān)于“spring boot springjpa如何支持多個(gè)數(shù)據(jù)源”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。