├── README.md ├── pom.xml └── src ├── main ├── java │ └── net │ │ └── guides │ │ └── springboot │ │ └── springbootmultipledatasources │ │ ├── SpringbootMultipleDatasourcesApplication.java │ │ ├── config │ │ ├── OrdersDataSourceConfig.java │ │ ├── SecurityDataSourceConfig.java │ │ └── WebMvcConfig.java │ │ ├── controllers │ │ └── HomeController.java │ │ ├── orders │ │ ├── entities │ │ │ ├── Order.java │ │ │ └── OrderItem.java │ │ └── repositories │ │ │ └── OrderRepository.java │ │ ├── security │ │ ├── entities │ │ │ ├── Address.java │ │ │ └── User.java │ │ └── repositories │ │ │ └── UserRepository.java │ │ └── services │ │ └── UserOrdersService.java └── resources │ ├── application-prod.properties │ ├── application.properties │ ├── orders-data.sql │ ├── security-data.sql │ └── templates │ └── users.html └── test └── java └── net └── guides └── springboot └── springbootmultipledatasources └── SpringbootMultipleDatasourcesApplicationTests.java /README.md: -------------------------------------------------------------------------------- 1 | # springboot-jpa-multiple-datasources 2 | Spring Boot JPA Multiple Data Sources Example 3 | 4 | Article of this example - http://www.javaguides.net/2018/09/spring-boot-jpa-multiple-data-sources-example.html 5 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | net.guides.springboot 8 | springboot-multiple-datasources 9 | 0.0.1-SNAPSHOT 10 | jar 11 | 12 | springboot-multiple-datasources 13 | Demo project for Spring Boot 14 | 15 | 16 | org.springframework.boot 17 | spring-boot-starter-parent 18 | 3.0.4 19 | 20 | 21 | 22 | 23 | UTF-8 24 | UTF-8 25 | 17 26 | 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-test 32 | test 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-devtools 37 | true 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-configuration-processor 42 | true 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-starter-data-jpa 47 | 48 | 49 | org.springframework.boot 50 | spring-boot-starter-web 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-starter-thymeleaf 55 | 56 | 57 | com.mysql 58 | mysql-connector-j 59 | 60 | 61 | com.h2database 62 | h2 63 | 64 | 65 | 66 | 67 | 68 | 69 | org.springframework.boot 70 | spring-boot-maven-plugin 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /src/main/java/net/guides/springboot/springbootmultipledatasources/SpringbootMultipleDatasourcesApplication.java: -------------------------------------------------------------------------------- 1 | package net.guides.springboot.springbootmultipledatasources; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 7 | import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration; 8 | import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; 9 | import org.springframework.transaction.annotation.EnableTransactionManagement; 10 | 11 | @SpringBootApplication( 12 | exclude = { DataSourceAutoConfiguration.class, 13 | HibernateJpaAutoConfiguration.class, 14 | DataSourceTransactionManagerAutoConfiguration.class }) 15 | @EnableTransactionManagement 16 | public class SpringbootMultipleDatasourcesApplication { 17 | 18 | public static void main(String[] args) { 19 | SpringApplication.run(SpringbootMultipleDatasourcesApplication.class, args); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/net/guides/springboot/springbootmultipledatasources/config/OrdersDataSourceConfig.java: -------------------------------------------------------------------------------- 1 | package net.guides.springboot.springbootmultipledatasources.config; 2 | 3 | import java.util.Properties; 4 | 5 | import jakarta.persistence.EntityManagerFactory; 6 | import javax.sql.DataSource; 7 | 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; 10 | import org.springframework.boot.context.properties.ConfigurationProperties; 11 | import org.springframework.boot.jdbc.DataSourceBuilder; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.core.env.Environment; 15 | import org.springframework.core.io.ClassPathResource; 16 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 17 | import org.springframework.jdbc.datasource.init.DataSourceInitializer; 18 | import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; 19 | import org.springframework.orm.jpa.JpaTransactionManager; 20 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 21 | import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; 22 | import org.springframework.transaction.PlatformTransactionManager; 23 | 24 | /** 25 | * @author Ramesh Fadatare 26 | * 27 | */ 28 | @Configuration 29 | @EnableJpaRepositories( 30 | basePackages = "net.guides.springboot.springbootmultipledatasources.orders.repositories", 31 | entityManagerFactoryRef = "ordersEntityManagerFactory", 32 | transactionManagerRef = "ordersTransactionManager" 33 | ) 34 | public class OrdersDataSourceConfig { 35 | 36 | @Autowired 37 | private Environment env; 38 | 39 | @Bean 40 | @ConfigurationProperties(prefix="datasource.orders") 41 | public DataSourceProperties ordersDataSourceProperties() { 42 | return new DataSourceProperties(); 43 | } 44 | 45 | @Bean 46 | public DataSource ordersDataSource() { 47 | DataSourceProperties primaryDataSourceProperties = ordersDataSourceProperties(); 48 | return DataSourceBuilder.create() 49 | .driverClassName(primaryDataSourceProperties.getDriverClassName()) 50 | .url(primaryDataSourceProperties.getUrl()) 51 | .username(primaryDataSourceProperties.getUsername()) 52 | .password(primaryDataSourceProperties.getPassword()) 53 | .build(); 54 | } 55 | 56 | @Bean 57 | public PlatformTransactionManager ordersTransactionManager() 58 | { 59 | EntityManagerFactory factory = ordersEntityManagerFactory().getObject(); 60 | return new JpaTransactionManager(factory); 61 | } 62 | 63 | @Bean 64 | public LocalContainerEntityManagerFactoryBean ordersEntityManagerFactory() 65 | { 66 | LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); 67 | factory.setDataSource(ordersDataSource()); 68 | factory.setPackagesToScan(new String[]{"net.guides.springboot.springbootmultipledatasources.orders.entities"}); 69 | factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); 70 | 71 | Properties jpaProperties = new Properties(); 72 | jpaProperties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto")); 73 | jpaProperties.put("hibernate.show-sql", env.getProperty("spring.jpa.show-sql")); 74 | factory.setJpaProperties(jpaProperties); 75 | 76 | return factory; 77 | 78 | } 79 | 80 | @Bean 81 | public DataSourceInitializer ordersDataSourceInitializer() 82 | { 83 | DataSourceInitializer dataSourceInitializer = new DataSourceInitializer(); 84 | dataSourceInitializer.setDataSource(ordersDataSource()); 85 | ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); 86 | databasePopulator.addScript(new ClassPathResource("orders-data.sql")); 87 | dataSourceInitializer.setDatabasePopulator(databasePopulator); 88 | dataSourceInitializer.setEnabled(env.getProperty("datasource.orders.initialize", Boolean.class, false)); 89 | return dataSourceInitializer; 90 | } 91 | 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/net/guides/springboot/springbootmultipledatasources/config/SecurityDataSourceConfig.java: -------------------------------------------------------------------------------- 1 | package net.guides.springboot.springbootmultipledatasources.config; 2 | 3 | import java.util.Properties; 4 | 5 | import jakarta.persistence.EntityManagerFactory; 6 | import javax.sql.DataSource; 7 | 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; 10 | import org.springframework.boot.context.properties.ConfigurationProperties; 11 | import org.springframework.boot.jdbc.DataSourceBuilder; 12 | import org.springframework.context.annotation.Bean; 13 | import org.springframework.context.annotation.Configuration; 14 | import org.springframework.core.env.Environment; 15 | import org.springframework.core.io.ClassPathResource; 16 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 17 | import org.springframework.jdbc.datasource.init.DataSourceInitializer; 18 | import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; 19 | import org.springframework.orm.jpa.JpaTransactionManager; 20 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 21 | import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; 22 | import org.springframework.transaction.PlatformTransactionManager; 23 | 24 | /** 25 | * @author Ramesh Fadatare 26 | * 27 | */ 28 | @Configuration 29 | @EnableJpaRepositories( 30 | basePackages = "net.guides.springboot.springbootmultipledatasources.security.repositories", 31 | entityManagerFactoryRef = "securityEntityManagerFactory", 32 | transactionManagerRef = "securityTransactionManager" 33 | ) 34 | public class SecurityDataSourceConfig 35 | { 36 | @Autowired 37 | private Environment env; 38 | 39 | @Bean 40 | @ConfigurationProperties(prefix="datasource.security") 41 | public DataSourceProperties securityDataSourceProperties() { 42 | return new DataSourceProperties(); 43 | } 44 | 45 | @Bean 46 | public DataSource securityDataSource() { 47 | DataSourceProperties securityDataSourceProperties = securityDataSourceProperties(); 48 | return DataSourceBuilder.create() 49 | .driverClassName(securityDataSourceProperties.getDriverClassName()) 50 | .url(securityDataSourceProperties.getUrl()) 51 | .username(securityDataSourceProperties.getUsername()) 52 | .password(securityDataSourceProperties.getPassword()) 53 | .build(); 54 | } 55 | 56 | @Bean 57 | public PlatformTransactionManager securityTransactionManager() 58 | { 59 | EntityManagerFactory factory = securityEntityManagerFactory().getObject(); 60 | return new JpaTransactionManager(factory); 61 | } 62 | 63 | @Bean 64 | public LocalContainerEntityManagerFactoryBean securityEntityManagerFactory() 65 | { 66 | LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); 67 | factory.setDataSource(securityDataSource()); 68 | factory.setPackagesToScan(new String[]{"net.guides.springboot.springbootmultipledatasources.security.entities"}); 69 | factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); 70 | 71 | Properties jpaProperties = new Properties(); 72 | jpaProperties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto")); 73 | jpaProperties.put("hibernate.show-sql", env.getProperty("spring.jpa.show-sql")); 74 | factory.setJpaProperties(jpaProperties); 75 | 76 | return factory; 77 | } 78 | 79 | @Bean 80 | public DataSourceInitializer securityDataSourceInitializer() 81 | { 82 | DataSourceInitializer dataSourceInitializer = new DataSourceInitializer(); 83 | dataSourceInitializer.setDataSource(securityDataSource()); 84 | ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); 85 | databasePopulator.addScript(new ClassPathResource("security-data.sql")); 86 | dataSourceInitializer.setDatabasePopulator(databasePopulator); 87 | dataSourceInitializer.setEnabled(env.getProperty("datasource.security.initialize", Boolean.class, false)); 88 | return dataSourceInitializer; 89 | } 90 | 91 | 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/net/guides/springboot/springbootmultipledatasources/config/WebMvcConfig.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package net.guides.springboot.springbootmultipledatasources.config; 5 | 6 | import org.springframework.context.annotation.Bean; 7 | 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter; 10 | 11 | /** 12 | * @author Ramesh Fadatare 13 | * 14 | */ 15 | @Configuration 16 | public class WebMvcConfig 17 | { 18 | 19 | @Bean 20 | public OpenEntityManagerInViewFilter securityOpenEntityManagerInViewFilter() 21 | { 22 | OpenEntityManagerInViewFilter osivFilter = new OpenEntityManagerInViewFilter(); 23 | osivFilter.setEntityManagerFactoryBeanName("securityEntityManagerFactory"); 24 | return osivFilter; 25 | } 26 | 27 | @Bean 28 | public OpenEntityManagerInViewFilter ordersOpenEntityManagerInViewFilter() 29 | { 30 | OpenEntityManagerInViewFilter osivFilter = new OpenEntityManagerInViewFilter(); 31 | osivFilter.setEntityManagerFactoryBeanName("ordersEntityManagerFactory"); 32 | return osivFilter; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/net/guides/springboot/springbootmultipledatasources/controllers/HomeController.java: -------------------------------------------------------------------------------- 1 | 2 | package net.guides.springboot.springbootmultipledatasources.controllers; 3 | 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.stereotype.Controller; 6 | import org.springframework.ui.Model; 7 | import org.springframework.web.bind.annotation.RequestMapping; 8 | import org.springframework.web.bind.annotation.RequestMethod; 9 | 10 | import net.guides.springboot.springbootmultipledatasources.services.UserOrdersService; 11 | 12 | /** 13 | * @author Ramesh Fadatare 14 | * 15 | */ 16 | @Controller 17 | public class HomeController 18 | { 19 | @Autowired 20 | private UserOrdersService userOrdersService; 21 | 22 | @RequestMapping(value = {"/", "/app/users"}, method = RequestMethod.GET) 23 | public String getUsers(Model model) 24 | { 25 | model.addAttribute("users", userOrdersService.getUsers()); 26 | model.addAttribute("orders", userOrdersService.getOrders()); 27 | 28 | return "users"; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/net/guides/springboot/springbootmultipledatasources/orders/entities/Order.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package net.guides.springboot.springbootmultipledatasources.orders.entities; 5 | 6 | import java.util.Set; 7 | 8 | import jakarta.persistence.*; 9 | 10 | /** 11 | * @author Ramesh Fadatare 12 | * 13 | */ 14 | @Entity 15 | @Table(name="ORDERS") 16 | public class Order 17 | { 18 | @Id @GeneratedValue(strategy=GenerationType.AUTO) 19 | private Integer id; 20 | @Column(nullable=false, name="cust_name") 21 | private String customerName; 22 | @Column(nullable=false, name="cust_email") 23 | private String customerEmail; 24 | 25 | @OneToMany(mappedBy="order") 26 | private Set orderItems; 27 | 28 | public Integer getId() { 29 | return id; 30 | } 31 | public void setId(Integer id) { 32 | this.id = id; 33 | } 34 | public String getCustomerName() { 35 | return customerName; 36 | } 37 | public void setCustomerName(String customerName) { 38 | this.customerName = customerName; 39 | } 40 | public String getCustomerEmail() { 41 | return customerEmail; 42 | } 43 | public void setCustomerEmail(String customerEmail) { 44 | this.customerEmail = customerEmail; 45 | } 46 | public Set getOrderItems() 47 | { 48 | return orderItems; 49 | } 50 | public void setOrderItems(Set orderItems) 51 | { 52 | this.orderItems = orderItems; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/net/guides/springboot/springbootmultipledatasources/orders/entities/OrderItem.java: -------------------------------------------------------------------------------- 1 | 2 | package net.guides.springboot.springbootmultipledatasources.orders.entities; 3 | 4 | import jakarta.persistence.*; 5 | 6 | /** 7 | * @author Ramesh Fadatare 8 | * 9 | */ 10 | @Entity 11 | @Table(name="ORDER_ITEMS") 12 | public class OrderItem 13 | { 14 | @Id @GeneratedValue(strategy=GenerationType.AUTO) 15 | private Integer id; 16 | private String productCode; 17 | private int quantity; 18 | @ManyToOne 19 | @JoinColumn(name="order_id") 20 | private Order order; 21 | 22 | public Integer getId() 23 | { 24 | return id; 25 | } 26 | public void setId(Integer id) 27 | { 28 | this.id = id; 29 | } 30 | public String getProductCode() 31 | { 32 | return productCode; 33 | } 34 | public void setProductCode(String productCode) 35 | { 36 | this.productCode = productCode; 37 | } 38 | public int getQuantity() 39 | { 40 | return quantity; 41 | } 42 | public void setQuantity(int quantity) 43 | { 44 | this.quantity = quantity; 45 | } 46 | public Order getOrder() 47 | { 48 | return order; 49 | } 50 | public void setOrder(Order order) 51 | { 52 | this.order = order; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/net/guides/springboot/springbootmultipledatasources/orders/repositories/OrderRepository.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package net.guides.springboot.springbootmultipledatasources.orders.repositories; 5 | 6 | import org.springframework.data.jpa.repository.JpaRepository; 7 | 8 | import net.guides.springboot.springbootmultipledatasources.orders.entities.Order; 9 | 10 | /** 11 | * @author Ramesh Fadatare 12 | * 13 | */ 14 | public interface OrderRepository extends JpaRepository{ 15 | 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/net/guides/springboot/springbootmultipledatasources/security/entities/Address.java: -------------------------------------------------------------------------------- 1 | 2 | package net.guides.springboot.springbootmultipledatasources.security.entities; 3 | 4 | import jakarta.persistence.*; 5 | 6 | /** 7 | * @author Ramesh Fadatare 8 | * 9 | */ 10 | @Entity 11 | @Table(name="ADDRESSES") 12 | public class Address 13 | { 14 | @Id @GeneratedValue(strategy=GenerationType.AUTO) 15 | private Integer id; 16 | @Column(nullable=false) 17 | private String city; 18 | @ManyToOne 19 | @JoinColumn(name="user_id") 20 | private User user; 21 | 22 | public Integer getId() 23 | { 24 | return id; 25 | } 26 | public void setId(Integer id) 27 | { 28 | this.id = id; 29 | } 30 | public String getCity() 31 | { 32 | return city; 33 | } 34 | public void setCity(String city) 35 | { 36 | this.city = city; 37 | } 38 | public User getUser() 39 | { 40 | return user; 41 | } 42 | public void setUser(User user) 43 | { 44 | this.user = user; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/net/guides/springboot/springbootmultipledatasources/security/entities/User.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package net.guides.springboot.springbootmultipledatasources.security.entities; 5 | 6 | import java.util.Set; 7 | 8 | import jakarta.persistence.*; 9 | 10 | /** 11 | * @author Ramesh Fadatare 12 | * 13 | */ 14 | @Entity 15 | @Table(name="USERS") 16 | public class User 17 | { 18 | @Id @GeneratedValue(strategy=GenerationType.AUTO) 19 | private Integer id; 20 | @Column(nullable=false) 21 | private String name; 22 | @Column(nullable=false, unique=true) 23 | private String email; 24 | private boolean disabled; 25 | @OneToMany(mappedBy="user") 26 | private Set
addresses; 27 | 28 | public User() 29 | { 30 | } 31 | 32 | public User(Integer id, String name, String email) 33 | { 34 | this.id = id; 35 | this.name = name; 36 | this.email = email; 37 | } 38 | public User(Integer id, String name, String email, boolean disabled) 39 | { 40 | this.id = id; 41 | this.name = name; 42 | this.email = email; 43 | this.disabled = disabled; 44 | } 45 | public Integer getId() 46 | { 47 | return id; 48 | } 49 | 50 | public void setId(Integer id) 51 | { 52 | this.id = id; 53 | } 54 | 55 | public String getName() 56 | { 57 | return name; 58 | } 59 | 60 | public void setName(String name) 61 | { 62 | this.name = name; 63 | } 64 | 65 | public String getEmail() 66 | { 67 | return email; 68 | } 69 | 70 | public void setEmail(String email) 71 | { 72 | this.email = email; 73 | } 74 | 75 | public boolean isDisabled() 76 | { 77 | return disabled; 78 | } 79 | 80 | public void setDisabled(boolean disabled) 81 | { 82 | this.disabled = disabled; 83 | } 84 | 85 | public Set
getAddresses() 86 | { 87 | return addresses; 88 | } 89 | 90 | public void setAddresses(Set
addresses) 91 | { 92 | this.addresses = addresses; 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /src/main/java/net/guides/springboot/springbootmultipledatasources/security/repositories/UserRepository.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package net.guides.springboot.springbootmultipledatasources.security.repositories; 5 | 6 | import org.springframework.data.jpa.repository.JpaRepository; 7 | 8 | import net.guides.springboot.springbootmultipledatasources.security.entities.User; 9 | 10 | /** 11 | * @author Ramesh Fadatare 12 | * 13 | */ 14 | public interface UserRepository extends JpaRepository 15 | { 16 | 17 | } 18 | 19 | -------------------------------------------------------------------------------- /src/main/java/net/guides/springboot/springbootmultipledatasources/services/UserOrdersService.java: -------------------------------------------------------------------------------- 1 | 2 | package net.guides.springboot.springbootmultipledatasources.services; 3 | 4 | import java.util.List; 5 | 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import net.guides.springboot.springbootmultipledatasources.orders.entities.Order; 11 | import net.guides.springboot.springbootmultipledatasources.orders.repositories.OrderRepository; 12 | import net.guides.springboot.springbootmultipledatasources.security.entities.User; 13 | import net.guides.springboot.springbootmultipledatasources.security.repositories.UserRepository; 14 | 15 | /** 16 | * @author Ramesh Fadatare 17 | * 18 | */ 19 | @Service 20 | public class UserOrdersService 21 | { 22 | @Autowired 23 | private OrderRepository orderRepository; 24 | 25 | @Autowired 26 | private UserRepository userRepository; 27 | 28 | @Transactional(transactionManager="securityTransactionManager") 29 | public List getUsers() 30 | { 31 | return userRepository.findAll(); 32 | } 33 | 34 | @Transactional(transactionManager="ordersTransactionManager") 35 | public List getOrders() 36 | { 37 | return orderRepository.findAll(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/resources/application-prod.properties: -------------------------------------------------------------------------------- 1 | 2 | datasource.security.driver-class-name=com.mysql.jdbc.Driver 3 | datasource.security.url=jdbc:mysql://localhost:3306/test 4 | datasource.security.username=root 5 | datasource.security.password=root 6 | 7 | datasource.security.initialize=true 8 | 9 | datasource.orders.driver-class-name=com.mysql.jdbc.Driver 10 | datasource.orders.url=jdbc:mysql://localhost:3306/orders 11 | datasource.orders.username=root 12 | datasource.orders.password=root 13 | 14 | datasource.orders.initialize=true 15 | 16 | spring.jpa.hibernate.ddl-auto=update 17 | spring.jpa.show-sql=true 18 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | debug=true 2 | 3 | datasource.security.driver-class-name=org.h2.Driver 4 | datasource.security.url=jdbc:h2:mem:securitydb;DB_CLOSE_DELAY=-1 5 | datasource.security.username=sa 6 | datasource.security.password= 7 | 8 | datasource.security.initialize=true 9 | 10 | datasource.orders.driver-class-name=org.h2.Driver 11 | datasource.orders.url=jdbc:h2:mem:ordersdb;DB_CLOSE_DELAY=-1 12 | datasource.orders.username=sa 13 | datasource.orders.password= 14 | 15 | datasource.orders.initialize=true 16 | 17 | spring.jpa.hibernate.ddl-auto=update 18 | spring.jpa.show-sql=true 19 | -------------------------------------------------------------------------------- /src/main/resources/orders-data.sql: -------------------------------------------------------------------------------- 1 | delete from order_items; 2 | delete from orders; 3 | 4 | insert into orders(id, cust_name, cust_email) values(1,'Ramesh','ramesh@gmail.com'); 5 | insert into orders(id, cust_name, cust_email) values(2,'Jonn','sanjay@gmail.com'); 6 | insert into orders(id, cust_name, cust_email) values(3,'Salman','salman@gmail.com'); 7 | 8 | insert into order_items(id, productcode,quantity,order_id) values(1,'P100', 2, 1); 9 | insert into order_items(id, productcode,quantity,order_id) values(2,'P101', 1, 1); 10 | insert into order_items(id, productcode,quantity,order_id) values(3,'P102', 5, 1); 11 | insert into order_items(id, productcode,quantity,order_id) values(4,'P103', 2, 2); 12 | insert into order_items(id, productcode,quantity,order_id) values(5,'P104', 1, 2); 13 | -------------------------------------------------------------------------------- /src/main/resources/security-data.sql: -------------------------------------------------------------------------------- 1 | delete from addresses; 2 | delete from users; 3 | 4 | insert into users(id, name, email,disabled) values(1,'Ramesh','ramesh@gmail.com', false); 5 | insert into users(id, name, email,disabled) values(2,'john','john@gmail.com', false); 6 | insert into users(id, name, email,disabled) values(3,'Salman','salman@gmail.com', true); 7 | 8 | insert into addresses(id,city,user_id) values(1, 'City1',1); 9 | insert into addresses(id,city,user_id) values(2, 'City2',1); 10 | insert into addresses(id,city,user_id) values(3, 'City3',2); 11 | insert into addresses(id,city,user_id) values(4, 'City4',3); 12 | insert into addresses(id,city,user_id) values(5, 'City5',3); 13 | insert into addresses(id,city,user_id) values(6, 'City6',3); 14 | -------------------------------------------------------------------------------- /src/main/resources/templates/users.html: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | SpringBoot 7 | 8 | 9 |
10 |

Users

11 |
12 |
13 |

Name: Name

14 |

Addresses

15 |
16 |

City

17 |
18 |
19 |
20 |
21 |

Orders

22 |
23 |
24 |

Customer Name: customerName

25 |

Order Items

26 |
27 |

productCode

28 |
29 |
30 |
31 | 32 | 33 | -------------------------------------------------------------------------------- /src/test/java/net/guides/springboot/springbootmultipledatasources/SpringbootMultipleDatasourcesApplicationTests.java: -------------------------------------------------------------------------------- 1 | package net.guides.springboot.springbootmultipledatasources; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class SpringbootMultipleDatasourcesApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------