application.properties配置:
#数据库配置 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:
@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了.跟单数据源没有区别.
参考:
https://www.cnblogs.com/sxdcgaq8080/p/7978205.html (springboot-1.5.9)
https://my.oschina.net/chinesedragon/blog/1647846 (springboot-2.0.0)
原文地址:https://www.cnblogs.com/niuzhifa/p/9279543.html
时间: 2024-10-07 21:57:24