Spring 学习

Spring boot2.0 spring-data-jpa使用

2019-02-13  本文已影响12人  桥er桑

介绍

spring boot2.0 多数据源时jpa的使用
参考:Baeldung文档

1.application.properties配置

spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none

2.Multiple Data Sources

略...

3.The Entities

User Entity in jack data source

package com.jack.jackdawson.entity.jack;

@Entity
@Table(name = "user")
public class User {
    @Id
    private Long id;
    @Column
    private String name;

}

4.The JPA Repositories

package com.jack.jackdawson.dao.jack;

@Repository
public interface UserDAO extends JpaRepository<User, Long> {
}

5.Configure JPA with Java

Next – let’s get to the actual Spring configuration. We’ll start by setting up 2 configuration classes – one for the test and the other for the jack.

In each one of this configuration classes, we’ll need to define the following interfaces:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages= {"com.jack.jackdawson.dao.test"}, //设置Repository所在位置
        entityManagerFactoryRef="testEntityManager",
        transactionManagerRef="testTransactionManager"
)
public class TestEntityConfig {

    @Resource(name = "myDataSource1")
    private DataSource primaryDataSource;

    @Bean("testEntityManager")
    @Primary
    public LocalContainerEntityManagerFactoryBean testEntityManager() {
        LocalContainerEntityManagerFactoryBean em
                = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(primaryDataSource);
        em.setPackagesToScan(
                new String[] { "com.jack.jackdawson.entity.test" });

        HibernateJpaVendorAdapter vendorAdapter
                = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<>();
//        properties.put("hibernate.hbm2ddl.auto",
//                env.getProperty("hibernate.hbm2ddl.auto"));
//        properties.put("hibernate.dialect",
//                env.getProperty("hibernate.dialect"));
        em.setJpaPropertyMap(properties);

        return em;
    }

    @Bean("testTransactionManager")
    @Primary
    public PlatformTransactionManager testTransactionManager() {

        JpaTransactionManager transactionManager
                = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(
                testEntityManager().getObject());
        return transactionManager;
    }

}
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        basePackages= { "com.jack.jackdawson.dao.jack" },//设置Repository所在位置
        entityManagerFactoryRef="jackEntityManager",
        transactionManagerRef="jackTransactionManager")
public class JackEntityConfig {

    @Resource(name = "myDataSource2")
    private DataSource primaryDataSource;


    @Bean("jackEntityManager")
    @Primary
    public LocalContainerEntityManagerFactoryBean jackEntityManager() {
        LocalContainerEntityManagerFactoryBean em
                = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(primaryDataSource);
        em.setPackagesToScan(
                new String[] { "com.jack.jackdawson.entity.jack" });

        HibernateJpaVendorAdapter vendorAdapter
                = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<>();
//        properties.put("hibernate.hbm2ddl.auto",
//                env.getProperty("hibernate.hbm2ddl.auto"));
//        properties.put("hibernate.dialect",
//                env.getProperty("hibernate.dialect"));
        em.setJpaPropertyMap(properties);

        return em;
    }

    @Bean("jackTransactionManager")
    @Primary
    public PlatformTransactionManager testTransactionManager() {

        JpaTransactionManager transactionManager
                = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(
                jackEntityManager().getObject());
        return transactionManager;
    }

}
上一篇下一篇

猜你喜欢

热点阅读