Jpa多源数据配置

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());
}

@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);
}
}

猜你喜欢

转载自www.cnblogs.com/ylpb/p/9209802.html