├── README.md ├── src ├── main │ ├── java │ │ └── me │ │ │ └── wonwoo │ │ │ ├── user │ │ │ ├── repository │ │ │ │ └── UserRepository.java │ │ │ ├── domain │ │ │ │ └── User.java │ │ │ └── config │ │ │ │ └── UserDataSourceConfig.java │ │ │ ├── customer │ │ │ ├── repository │ │ │ │ └── CustomerRepository.java │ │ │ ├── domain │ │ │ │ └── Customer.java │ │ │ └── config │ │ │ │ └── CustomerDataSourceConfig.java │ │ │ └── Application.java │ └── resources │ │ └── application.yml └── test │ └── java │ └── me │ └── wonwoo │ └── ApplicationTest.java ├── .gitignore └── pom.xml /README.md: -------------------------------------------------------------------------------- 1 | # spring-boot-jpa-multiple-datasource 2 | 3 | spring-boot-jpa-multiple-datasource sample 4 | -------------------------------------------------------------------------------- /src/main/java/me/wonwoo/user/repository/UserRepository.java: -------------------------------------------------------------------------------- 1 | package me.wonwoo.user.repository; 2 | 3 | import me.wonwoo.user.domain.User; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * Created by wonwoo on 2016. 5. 4.. 8 | */ 9 | public interface UserRepository extends JpaRepository { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/me/wonwoo/customer/repository/CustomerRepository.java: -------------------------------------------------------------------------------- 1 | package me.wonwoo.customer.repository; 2 | 3 | import me.wonwoo.customer.domain.Customer; 4 | import org.springframework.data.jpa.repository.JpaRepository; 5 | 6 | /** 7 | * Created by wonwoo on 2016. 5. 4.. 8 | */ 9 | public interface CustomerRepository extends JpaRepository { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | jpa: 3 | show-sql: true 4 | generate-ddl: true 5 | properties: 6 | hibernate: 7 | ddl-auto: validate 8 | hbm2ddl: 9 | import_files: 10 | 11 | user: 12 | datasource: 13 | url: jdbc:h2:tcp://localhost/~/user 14 | username: sa 15 | password: 16 | driverClassName: org.h2.Driver 17 | 18 | customer: 19 | datasource: 20 | url: jdbc:h2:tcp://localhost/~/customer 21 | username: sa 22 | password: 23 | driverClassName: org.h2.Driver -------------------------------------------------------------------------------- /src/main/java/me/wonwoo/user/domain/User.java: -------------------------------------------------------------------------------- 1 | package me.wonwoo.user.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import javax.persistence.Entity; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.Id; 10 | 11 | /** 12 | * Created by wonwoo on 2016. 5. 4.. 13 | */ 14 | @Entity 15 | @Data 16 | @AllArgsConstructor 17 | @NoArgsConstructor 18 | public class User { 19 | 20 | public User(String username){ 21 | this.username = username; 22 | } 23 | 24 | @Id 25 | @GeneratedValue 26 | private Long id; 27 | 28 | private String username; 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/me/wonwoo/customer/domain/Customer.java: -------------------------------------------------------------------------------- 1 | package me.wonwoo.customer.domain; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | import javax.persistence.Entity; 8 | import javax.persistence.GeneratedValue; 9 | import javax.persistence.Id; 10 | 11 | /** 12 | * Created by wonwoo on 2016. 5. 4.. 13 | */ 14 | 15 | @Entity 16 | @Data 17 | @AllArgsConstructor 18 | @NoArgsConstructor 19 | public class Customer { 20 | 21 | @Id 22 | @GeneratedValue 23 | private Long id; 24 | 25 | private String customerName; 26 | 27 | public Customer(String customerName) { 28 | this.customerName = customerName; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/me/wonwoo/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package me.wonwoo; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import me.wonwoo.customer.repository.CustomerRepository; 5 | import me.wonwoo.user.repository.UserRepository; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.junit4.SpringRunner; 11 | 12 | @RunWith(SpringRunner.class) 13 | @SpringBootTest 14 | @Slf4j 15 | public class ApplicationTest { 16 | 17 | @Autowired 18 | private UserRepository userRepository; 19 | 20 | @Autowired 21 | private CustomerRepository customerRepository; 22 | 23 | @Test 24 | public void userTest() { 25 | userRepository.findAll() 26 | .stream() 27 | .map(i -> i.toString()) 28 | .forEach(log::info); 29 | } 30 | 31 | @Test 32 | public void custoemrTest(){ 33 | customerRepository.findAll() 34 | .stream() 35 | .map(i -> i.toString()) 36 | .forEach(log::info); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/me/wonwoo/Application.java: -------------------------------------------------------------------------------- 1 | package me.wonwoo; 2 | 3 | import me.wonwoo.customer.domain.Customer; 4 | import me.wonwoo.customer.repository.CustomerRepository; 5 | import me.wonwoo.user.domain.User; 6 | import me.wonwoo.user.repository.UserRepository; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.CommandLineRunner; 9 | import org.springframework.boot.SpringApplication; 10 | import org.springframework.boot.autoconfigure.SpringBootApplication; 11 | import org.springframework.context.annotation.Bean; 12 | 13 | import java.util.Arrays; 14 | 15 | @SpringBootApplication 16 | public class Application { 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(Application.class, args); 20 | } 21 | 22 | @Autowired 23 | private UserRepository userRepository; 24 | 25 | @Autowired 26 | private CustomerRepository customerRepository; 27 | 28 | // @Bean 29 | // public CommandLineRunner commandLineRunner(){ 30 | // return args ->{ 31 | // Arrays.asList(new User("wonwoo"),new User("kevin")) 32 | // .forEach(userRepository::save); 33 | // Arrays.asList(new Customer("customerName"), new Customer("test")) 34 | // .forEach(customerRepository::save); 35 | // }; 36 | // } 37 | } 38 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/int,intellij,java 3 | 4 | #!! ERROR: int is undefined. Use list command to see defined gitignore types !!# 5 | 6 | ### Intellij ### 7 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 8 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 9 | 10 | # User-specific stuff: 11 | .idea/workspace.xml 12 | .idea/tasks.xml 13 | .idea/dictionaries 14 | .idea/vcs.xml 15 | .idea/jsLibraryMappings.xml 16 | 17 | # Sensitive or high-churn files: 18 | .idea/dataSources.ids 19 | .idea/dataSources.xml 20 | .idea/dataSources.local.xml 21 | .idea/sqlDataSources.xml 22 | .idea/dynamic.xml 23 | .idea/uiDesigner.xml 24 | 25 | # Gradle: 26 | .idea/gradle.xml 27 | .idea/libraries 28 | 29 | # Mongo Explorer plugin: 30 | .idea/mongoSettings.xml 31 | 32 | ## File-based project format: 33 | *.iws 34 | 35 | ## Plugin-specific files: 36 | 37 | # IntelliJ 38 | /out/ 39 | 40 | # mpeltonen/sbt-idea plugin 41 | .idea_modules/ 42 | 43 | # JIRA plugin 44 | atlassian-ide-plugin.xml 45 | 46 | # Crashlytics plugin (for Android Studio and IntelliJ) 47 | com_crashlytics_export_strings.xml 48 | crashlytics.properties 49 | crashlytics-build.properties 50 | fabric.properties 51 | 52 | ### Intellij Patch ### 53 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 54 | 55 | # *.iml 56 | # modules.xml 57 | 58 | 59 | ### Java ### 60 | *.class 61 | 62 | # Mobile Tools for Java (J2ME) 63 | .mtj.tmp/ 64 | 65 | # Package Files # 66 | *.jar 67 | *.war 68 | *.ear 69 | 70 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 71 | hs_err_pid* 72 | -------------------------------------------------------------------------------- /src/main/java/me/wonwoo/customer/config/CustomerDataSourceConfig.java: -------------------------------------------------------------------------------- 1 | package me.wonwoo.customer.config; 2 | 3 | import org.springframework.beans.factory.annotation.Qualifier; 4 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.context.annotation.Primary; 10 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 11 | import org.springframework.orm.jpa.JpaTransactionManager; 12 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 13 | import org.springframework.transaction.PlatformTransactionManager; 14 | import org.springframework.transaction.annotation.EnableTransactionManagement; 15 | 16 | import javax.persistence.EntityManagerFactory; 17 | import javax.sql.DataSource; 18 | 19 | /** 20 | * Created by wonwoo on 2016. 5. 4.. 21 | */ 22 | @Configuration 23 | @EnableTransactionManagement 24 | @EnableJpaRepositories( 25 | basePackages = {"me.wonwoo.customer.repository"}) 26 | public class CustomerDataSourceConfig { 27 | 28 | @Primary 29 | @Bean(name = "dataSource") 30 | @ConfigurationProperties(prefix = "customer.datasource") 31 | public DataSource dataSource() { 32 | return DataSourceBuilder.create().build(); 33 | } 34 | 35 | @Primary 36 | @Bean(name = "entityManagerFactory") 37 | public LocalContainerEntityManagerFactoryBean entityManagerFactory( 38 | EntityManagerFactoryBuilder builder,DataSource dataSource) { 39 | return builder 40 | .dataSource(dataSource) 41 | .packages("me.wonwoo.customer.domain") 42 | .persistenceUnit("customer") 43 | .build(); 44 | } 45 | 46 | @Primary 47 | @Bean(name = "transactionManager") 48 | public PlatformTransactionManager transactionManager( 49 | EntityManagerFactory entityManagerFactory) { 50 | return new JpaTransactionManager(entityManagerFactory); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/me/wonwoo/user/config/UserDataSourceConfig.java: -------------------------------------------------------------------------------- 1 | package me.wonwoo.user.config; 2 | 3 | import org.springframework.beans.factory.annotation.Qualifier; 4 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 5 | import org.springframework.boot.context.properties.ConfigurationProperties; 6 | import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 10 | import org.springframework.orm.jpa.JpaTransactionManager; 11 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 12 | import org.springframework.transaction.PlatformTransactionManager; 13 | import org.springframework.transaction.annotation.EnableTransactionManagement; 14 | 15 | import javax.persistence.EntityManagerFactory; 16 | import javax.sql.DataSource; 17 | 18 | /** 19 | * Created by wonwoo on 2016. 5. 4.. 20 | */ 21 | @Configuration 22 | @EnableTransactionManagement 23 | @EnableJpaRepositories( 24 | entityManagerFactoryRef = "userEntityManagerFactory", 25 | transactionManagerRef = "userTransactionManager", 26 | basePackages = {"me.wonwoo.user.repository"}) 27 | public class UserDataSourceConfig { 28 | 29 | @Bean(name = "userDataSource") 30 | @ConfigurationProperties(prefix = "user.datasource") 31 | public DataSource userDataSource() { 32 | return DataSourceBuilder.create().build(); 33 | } 34 | 35 | @Bean(name = "userEntityManagerFactory") 36 | public LocalContainerEntityManagerFactoryBean userEntityManagerFactory( 37 | EntityManagerFactoryBuilder builder, @Qualifier("userDataSource") DataSource userDataSource) { 38 | return builder 39 | .dataSource(userDataSource) 40 | .packages("me.wonwoo.user.domain") 41 | .persistenceUnit("user") 42 | .build(); 43 | } 44 | 45 | @Bean(name = "userTransactionManager") 46 | public PlatformTransactionManager userTransactionManager( 47 | @Qualifier("userEntityManagerFactory") EntityManagerFactory userEntityManagerFactory) { 48 | return new JpaTransactionManager(userEntityManagerFactory); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | me.wonwoo 7 | spring-boot-jpa-multiple-datasource 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | spring-boot-jpa-multiple-datasource 12 | Demo project for Spring Boot 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.4.0.M2 18 | 19 | 20 | 21 | 22 | UTF-8 23 | 1.8 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter-data-jpa 30 | 31 | 32 | 33 | org.projectlombok 34 | lombok 35 | 36 | 37 | 38 | com.h2database 39 | h2 40 | runtime 41 | 42 | 43 | org.springframework.boot 44 | spring-boot-starter-test 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.springframework.boot 53 | spring-boot-maven-plugin 54 | 55 | 56 | 57 | 58 | 59 | 60 | spring-snapshots 61 | Spring Snapshots 62 | https://repo.spring.io/snapshot 63 | 64 | true 65 | 66 | 67 | 68 | spring-milestones 69 | Spring Milestones 70 | https://repo.spring.io/milestone 71 | 72 | false 73 | 74 | 75 | 76 | 77 | 78 | spring-snapshots 79 | Spring Snapshots 80 | https://repo.spring.io/snapshot 81 | 82 | true 83 | 84 | 85 | 86 | spring-milestones 87 | Spring Milestones 88 | https://repo.spring.io/milestone 89 | 90 | false 91 | 92 | 93 | 94 | 95 | 96 | --------------------------------------------------------------------------------