当前位置 : 主页 > 编程语言 > java >

JPA配置

来源:互联网 收集:自由互联 发布时间:2021-06-28
gistfile1.txt package cn.com.lianyitech.calculate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;import org.springframework.boot.autoconfigure.
gistfile1.txt
package cn.com.lianyitech.calculate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
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.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Map;

@Configuration
@ConditionalOnMissingClass("calculateDataSource")
@EnableJpaRepositories(
        entityManagerFactoryRef = "calculateEntityManagerFactory",
        transactionManagerRef = "calculateTransactionManager",//calculateTransactionManager
        basePackages = "cn.com.lianyitech.calculate.repository")
@EntityScan("cn.com.lianyitech.calculate.repository")
@EnableTransactionManagement
public class CalculateDatasourceConfigurer {

    @Bean
    public PlatformTransactionManager calculateTransactionManager() {
        return new JpaTransactionManager(calculateEntityManagerFactory().getObject());
    }

    @Autowired
    JpaProperties jpaProperties;
    @Autowired
    Environment environment;

    @Bean
    LocalContainerEntityManagerFactoryBean calculateEntityManagerFactory() {
        Map
 
   properties = jpaProperties.getProperties();
        properties.put("hibernate.default_schema", environment.getProperty("calculate.datasource.username"));
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
        jpaVendorAdapter.setGenerateDdl(true);
        jpaVendorAdapter.setDatabase(Database.ORACLE);
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(calculateDataSource());
        factoryBean.setJpaPropertyMap(properties);
        factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        factoryBean.setPersistenceUnitName("calculate");
        factoryBean.setPackagesToScan(CalculateDatasourceConfigurer.class.getPackage().getName());
        return factoryBean;
    }

    @Bean
    @ConfigurationProperties("calculate.dataSource")
    public DataSource calculateDataSource() {
        return DataSourceBuilder.create().build();
    }

}
 
上一篇:短信验证码定时器
下一篇:App轮播效果
网友评论