SpringBoot+JPA数据源配置&解决springboot2.1.x之后jpaProperties报错的问题

springboot2.1.x之后整合JPA配置多数据源时

HibernateSettings hibernateSettings = new HibernateSettings();

jpaProperties.getHibernateProperties(hibernateSettings)报错

 

(springboot2.1.x之后不再包含此方法)

 

解决此报错:

1. 将jpaProperties.getHibernateProperties所在方法getVendorProperties()注释掉;

2. 注入:

@Autowired

private HibernateProperties hibernateProperties;

3.更改properties()方法参数: 

将原getVendorProperties()改为:

Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());

@Primary
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder entityManagerFactoryBuilder)
{
    // 解决2.1.x版本jpaProperties.getHibernateProperties(hibernateSettings);失效的问题
    Map<String, Object> properties =         
    hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new 
    HibernateSettings());
    return entityManagerFactoryBuilder.dataSource(dataSource())
        // .properties(getVendorProperties()) // 2.1.X后不可用 
        .properties(properties)// 更改为此方式
        .packages("com.api.jpa.entity") // 实体类位置
        .persistenceUnit("persistenceUnit")// 持久性单元的名称。
        .build();
}

 

 

附上JPA数据源配置代码:

@Configuration
//jpa配置
@EnableJpaRepositories(entityManagerFactoryRef = "fastApiEntityManagerFactory", // 实体工厂
    transactionManagerRef = "transcationManagementFastApi", // 事务
    basePackages = {"com.api.jpa.repository"} // Repository 位置
)
public class DBConfig
{
    @Resource
    private JpaProperties jpaProperties;
    
    @Autowired
    private HibernateProperties hibernateProperties;// 解决2.1.x版本jpaProperties.getHibernateProperties(hibernateSettings);失效的问题
    
    @Bean(name = "jdbcTemplate")
    public JdbcTemplate jdbcTemplateStation(DataSource dataSource)
    {
        return new JdbcTemplate(dataSource);
    }
    
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.fastapi")
    public DataSource dataSource()
    {
        return DataSourceBuilder.create().build();
    }
    
    @Primary
    @Bean(name = "transcationManagementFastApi")
    public PlatformTransactionManager primaryTransactionManage(DataSource dataSource)
    {
        return new DataSourceTransactionManager(dataSource);
    }
    
    /**
     * 
     * @title 配置sessionFactory
     * @Describtion
     * @Date 2018年12月19日 下午1:38:24
     * @author zch
     * @param emf
     * @return
     */
    @Bean(name = "fastApiSessionFactory")
    public SessionFactory sessionFactory(@Qualifier("fastApiEntityManagerFactory") EntityManagerFactory emf)
    {
        return emf.unwrap(SessionFactory.class);
    }
    
    /**
     * 
     * @title 实体类管理配置,类似于hibernate的session
     * @Describtion
     * @Date 2019年10月23日 下午3:20:56
     * @author zch
     * @return
     */
    @Primary
    @Bean(name = "fastApiEntityManager")
    public EntityManager entityManager(EntityManagerFactoryBuilder entityManagerFactoryBuilder)
    {
        return entityManagerFactoryBean(entityManagerFactoryBuilder).getObject().createEntityManager();
    }
    
    /**
     * 实体类管理工厂,类似于hibernate的sessionFactory
     * 
     * @title
     * @Describtion
     * @Date 2019年10月23日 下午3:33:46
     * @author zch
     * @param entityManagerFactoryBuilder * persistenceUnit 持久性单元的名称。 如果只建立一个EntityManagerFactory,你可以省略这个,但是如果在同一个应用程序中有多个,你应该给它们不同的名字 properties 标准JPA或供应商特定配置的通用属性。 这些属性覆盖构造函数中提供的任何值。
     * @return
     */
    @Primary
    @Bean(name = "fastApiEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(EntityManagerFactoryBuilder entityManagerFactoryBuilder)
    {
        // 解决2.1.x版本jpaProperties.getHibernateProperties(hibernateSettings);失效的问题
        Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
        return entityManagerFactoryBuilder.dataSource(dataSource())
            // .properties(getVendorProperties())
            .properties(properties)
            .packages("com.api.jpa.entity") // 实体类位置
            .persistenceUnit("fastApiPersistenceUnit")// 持久性单元的名称。
            .build();
    }
    
    /**
     * 
     * @title 获取hibernate配置
     * @Describtion
     * @Date 2019年10月23日 下午3:42:48
     * @author zch
     * @return
     */
//    private Map<String, Object> getVendorProperties()
//    {
//        HibernateSettings hibernateSettings = new HibernateSettings();
//        return jpaProperties.getHibernateProperties(hibernateSettings);// (springboot2.1.x后失效)
//    }
    
}

application.yml中datasource:

datasource:
    fastapi:
      jdbc-url: 
      username: 
      password: 
      driver-class-name:  com.mysql.cj.jdbc.Driver
      validationQuery: SELECT 1
      type: org.apache.commons.dbcp2.BasicDataSource
  jpa: #统一JPA配置
    database: MYSQL
    show-sql: true
    properties: 
      hibernate:  
        dialect: org.hibernate.dialect.MySQL5Dialect
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect

  # db2: 配置多数据源

 多数据源配置:

配置新的DataSource

配置新的事务管理

配置新的entityManagerFactory(配置新的sessionFactory,配置新的entityManager)

配置JPA:

@EnableJpaRepositories(entityManagerFactoryRef = "", // 实体工厂
    transactionManagerRef = "", // 事务
    basePackages = {""} // Repository 位置
)

(注意设置默认数据源@Primary不要冲突)

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
【资源说明】 1、基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 2、该资源包括项目的全部源码,下载可以直接使用! 3、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 4、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip 基于SpringBoot+Thymeleaf+JPA的博客系统源码.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值