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);
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器