Spring Boot, Spring Data JPA mit mehreren Datenquellen
In Spring Boot ermöglicht die Verwendung mehrerer Datenquellen die Verbindung verschiedener Repositorys zu separaten Datenquellen . Um dies zu erreichen, verwenden wir Annotationen und Konfigurationen, um die Datenquelle und den Transaktionsmanager für jedes Repository anzugeben.
Konfiguration
CustomerDataSourceConfiguration.java ( Erste Datenquelle)
<code class="java">@Configuration @EnableJpaRepositories( entityManagerFactoryRef = "customerEntityManager", transactionManagerRef = "customerTransactionManager", basePackages = {"com.mm.repository.customer"}) public class CustomerDataSourceConfiguration { @Bean(name = "customerDataSource") public DataSource dataSource() { return DataSourceBuilder.create() .url("jdbc:h2:mem:customer:H2") .driverClassName("org.h2.Driver") .username("sa") .password("") .build(); } @Bean(name = "customerEntityManager") public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); em.setPackagesToScan(new String[] {"com.mm.domain.customer"}); JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); em.setJpaProperties(additionalJpaProperties()); em.setPersistenceUnitName("customerPersistence"); em.setPackagesToScan("com.mm.domain.customer"); return em; } Properties additionalJpaProperties() { Properties properties = new Properties(); properties.setProperty("hibernate.hbm2ddl.auto", "create-drop"); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); properties.setProperty("hibernate.show_sql", "true"); return properties; } @Bean(name = "customerTransactionManager") public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(emf); return transactionManager; } }</code>
GenericDataSourceConfiguration.java (Zweite Datenquelle)
<code class="java">@Configuration @EnableJpaRepositories( entityManagerFactoryRef = "genericEntityManager", transactionManagerRef = "genericTransactionManager", basePackages = {"com.mm.repository.generic"}) public class GenericDataSourceConfiguration { @Bean(name = "genericDataSource") public DataSource dataSource() { return DataSourceBuilder.create() .url("jdbc:h2:mem:generic:H2") .driverClassName("org.h2.Driver") .username("sa") .password("") .build(); } @Bean(name = "genericEntityManager") public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); em.setPackagesToScan(new String[] {"com.mm.domain.generic"}); JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); em.setJpaProperties(additionalJpaProperties()); em.setPersistenceUnitName("genericPersistence"); em.setPackagesToScan("com.mm.domain.generic"); return em; } Properties additionalJpaProperties() { Properties properties = new Properties(); properties.setProperty("hibernate.hbm2ddl.auto", "create-drop"); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); properties.setProperty("hibernate.show_sql", "true"); return properties; } @Bean(name = "genericTransactionManager") public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(emf); return transactionManager; } }</code>
Application.java
<code class="java">@Configuration @ComponentScan @EnableAutoConfiguration public class Application extends SpringApplication { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean public ServletRegistrationBean h2Console() { ServletRegistrationBean reg = new ServletRegistrationBean(new WebServlet(), "/console/*"); reg.setLoadOnStartup(1); return reg; } }</code>
Entitäten und Repositorys
Customer.java (Entität)
<code class="java">@Entity @Table(name = "customer") @Data @EqualsAndHashCode(exclude = {"id"}) public class Customer { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name = "name", nullable = false) private String name; @Column(name = "age", nullable = false) private Integer age; .... }</code>
Order.java ( Entität)
<code class="java">@Entity @Table(name = "order") @Data @EqualsAndHashCode(exclude = {"id"}) public class Order { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Column(name = "code", nullable = false) private Integer code; @Column(name = "quality", nullable = false) private Integer quality; }</code>
CustomerRepository.java (Repository für erste Datenquelle)
<code class="java">public interface CustomerRepository extends JpaRepository<Customer, Integer> {}</code>
GenericRepository.java (Repository für zweite Daten). Quelle)
<code class="java">public interface GenericRepository extends JpaRepository<Generic, Integer> {}</code>
Mit diesem Ansatz erstellt Spring Data JPA separate EntityManagerFactories und TransactionManagers für jede Datenquelle und ermöglicht so eine nahtlose Integration mit mehreren Datenbanken in einer Spring Boot-Anwendung.
Das obige ist der detaillierte Inhalt vonWie konfigurieren Sie Spring Data JPA für die Verwendung mehrerer Datenquellen in einer Spring Boot-Anwendung?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!