springboot的jpa多数据源配置

configuration

@Configuration
@EntityScan("com.patterncat.domain")
@EnableJpaRepositories(basePackages = "com.patterncat",entityManagerFactoryRef = "pgEntityManagerFactory",
transactionManagerRef = "pgTransactionManager")
public class JpaConfig {

@Autowired
@Qualifier("pgDataSource")
public DataSource pgDataSource;

@Bean
PlatformTransactionManager pgTransactionManager() {
    return new JpaTransactionManager(pgEntityManagerFactory().getObject());
}

@Bean
LocalContainerEntityManagerFactoryBean pgEntityManagerFactory() {

    HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setGenerateDdl(true);
    jpaVendorAdapter.setDatabase(Database.POSTGRESQL);
    jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");

    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
    factoryBean.setDataSource(pgDataSource);
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
    factoryBean.setJpaPropertyMap(jpaProperties());

    return factoryBean;
}

private Map jpaProperties() {
    Map props = new HashMap();
    props.put("hibernate.ejb.naming_strategy",new SpringNamingStrategy());
    return props;
}

}

关键字:产品经理

版权声明

本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部