package com.example.msmultipledatasource.configuration; import com.zaxxer.hikari.HikariDataSource; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; 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.Database; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; import java.util.Properties; @Configuration @EnableJpaRepositories( basePackages = {"com.example.msmultipledatasource.dao.first"},// 1. dao 层所在的包 entityManagerFactoryRef = "firstEntityManagerFactory") @EnableTransactionManagement public class FirstDataSourceConfigure { @Bean @Primary public LocalContainerEntityManagerFactoryBean firstEntityManagerFactory() { LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); factoryBean.setDataSource(firstDataSource()); HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.SQLServer2008Dialect"); jpaVendorAdapter.setDatabase(Database.SQL_SERVER); jpaVendorAdapter.setShowSql(true); factoryBean.setJpaVendorAdapter(jpaVendorAdapter); // 2. 实体类所在的包 factoryBean.setPackagesToScan("com.example.msmultipledatasource.domain.first"); return factoryBean; } @Bean @Primary @ConfigurationProperties("app.datasource.first") public DataSourceProperties firstDataSourceProperties() { return new DataSourceProperties(); } @Bean @Primary @ConfigurationProperties("app.datasource.first.configuration") public DataSource firstDataSource() { return firstDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build(); } }
logging: level: org.hibernate.type.descriptor.sql.BasicBinder: trace spring: application: name: msmultipledatasource jpa: show-sql: true hibernate: naming: physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy eureka: client: service-url: defaultZone: http://127.0.0.1:8761/eureka/ app: datasource: first: driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=ckgl username: sa password: second: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/springcloud_sell?characterEncoding=utf-8&useSSL=false username: root password: 123456
weixin_慕数据1513485