查看: 1854|回复: 0

[Java学习] spring boot springjpa 支持多个数据源的实例代码

发表于 2018-5-5 09:30:53

1.SpringBoot的程序启动类

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  3. import org.springframework.boot.builder.SpringApplicationBuilder;
  4. import org.springframework.boot.context.web.SpringBootServletInitializer;
  5. import org.springframework.context.annotation.ComponentScan;
  6. @ComponentScan
  7. @EnableAutoConfiguration
  8. //@EnableJpaRepositories(basePackages = "com.sonychina.backend.repository")
  9. public class Application extends SpringBootServletInitializer {
  10. public static void main(String[] args) {
  11. SpringApplication app = new SpringApplication(Application.class);
  12. app.run(args);
  13. //SpringApplication.run(Application.class, args);
  14. }
  15. @Override
  16. protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
  17. return application.sources(Application.class);
  18. }
  19. }
复制代码

2.双数据源配置类

  1. import java.util.Map;
  2. import javax.sql.DataSource;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
  5. import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder;
  6. import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
  7. import org.springframework.boot.context.properties.ConfigurationProperties;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.context.annotation.Primary;
  11. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  12. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  13. import com.test.entity.statistic.SysUser;
  14. import com.test.repository.system.SystemRepository;
  15. @Configuration
  16. @EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactoryPrimary",
  17. basePackageClasses= {SystemRepository.class})
  18. public class GlobalDataConfiguration {
  19. // @Autowired
  20. // private DBConfig dbConfig;
  21. @Autowired
  22. private JpaProperties jpaProperties;
  23. @Bean(name="primaryDataSource")
  24. @Primary
  25. @ConfigurationProperties(prefix="datasource.primary")
  26. public DataSource primaryDataSource() {
  27. System.out.println("-------------------- primaryDataSource init ---------------------");
  28. return DataSourceBuilder.create().build();
  29. }
  30. @Bean(name="secondaryDataSource")
  31. @ConfigurationProperties(prefix="datasource.secondary")
  32. public DataSource secondaryDataSource() {
  33. System.out.println("-------------------- secondaryDataSource init ---------------------");
  34. // DataSourceBuilder factory = DataSourceBuilder
  35. // .create(DBConfig.class.getClassLoader())
  36. // .driverClassName(dbConfig.getDriver())
  37. // .url(dbConfig.getUrl())
  38. // .username(dbConfig.getUser())
  39. // .password(dbConfig.getPassword());
  40. // return factory.build();
  41. return DataSourceBuilder.create().build();
  42. }
  43. // @Bean(name = "entityManagerPrimary")
  44. // @Primary
  45. // public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
  46. // return customerEntityManagerFactory(builder).getObject().createEntityManager();
  47. // }
  48. @Bean(name="entityManagerFactoryPrimary")
  49. @Primary
  50. public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(EntityManagerFactoryBuilder builder) {
  51. return builder.dataSource(primaryDataSource())
  52. .properties(getVendorProperties(primaryDataSource()))
  53. .packages(SysUser.class)
  54. .persistenceUnit("system")
  55. .build();
  56. }
  57. private Map<String, String> getVendorProperties(DataSource dataSource) {
  58. return jpaProperties.getHibernateProperties(dataSource);
  59. }
  60. }
复制代码

3.第二个jpa实体管理器

  1. import java.util.Map;
  2. import javax.sql.DataSource;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Qualifier;
  5. import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilder;
  6. import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  10. import org.springframework.orm.jpa.JpaTransactionManager;
  11. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  12. import org.springframework.transaction.PlatformTransactionManager;
  13. import org.springframework.transaction.annotation.EnableTransactionManagement;
  14. import com.test.entity.manage.Banner;
  15. import com.test.repository.manage.BannerRepository;
  16. @Configuration
  17. @EnableTransactionManagement
  18. @EnableJpaRepositories(entityManagerFactoryRef="entityManagerFactorySecondary",
  19. transactionManagerRef="transactionManagerSecondary",
  20. basePackageClasses= {BannerRepository.class})
  21. public class SecondEMFBConfig {
  22. @Autowired
  23. private JpaProperties jpaProperties;
  24. @Autowired@Qualifier("secondaryDataSource")
  25. private DataSource dataSource;
  26. // @Bean(name = "entityManagerPrimary")
  27. // @Primary
  28. // public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
  29. // return customerEntityManagerFactory(builder).getObject().createEntityManager();
  30. // }
  31. @Bean(name="entityManagerFactorySecondary")
  32. public LocalContainerEntityManagerFactoryBean customerEntityManagerFactory(EntityManagerFactoryBuilder builder) {
  33. return builder.dataSource(dataSource)
  34. .properties(getVendorProperties(dataSource))
  35. .packages(Banner.class)
  36. .persistenceUnit("customers")
  37. .build();
  38. }
  39. private Map<String, String> getVendorProperties(DataSource dataSource) {
  40. return jpaProperties.getHibernateProperties(dataSource);
  41. }
  42. @Bean(name = "transactionManagerSecondary")
  43. PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
  44. return new JpaTransactionManager(customerEntityManagerFactory(builder).getObject());
  45. }
  46. }
复制代码

4.repository类举例

  1. import org.springframework.data.jpa.repository.JpaRepository;
  2. import org.springframework.data.jpa.repository.Modifying;
  3. import org.springframework.data.jpa.repository.Query;
  4. import com.test.entity.manage.Banner;
  5. public interface BannerRepository extends JpaRepository<Banner, Long> {
  6. @Modifying
  7. @Query("update Banner m set m.name=?1 where m.id=?2")
  8. public void update(String bannerName, Long id);
  9. }
复制代码

1.5.注意:对@Primary修饰的LocalContainerEntityManagerFactoryBean可以不用指定TransactionManager,spring上下文自动使用默认的JpaTransactionManager,但是对于第二个或第三个等等必须指定TransactionManager。可以参考springboot官方文档中的相关章节。

总结

以上所述是小编给大家介绍的spring boot springjpa 支持多个数据源的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对程序员之家网站的支持!



回复

使用道具 举报