网上大牛们的建议:一个系统最好一个数据源,多个数据源考虑使用微服务。
一、开发环境:
1.maven(version3.8.1)依赖:
<dependency> ? ? <groupId>org.springframework.boot</groupId> ? ? <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> ? ? <groupId>org.springframework.boot</groupId> ? ? <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> ? ? <groupId>mysql</groupId> ? ? <artifactId>mysql-connector-java</artifactId> ? ? <scope>runtime</scope> </dependency> <dependency> ? ? <groupId>org.projectlombok</groupId> ? ? <artifactId>lombok</artifactId> ? ? <optional>true</optional> </dependency>
2.java版本:17
3.springboot版本:2.7.4
4.mysql5.8
二、开发步骤
包路径约定:com.example.multiple-database.one 下放数据库1对应的类,com.example.multiple-database.two下放数据库2对应的类
1.在配置文件application.properties里添加参数:
spring.jpa.open-in-view=false
spring.datasource.jdbc-url=jdbc:mysql://127.0.0.1:3306/my_database_one?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=Asia/Shanghai spring.datasource.username=your database one username spring.datasource.password=your database one password
spring.second-datasource.jdbc-url=jdbc:mysql://127.0.0.1:3306/my_database_two?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=Asia/Shanghai spring.second-datasource.username=your database two username spring.second-datasource.password=your database two password
2.添加数据库1的配置类
package?com.example.multiple-database.config;
import?org.springframework.boot.context.properties.ConfigurationProperties; import?org.springframework.boot.jdbc.DataSourceBuilder; 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.orm.jpa.vendor.HibernateJpaVendorAdapter; import?org.springframework.transaction.PlatformTransactionManager;
import?javax.sql.DataSource; import?java.util.HashMap; import?java.util.Map;
@Configuration @EnableJpaRepositories( ? ? ? ? basePackages =?"com.example.multiple-database.one", ? ? ? ??entityManagerFactoryRef =?"dbOneEntityManager", ? ? ? ? //对应下面定义的entityManager函数名字 ? ? ? ??transactionManagerRef =?"dbOneTransactionManager")?//对应下面事务管理的函数名字 public class?DatabaseOneConfig { ? ??@Bean ? ? @Primary ? ? @ConfigurationProperties(prefix =?"spring.datasource") ? ??public?DataSource?dbOneDataSource(){ ? ? ? ??return?DataSourceBuilder.create().build(); ? ??}
? ??@Bean ? ??public?LocalContainerEntityManagerFactoryBean?dbOneEntityManager(){ ? ? ? ? LocalContainerEntityManagerFactoryBean em =?new?LocalContainerEntityManagerFactoryBean();
? ? ? ??em.setDataSource(mainDataSource()); ? ? ? ??em.setPackagesToScan("com.example.multiple-database.one"); //这个包路径下放跟dbOne相关的类 ? ? ? ??HibernateJpaVendorAdapter adapter =?new?HibernateJpaVendorAdapter(); ? ? ? ??em.setJpaVendorAdapter(adapter); ? ? ? ??Map<String,?Object> properties =?new?HashMap<>(); ? ? ? ??properties.put("hibernate.dialect",?"org.hibernate.dialect.MySQLInnoDBDialect"); ? ? ? ??properties.put("hibernate.physical_naming_strategy",?"org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy"); ? ? ? ??properties.put("hibernate.implicit_naming_strategy",?"org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy"); ? ? ? ??em.setJpaPropertyMap(properties);
? ? ? ? return?em; ? ??}
? ??@Bean ? ? @Primary ? ??public?PlatformTransactionManager?dbOneTransactionManager(){ ? ? ? ? JpaTransactionManager manager =?new?JpaTransactionManager(); ? ? ? ??manager.setEntityManagerFactory(dbOneEntityManager().getObject()); ? ? ? ? return?manager; ? ??}
}
3.添加数据库2的配置类
package?com.example.multiple-database.config;
import?org.springframework.boot.context.properties.ConfigurationProperties; import?org.springframework.boot.jdbc.DataSourceBuilder; 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.orm.jpa.vendor.HibernateJpaVendorAdapter; import?org.springframework.transaction.PlatformTransactionManager;
import?javax.sql.DataSource; import?java.util.HashMap; import?java.util.Map;
@Configuration @EnableJpaRepositories( ? ? ? ? basePackages =?"com.example.multiple-database.two", ? ? ? ??entityManagerFactoryRef =?"dbTwoEntityManager", //对应下面实体类管理函数名 ? ? ? ??transactionManagerRef =?"dbTwoTransactionManager")//对应下面事务管理函数名 public class?DatabaseTwoConfig?{ ? ??@Bean ? ? @ConfigurationProperties(prefix =?"spring.second-datasource") ? ??public?DataSource?oldDataSource(){ ? ? ? ??return?DataSourceBuilder.create().build(); ? ??}
? ??@Bean ? ??public?LocalContainerEntityManagerFactoryBean?dbTwoEntityManager(){ ? ? ? ? LocalContainerEntityManagerFactoryBean em =?new?LocalContainerEntityManagerFactoryBean();
? ? ? ??em.setDataSource(oldDataSource()); ? ? ? ??em.setPackagesToScan("com.example.multiple-database.two"); //数据库2的实体类扫描路径 ? ? ? ??HibernateJpaVendorAdapter adapter =?new?HibernateJpaVendorAdapter(); ? ? ? ??em.setJpaVendorAdapter(adapter); ? ? ? ??Map<String,?Object> properties =?new?HashMap<>(); ? ? ? ??properties.put("hibernate.dialect",?"org.hibernate.dialect.MySQLInnoDBDialect"); ? ? ? ??properties.put("hibernate.physical_naming_strategy",?"org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy"); ? ? ? ??properties.put("hibernate.implicit_naming_strategy",?"org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy"); ? ? ? ??em.setJpaPropertyMap(properties);
? ? ? ? return?em; ? ??}
? ??@Bean ? ??public?PlatformTransactionManager?dbTwoTransactionManager(){ ? ? ? ? JpaTransactionManager manager =?new?JpaTransactionManager(); ? ? ? ??manager.setEntityManagerFactory(dbTwoEntityManager().getObject()); ? ? ? ? return?manager; ? ??}
}
三、使用
1.查询:跟单数库使用好像没啥区别
2.(增删改)事务: ? ?@Transactional ? ?public void?save(Project project){ ? ? ? ??projectRepository.save(project); ? ?} ?
更多参考:
Spring JPA – Multiple Databases | Baeldung
|