当前位置:   article > 正文

springcloud+hibernate+JPA+EntityManager Oracle 双数据源配置_java中yml配置hibernate多数据源调用接口

java中yml配置hibernate多数据源调用接口

一个需求需要在服务里增加一个新的数据源,框架也有点奇葩查了一些资料终于研究好了!供大家借鉴!!!

  1. yml配置多个数据源(树形排列也可以)
    1. spring.datasource.primary.url=jdbc:oracle:thin:@//dev-ceshi:1521/d0prps
    2. spring.datasource.primary.username=123123
    3. spring.datasource.primary.password=123456
    4. spring.datasource.secondary.url=jdbc:oracle:thin:@//sit-ceshi:1521/t0prps
    5. spring.datasource.secondary.username=123456
    6. spring.datasource.secondary.password=123456
  2. 创建数据源配置类注意几个注解
    1. @Configuration:
      被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
      一句话解释就是用上这注解,系统启动是就会自动注入到容器中

    2.  @Primary:自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常

  3. 创建数据源类
    1. import org.springframework.beans.factory.annotation.Qualifier;
    2. import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
    3. import org.springframework.boot.context.properties.ConfigurationProperties;
    4. import org.springframework.context.annotation.Bean;
    5. import org.springframework.context.annotation.Configuration;
    6. import org.springframework.context.annotation.Primary;
    7. import javax.sql.DataSource;
    8. /**
    9. * @NAME:
    10. * @DATE: 2021/9/22
    11. * 双数据源配置
    12. **/
    13. @Configuration
    14. public class DataSourcesConfig {
    15. @Bean(name = "primaryDataSource")
    16. @Qualifier("primaryDataSource")
    17. @Primary//自动装配 就是默认使用此数据源
    18. @ConfigurationProperties(prefix="spring.datasource.primary")
    19. public DataSource primaryDataSource() {
    20. System.out.println("primary db built");
    21. return DataSourceBuilder.create().build();
    22. }
    23. @Bean(name = "secondaryDataSource")
    24. @Qualifier("secondaryDataSource")
    25. @ConfigurationProperties(prefix="spring.datasource.secondary")
    26. public DataSource secondaryDataSource() {
    27. System.out.println("secondary db built");
    28. return DataSourceBuilder.create().build();
    29. }
    30. }

    创建secondary数据源单独配置

    1. import org.springframework.beans.factory.annotation.Autowired;
    2. import org.springframework.beans.factory.annotation.Qualifier;
    3. import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
    4. import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    8. import org.springframework.orm.jpa.JpaTransactionManager;
    9. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    10. import org.springframework.transaction.PlatformTransactionManager;
    11. import org.springframework.transaction.annotation.EnableTransactionManagement;
    12. import javax.persistence.EntityManager;
    13. import javax.sql.DataSource;
    14. import java.util.Map;
    15. /**
    16. * @NAME:
    17. * @DATE: 2021/9/22
    18. **/
    19. @Configuration
    20. @EnableTransactionManagement//事务管理注解
    21. @EnableJpaRepositories(
    22. entityManagerFactoryRef = "entityManagerFactorySecondary",
    23. transactionManagerRef = "transactionManagerSecondary",
    24. basePackages = {"com.fulan.trade.nbzdata.repository"})
    25. public class SecondaryConfig
    26. {
    27. @Autowired
    28. private JpaProperties jpaProperties;
    29. @Autowired
    30. @Qualifier("secondaryDataSource")
    31. private DataSource secondaryDataSource;
    32. @Bean(name = "entityManagerSecondary")
    33. public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    34. return entityManagerFactorySecondary(builder).getObject().createEntityManager();
    35. }
    36. @Bean(name = "entityManagerFactorySecondary")
    37. public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary (EntityManagerFactoryBuilder builder) {
    38. return builder
    39. .dataSource(secondaryDataSource)
    40. .properties(getVendorProperties(secondaryDataSource))
    41. .packages("com.fulan.trade.nbzdata.entity") //设置实体类所在位置
    42. .persistenceUnit("secondaryPersistenceUnit")
    43. .build();
    44. }
    45. private Map<String, String> getVendorProperties(DataSource dataSource) {
    46. return jpaProperties.getHibernateProperties(dataSource);
    47. }
    48. @Bean(name = "transactionManagerSecondary")
    49. PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
    50. return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
    51. }
    52. }

    创建primary数据源单独配置

    1. import org.springframework.beans.factory.annotation.Autowired;
    2. import org.springframework.beans.factory.annotation.Qualifier;
    3. import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
    4. import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
    5. import org.springframework.context.annotation.Bean;
    6. import org.springframework.context.annotation.Configuration;
    7. import org.springframework.context.annotation.Primary;
    8. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    9. import org.springframework.orm.jpa.JpaTransactionManager;
    10. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
    11. import org.springframework.transaction.PlatformTransactionManager;
    12. import org.springframework.transaction.annotation.EnableTransactionManagement;
    13. import javax.persistence.EntityManager;
    14. import javax.sql.DataSource;
    15. import java.util.Map;
    16. /**
    17. * @NAME: hzan
    18. * @DATE: 2021/9/22
    19. **/
    20. @Configuration
    21. @EnableTransactionManagement
    22. @EnableJpaRepositories(
    23. entityManagerFactoryRef="entityManagerFactoryPrimary",
    24. transactionManagerRef="transactionManagerPrimary",
    25. basePackages= { "com.fulan.trade.repository" }) //设置Repository所在位置可以设置多个
    26. public class PrimaryConfig {
    27. @Autowired
    28. @Qualifier("primaryDataSource")
    29. private DataSource primaryDataSource;
    30. @Primary
    31. @Bean(name = "entityManagerPrimary")
    32. public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    33. return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    34. }
    35. @Primary
    36. @Bean(name = "entityManagerFactoryPrimary")
    37. public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
    38. return builder
    39. .dataSource(primaryDataSource)
    40. .properties(getVendorProperties(primaryDataSource))
    41. .packages("com.fulan.trade.entity") //设置实体类所在位置
    42. .persistenceUnit("primaryPersistenceUnit")
    43. .build();
    44. }
    45. @Autowired(required=false)
    46. private JpaProperties jpaProperties;
    47. private Map<String, String> getVendorProperties(DataSource dataSource) {
    48. return jpaProperties.getHibernateProperties(dataSource);
    49. }
    50. @Primary
    51. @Bean(name = "transactionManagerPrimary")
    52. public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
    53. return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    54. }
    55. }

    按照上面配置的repository位置创建接口就可以实现JPA的查询了

  4. hibernate+EntityManager使用

    当你使用EntityManager 时

    正常引用是 会默认上述自动装载的数据源

    @Autowired
    @PersistenceContext
    private EntityManager entityManager;

    可以通过

    @PersistenceContext 中的 unitName 属性进行设置 
    @Autowired
    @PersistenceContext(unitName="entityManagerFactorySecondary")
    private EntityManager entityManager;

    这样配置就可以自定数据源了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/815292
推荐阅读
相关标签
  

闽ICP备14008679号