Springboot spring data jpa 多数据源的配置 (说明:这只是引入了多个数据源,他们各自管理各自的事务,并没有实现统一的事务控制)
例: user数据库 global 数据库
1 .application.properties
#database edi configuration
datasource.edi.jdbc-url=jdbc:mysql:///:3306/Bookings?autoReconnect=true&rewriteBatchedStatements=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
datasource.edi.driverClassName=com.mysql.cj.jdbc.Driver
datasource.edi.username=root
datasource.edi.password=root
datasource.edi.sql-script-encoding=UTF-8
datasource.edi.validation-query=SELECT 1
datasource.edi.initial-size=2
#database edi configuration datasource.bookings.jdbc-url=jdbc:mysql:///:3306/Bookings?autoReconnect=true&rewriteBatchedStatements=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
datasource.bookings.driverClassName=com.mysql.cj.jdbc.Driver
datasource.bookings.username=root
datasource.bookings.password=root
datasource.bookings.sql-script-encoding=UTF-8
datasource.bookings.validation-query=SELECT 1
datasource.bookings.initial-size=2
# Show all queries spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate.naming.physical-strategy =com.icil.milestone.push.query.strategy.UpperTableStrategy
2.配置: (下面为bookings数据库的配置)
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.jdbc.core.JdbcTemplate; 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 = "bookingsEntityManagerFactory", transactionManagerRef = "bookingsTransactionManager", basePackages = {"com.icil.elsa.collection.repository.bookings"})//repository的路径,给个数据源,务必单独一个文件夹 public class BookingsDbConfig { @Qualifier("bookingsDataSource") @Bean(name = "bookingsDataSource") @ConfigurationProperties(prefix = "datasource.bookings") //指定数据源的配置 public DataSource bookingsDataSource() { return DataSourceBuilder.create().build(); } /*protected Map<String, Object> jpaProperties() { Map<String, Object> props = new HashMap<>(); props.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName()); props.put("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName()); return props; }*/ @Bean("bookingsEntityManagerFactory") public LocalContainerEntityManagerFactoryBean entityManagerFactory( EntityManagerFactoryBuilder builder, @Qualifier("bookingsDataSource") DataSource dataSource) { return builder .dataSource(dataSource) .packages("com.icil.elsa.collection.entries.bookings") ////指定实体对象的路径,给个数据源,务必单独一个文件夹 .persistenceUnit("bookings") .build(); } @Bean(name = "bookingsTransactionManager") public PlatformTransactionManager transactionManager( @Qualifier("bookingsEntityManagerFactory") EntityManagerFactory entityManagerFactory) { return new JpaTransactionManager(entityManagerFactory); } @Bean(name = "bookingJdbcTemplate") public JdbcTemplate bookingJdbcTemplate(@Qualifier("bookingsDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } }
配置: (下面为EDI数据库的配置--和上面一样) booking 改为EDI