Spring
jpa
java
postgresql
#第一個 DB
spring.datasource.jdbc-url=DB_HOST
spring.datasource.username=Username
spring.datasource.password=Password
spring.datasource.driverClassName=org.postgresql.Driver
#第二個 DB
dbto.datasource.jdbc-url=DB_HOST
dbto.datasource.username=Username
dbto.datasource.password=Password
dbto.datasource.driverClassName=org.postgresql.Driver
package com.zygroup.ilnd.config;
import org.springframework.beans.factory.annotation.Qualifier;
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.context.annotation.Primary;
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;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "customerEntityManagerFactory",//對映(2)
transactionManagerRef = "customerTransactionManager",//對映(3)
basePackages = {
"com.zygroup.ilnd.model.repository" //Repository所在路徑
}
)
public class CustomerConfig {
@Primary
@Bean(name = "customerDataSource") //(1)自訂義定義 DataSource 名稱
@ConfigurationProperties(prefix = "spring.datasource")//application.properties設定的資料庫連線前綴
public DataSource customerDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "customerEntityManagerFactory") //(2)自訂義定義EntityManagerFactory名稱
public LocalContainerEntityManagerFactoryBean
entityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("customerDataSource") DataSource dataSource
) {
return builder
.dataSource(dataSource)
.packages("com.zygroup.ilnd.model")//Model所在路徑
.persistenceUnit("db1")
.build();
}
@Primary
@Bean(name = "customerTransactionManager") //(3)自訂義定義 TransactionManager 名稱
public PlatformTransactionManager customerTransactionManager(
@Qualifier("customerEntityManagerFactory") EntityManagerFactory customerEntityManagerFactory //對映(2)
) {
return new JpaTransactionManager(customerEntityManagerFactory);//對映(2)
}
}
package com.zygroup.ilnd.config;
import org.springframework.beans.factory.annotation.Qualifier;
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;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "productEntityManagerFactory",//對映(2)
transactionManagerRef = "productTransactionManager",//對映(3)
basePackages = {
"com.zygroup.ilnd.modelTo.repository"//Repository所在路徑
}
)
public class ProductConfig {
@Bean(name = "productDataSource")//(1)自訂義定義 DataSource 名稱
@ConfigurationProperties(prefix = "dbto.datasource")//application.properties設定的資料庫連線前綴
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "productEntityManagerFactory")//(2)自訂義定義
public LocalContainerEntityManagerFactoryBean
barEntityManagerFactory(
EntityManagerFactoryBuilder builder,
@Qualifier("productDataSource") DataSource dataSource
) {
return
builder
.dataSource(dataSource)
.packages("com.zygroup.ilnd.modelTo")//Model所在路徑
.persistenceUnit("db2")
.build();
}
@Bean(name = "productTransactionManager")//(3)自訂義定義
public PlatformTransactionManager productTransactionManager(
@Qualifier("productEntityManagerFactory") EntityManagerFactory productEntityManagerFactory //對映(2)
) {
return new JpaTransactionManager(productEntityManagerFactory);//對映(2)
}
}
https://www.baeldung.com/spring-data-jpa-multiple-databases
https://www.javadevjournal.com/spring-boot/multiple-data-sources-with-spring-boot/
一、建立檔案 .gitignore //不上傳檔案 二、指令 git --version //檢查版本 git init //初始化git git add [--all] //加入檔案 git add -f [filename] //強制加入檔案 git commit -m "mag" //建立儲存檔 git config user.name "name" //使用者名稱
Feb 22, 2021SQL MongoDB 說明 database database 資料庫 table collection
Oct 7, 2020/pom.xml <!-- https://github.com/google/guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>28.1-jre</version> </dependency> Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, hashing, caching, primitives, strings, and more! It is widely used on most Java projects within Google, and widely used by many other companies as well. https://github.com/google/guava
Mar 5, 2020{home_url}/.m2/settings.xml <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <activeProfiles> <activeProfile>github</activeProfile> </activeProfiles>
Feb 26, 2020or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up