Spring Boot, Spring Data JPA dengan Multiple DataSources
Dalam Spring Boot, menggunakan berbilang sumber data membenarkan penyambungan repositori berbeza untuk memisahkan sumber data . Untuk mencapai matlamat ini, kami menggunakan anotasi dan konfigurasi untuk menentukan sumber data dan pengurus transaksi bagi setiap repositori.
Konfigurasi
CustomerDataSourceConfiguration.java ( Sumber Data Pertama)
<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 (Sumber Data Kedua)
<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>
Entiti dan Repositori
Customer.java (Entiti)
<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 ( Entiti)
<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 (Repositori untuk Sumber Data Pertama)
<code class="java">public interface CustomerRepository extends JpaRepository<Customer, Integer> {}</code>
GenericRepository.java (Repositori untuk Data Kedua Sumber)
<code class="java">public interface GenericRepository extends JpaRepository<Generic, Integer> {}</code>
Dengan menggunakan pendekatan ini, Spring Data JPA akan mencipta EntityManagerFactories dan TransactionManagers yang berasingan untuk setiap sumber data, membolehkan penyepaduan yang lancar dengan berbilang pangkalan data dalam aplikasi Spring Boot.
Atas ialah kandungan terperinci Bagaimanakah anda mengkonfigurasi Spring Data JPA untuk menggunakan berbilang sumber data dalam aplikasi Spring Boot?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!