application.properties配置:html
#數據庫配置 mysql.spring.datasource.jdbc-url =jdbc:mysql://127.0.0.1:3306/test mysql.spring.datasource.username=admin mysql.spring.datasource.password=123456 mysql.spring.datasource.driver-class-name=com.mysql.jdbc.Driver #數據庫鏈接池 mysql.spring.datasource.max-idle=100 mysql.spring.datasource.max-wait=10000 mysql.spring.datasource.min-idle=5 mysql.spring.datasource.initial-size=5 #sqlite數據庫的配置 sqlite.spring.datasource.jdbc-url = jdbc:sqlite:test.db sqlite.spring.datasource.driver-class-name = org.sqlite.JDBC sqlite.spring.datasource.username = sqlite.spring.datasource.password = #關閉hibernate的自動建立表結構的機制 spring.jpa.hibernate.ddl-auto=none spring.jpa.show-sql=true
DataSourceConfig:mysql
@Configuration public class DataSourceConfig { @Bean(name = "sqliteDataSource") @Qualifier(value = "sqliteDataSource") //spring裝配bean的惟一標識 @ConfigurationProperties(prefix = "sqlite.spring.datasource") //application.properties配置文件中該數據源的配置前綴 public DataSource sqliteDataSource(){ return DataSourceBuilder.create().build(); } @Primary //配置該數據源爲主數據源 @Bean(name = "mysqlDataSource") @Qualifier(value = "mysqlDataSource") @ConfigurationProperties(prefix = "mysql.spring.datasource") public DataSource mysqlDataSource(){ return DataSourceBuilder.create().build(); } }
MysqlDataSourceConfig
@Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryMysql", //EntityManagerFactory引用 transactionManagerRef = "transactionManagerMysql", //transactionManager引用 basePackages = {"com.xxx.mysql"}) public class MysqlDataSourceConfig { /** * 注入 mysql數據源 */ @Resource(name = "mysqlDataSource") private DataSource mysqlDataSource; /** * 注入JPA配置實體 */ @Autowired private JpaProperties jpaProperties; /** * 這裏其實不須要配置數據庫的方言. * 像hibernate.hbm2ddl.auto 能夠在這裏配置.可是個人是在application.properties中配置的. */ private Map<String, Object> getVendorProperties() { HibernateSettings hibernateSettings = new HibernateSettings(); return jpaProperties.getHibernateProperties(hibernateSettings); } /** * 配置EntityManagerFactory實體 * * @param builder * @return 實體管理工廠 * packages 掃描@Entity註釋的軟件包名稱 * persistenceUnit 持久性單元的名稱。 若是隻創建一個EntityManagerFactory,你能夠省略這個,可是若是在同一個應用程序中有多個,你應該給它們不一樣的名字 * properties 標準JPA或供應商特定配置的通用屬性。 這些屬性覆蓋構造函數中提供的任何值。 */ @Primary @Bean(name = "entityManagerFactoryMysql") public LocalContainerEntityManagerFactoryBean entityManagerFactoryMysql(EntityManagerFactoryBuilder builder) { return builder .dataSource(mysqlDataSource) .properties(getVendorProperties()) .packages("com.xxx.mysql") .persistenceUnit("mysqlPersistenceUnit") .build(); } /** * 配置EntityManager實體 * * @param builder * @return 實體管理器 */ @Primary @Bean(name = "entityManagerMysql") public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactoryMysql(builder).getObject().createEntityManager(); } /** * 配置事務transactionManager * * @param builder * @return 事務管理器 */ @Primary @Bean(name = "transactionManagerMysql") public PlatformTransactionManager transactionManagerMysql(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactoryMysql(builder).getObject()); } }
SqliteDataSourceConfig
@Configuration @EnableTransactionManagement @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactorySqlite", //EntityManagerFactory引用 transactionManagerRef = "transactionManagerSqlite", //transactionManager引用 basePackages = {"com.xxx.sqlite"}) public class SqliteDataSourceConfig { /** * 注入 sqlite數據源 */ @Resource(name = "sqliteDataSource") private DataSource sqliteDataSource; /** * 注入JPA配置實體 */ @Autowired private JpaProperties jpaProperties; /** * 這裏其實不須要配置數據庫的方言. */ private Map<String, Object> getVendorProperties() { HibernateSettings hibernateSettings = new HibernateSettings(); return jpaProperties.getHibernateProperties(hibernateSettings); } /** * 配置EntityManagerFactory實體 * * @param builder * @return 實體管理工廠 * packages 掃描@Entity註釋的軟件包名稱 * persistenceUnit 持久性單元的名稱。 若是隻創建一個EntityManagerFactory,你能夠省略這個,可是若是在同一個應用程序中有多個,你應該給它們不一樣的名字 * properties 標準JPA或供應商特定配置的通用屬性。 這些屬性覆蓋構造函數中提供的任何值。 */ @Bean(name = "entityManagerFactorySqlite") public LocalContainerEntityManagerFactoryBean entityManagerFactorySqlite(EntityManagerFactoryBuilder builder) { return builder .dataSource(sqliteDataSource) .properties(getVendorProperties()) .packages("com.xxx.sqlite") .persistenceUnit("sqlitePersistenceUnit") .build(); } /** * 配置EntityManager實體 * * @param builder * @return 實體管理器 */ @Bean(name = "entityManagerSqlite") public EntityManager entityManager(EntityManagerFactoryBuilder builder) { return entityManagerFactorySqlite(builder).getObject().createEntityManager(); } /** * 配置事務transactionManager * * @param builder * @return 事務管理器 */ @Bean(name = "transactionManagerSqlite") public PlatformTransactionManager transactionManagerSqlite(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(entityManagerFactorySqlite(builder).getObject()); } }
而後com.xxx.mysql和com.xxx.sqlite目錄下就能夠放各自的domain和repository了.跟單數據源沒有區別.spring
參考:sql
http://www.javashuo.com/article/p-mpttiikt-bo.html (springboot-1.5.9)數據庫
https://my.oschina.net/chinesedragon/blog/1647846 (springboot-2.0.0)springboot