zoukankan      html  css  js  c++  java
  • Springboot-Jpa多数据库配置-2.0+版本

    pom.xml增加:

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    配置表同JdbcTemplate配置.

    主数据源:

    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary",
    transactionManagerRef = "transactionManagerPrimary",
    basePackages = {"com.example.demo.p"})
    public class PrimaryConfig {
    @Bean(name = "primaryDataSource")
    @Primary
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.primary")
    public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
    }

    @Autowired
    @Qualifier("primaryDataSource")
    private DataSource primaryDatasource;

    //@Primary
    //@Bean(name = "entityManagerPrimary")
    //public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    // return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    //}

    //@Autowired
    //private JpaProperties jpaProperties;

    //private Map<String, Object> getVendorProperties() {
    // return jpaProperties.getHibernateProperties(new HibernateSettings());//与1.5版本不同,注意.
    //}

    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary(EntityManagerFactoryBuilder builder) {
    return builder.
    dataSource(primaryDatasource)
    //.properties(getVendorProperties())
    .packages("com.example.demo.p")
    .persistenceUnit("primaryPersistenceUnit")
    .build();
    }

    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }
    }

    主Entity:

    @Entity
    @Table(name = "xx")
    public class Dtl {
    @Id
    @Column(name = "id")
    private Long id;

    @Column(name = "TICKER_SYMBOL")
    private String tickerSymbol;

    public Dtl() {}

    public Dtl(String tickerSymbol) {
    this.tickerSymbol = tickerSymbol;
    }

    public Long getId() {
    return id;
    }

    public String getTickerSymbol() {
    return tickerSymbol;
    }

    public void setTickerSymbol(String tickerSymbol) {
    this.tickerSymbol = tickerSymbol;
    }
    }

    主Repository:

    public interface GetDtlP extends JpaRepository<Dtl, Long> {
    List<Dtl> findByTickerSymbol(String tickerSymbol);
    }

    次数据源:

    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactorySecondary",
    transactionManagerRef = "transactionManagerSecondary",
    basePackages = {"com.example.demo.s"})
    public class SecondaryConfig {
    @Bean(name = "secondaryDataSource")
    @Qualifier("secondaryDataSource")
    @ConfigurationProperties(prefix="spring.datasource.secondary")
    public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
    }

    @Autowired
    @Qualifier("secondaryDataSource")
    private DataSource secondaryDataSource;

    @Autowired
    private JpaProperties jpaProperties;

    private Map<String, Object> getVendorProperties() {
    return jpaProperties.getHibernateProperties(new HibernateSettings());
    }

    @Bean(name = "entityManagerFactorySecondary")
    public LocalContainerEntityManagerFactoryBean entityManagerFactorySecondary(EntityManagerFactoryBuilder builder) {
    return builder.
    dataSource(secondaryDataSource)
    .properties(getVendorProperties())
    .packages("com.example.demo.s")
    .persistenceUnit("SecondaryPersistenceUnit")
    .build();
    }

    @Bean(name = "entityManagerSecondary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    return entityManagerFactorySecondary(builder).getObject().createEntityManager();
    }

    @Bean(name = "transactionManagerSecondary")
    public PlatformTransactionManager transactionManagerSecondary(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(entityManagerFactorySecondary(builder).getObject());
    }
    }

    次Entity:

    @Entity
    @Table(name = "xx")
    public class Dtl {
    @Id
    @Column(name = "id")
    private Long id;

    @Column(name = "TICKER_SYMBOL")
    private String tickerSymbol;

    public Dtl() {}

    public Dtl(String tickerSymbol) {
    this.tickerSymbol = tickerSymbol;
    }

    public Long getId() {
    return id;
    }

    public String getTickerSymbol() {
    return tickerSymbol;
    }

    public void setTickerSymbol(String tickerSymbol) {
    this.tickerSymbol = tickerSymbol;
    }
    }

    次Repository:

    public interface GetDtlS extends JpaRepository<Dtl, Long> {
    List<Dtl> findByTickerSymbol(String tickerSymbol);
    }

    测试:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class JpaTest {

    @Autowired
    protected GetDtlP getDtlP;

    @Autowired
    protected GetDtlS getDtlS;

    @Test
    public void test() throws Exception {
    List a = getDtlP.findByTickerSymbol("3");
    List b = getDtlS.findByTickerSymbol("3");
    Assert.assertEquals(a, b);
    }
    }
  • 相关阅读:
    【LeetCode】205. Isomorphic Strings
    Syscall param open(filename) points to unaddressable byte(s)
    五种主要多核并行编程方法分析与比较
    计算机时间复杂度和空间复杂度
    CUDA学习笔记(二)【转】
    CUDA学习笔记(一)【转】
    CUDA Thread Indexing
    Intel MKL函数,如何得到相同的计算结果?【转】
    CUDA编程
    GPU(CUDA)学习日记(十一)------ 深入理解CUDA线程层次以及关于设置线程数的思考
  • 原文地址:https://www.cnblogs.com/ylpb/p/9209802.html
Copyright © 2011-2022 走看看