|
@@ -0,0 +1,65 @@
|
|
|
+package com.tzld.longarticle.recommend.server.config.db;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
+import org.springframework.boot.jdbc.DataSourceBuilder;
|
|
|
+import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
|
|
+import org.springframework.orm.jpa.JpaTransactionManager;
|
|
|
+import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
|
|
+import org.springframework.transaction.PlatformTransactionManager;
|
|
|
+import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
+
|
|
|
+import javax.persistence.EntityManagerFactory;
|
|
|
+import javax.sql.DataSource;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Configuration
|
|
|
+@EnableTransactionManagement
|
|
|
+@EnableJpaRepositories(
|
|
|
+ basePackages = "com.tzld.longarticle.recommend.server.repository.aigc",
|
|
|
+ entityManagerFactoryRef = "aigcEntityManagerFactory",
|
|
|
+ transactionManagerRef = "aigcTransactionManager"
|
|
|
+)
|
|
|
+public class AigcDBConfig {
|
|
|
+
|
|
|
+ @Value("${spring.jpa.aigc.hibernate.ddl-auto}")
|
|
|
+ private String ddlAuto;
|
|
|
+ @Value("${spring.jpa.aigc.database}")
|
|
|
+ private String database;
|
|
|
+
|
|
|
+ public Map<String, Object> hibernateProperties() {
|
|
|
+ Map<String, Object> properties = new HashMap<>();
|
|
|
+ properties.put("hibernate.ddl-auto", ddlAuto);
|
|
|
+ properties.put("database", database);
|
|
|
+ return properties;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = "aigcDataSource")
|
|
|
+ @ConfigurationProperties(prefix = "spring.datasource.aigc")
|
|
|
+ public DataSource aigcDataSource() {
|
|
|
+ return DataSourceBuilder.create().build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = "aigcEntityManagerFactory")
|
|
|
+ public LocalContainerEntityManagerFactoryBean aigcEntityManagerFactory(EntityManagerFactoryBuilder builder,
|
|
|
+ @Qualifier("aigcDataSource") DataSource dataSource) {
|
|
|
+ return builder
|
|
|
+ .dataSource(dataSource)
|
|
|
+ .packages("com.tzld.longarticle.recommend.server.repository.entity.aigc") // 实体类包路径
|
|
|
+ .persistenceUnit("aigc")
|
|
|
+ .properties(hibernateProperties())
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = "aigcTransactionManager")
|
|
|
+ public PlatformTransactionManager aigcTransactionManager(
|
|
|
+ @Qualifier("aigcEntityManagerFactory") EntityManagerFactory aigcEntityManagerFactory) {
|
|
|
+ return new JpaTransactionManager(aigcEntityManagerFactory);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|