使用muiltiple数据源H2和Mysql进行Spring启动,而不是在控制台中创建H2表。 访问h2表时收到错误,表示找不到表。 在检查控制台时,我可以确认表格是否正在创建。
我有两个数据源,一个是ibdemo,它使用MySQL,另一个是h2。实体类在相应的包中。
在删除自定义数据源配置时会创建H2表并尝试自动配置,但MySQL表也会在H2数据库中创建。
我想在我的应用程序中使用它们。 这些是属性
## ibdemo proeprties
app.datasource.ibdemo.url=jdbc:mysql://url
app.datasource.ibdemo.username=username
app.datasource.ibdemo.password=password
app.datasource.ibdemo.driver-class-name=com.mysql.jdbc.Driver
## H2 DB properties
spring.h2.console.enabled=true
app.datasource.h2.url=jdbc:h2:mem:testdb
app.datasource.h2.driver-class-name=org.h2.Driver
app.datasource.h2.username=sa
app.datasource.h2.password=
IBDemoDataSource
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "ibDemoEntityManager",
transactionManagerRef = "ibDemoTransactionManager",
basePackages = {"com.cavion.demo.repositories"}
)
public class IBDemoConfig {
@Bean(name = "ibDemoEntityManager")
@Primary
public LocalContainerEntityManagerFactoryBean getIBDemoEntityManager(EntityManagerFactoryBuilder builder,
@Qualifier("ibDemoDataSource") DataSource ibDemoDataSource){
return builder
.dataSource(ibDemoDataSource)
.packages("com.cavion.demo.entity")
.persistenceUnit("ibdemo")
.build();
}
@Bean("ibDemoDataSourceProperties")
@Primary
@ConfigurationProperties("app.datasource.ibdemo")
public DataSourceProperties ibDemoDataSourceProperties(){
return new DataSourceProperties();
}
@Bean("ibDemoDataSource")
@Primary
@ConfigurationProperties("app.datasource.ibdemo")
public DataSource ibDemoDataSource(@Qualifier("ibDemoDataSourceProperties") DataSourceProperties ibDemoDataSourceProperties) {
return ibDemoDataSourceProperties().initializeDataSourceBuilder().build();
}
@Bean(name = "ibDemoTransactionManager")
public JpaTransactionManager transactionManager(@Qualifier("ibDemoEntityManager") EntityManagerFactory ibDemoEntityManager){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(ibDemoEntityManager);
return transactionManager;
}
}
H2DataSource
@Configuration
@EnableJpaRepositories(
entityManagerFactoryRef = "h2DemoEntityManager",
transactionManagerRef = "h2DemoTransactionManager",
basePackages = {"com.tfs.repository"}
)
public class H2DemoConfig {
@Bean(name = "h2DemoEntityManager")
public LocalContainerEntityManagerFactoryBean getServersEntityManager(EntityManagerFactoryBuilder builder,
@Qualifier("h2DemoDataSource") DataSource h2DemoDataSource){
return builder
.dataSource(h2DemoDataSource)
.packages("com.tfs.entity")
.persistenceUnit("h2demo")
.build();
}
@Bean("h2DemoDataSourceProperties")
@ConfigurationProperties("app.datasource.h2")
public DataSourceProperties h2DemoDataSourceProperties(){
return new DataSourceProperties();
}
@Bean("h2DemoDataSource")
@ConfigurationProperties("app.datasource.h2")
public DataSource h2DemoDataSource(@Qualifier("h2DemoDataSourceProperties") DataSourceProperties h2DemoDataSourceProperties) {
return h2DemoDataSourceProperties().initializeDataSourceBuilder().build();
}
@Bean(name = "h2DemoTransactionManager")
public JpaTransactionManager transactionManager(@Qualifier("h2DemoEntityManager") EntityManagerFactory h2DemoEntityManager){
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(h2DemoEntityManager);
return transactionManager;
}
}
**Main method**
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class})
@EnableAsync
@EntityScan({"com.cavion.demo.entity","com.tfs.entity"})
@ComponentScan({"com.cavion.controller","com.cavion.service",
"com.udemanager.service", "com.udemanager.controller", "com.udemanager.entity" ,"com.tfs.controller","com.tfs.service"})
@Import({IBDemoConfig.class, H2DemoConfig.class})
public class UdeManagerServiceApplication extends SpringBootServletInitializer {
}
日志好像H2 DataSource仍在使用MySql5 Dialect
2018-04-29 13:13:29.173 INFO 1888 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-04-29 13:13:29.173 INFO 1888 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-04-29 13:13:57.987 INFO 1888 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'ibdemo'
2018-04-29 13:13:57.987 INFO 1888 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: ibdemo
...]
2018-04-29 13:13:58.019 INFO 1888 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2018-04-29 13:13:58.128 INFO 1888 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'ibdemo'
2018-04-29 13:13:58.347 INFO 1888 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'h2demo'
2018-04-29 13:13:58.347 INFO 1888 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: h2demo
...]
2018-04-29 13:13:58.394 INFO 1888 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2018-04-29 13:13:58.440 INFO 1888 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'h2demo'
2018-04-29 13:13:58.550 INFO 1888 --- [ restartedMain] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory
2018-04-29 13:13:58.846 INFO 1888 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService
答案 0 :(得分:0)
如何创建表格?有了spring数据源吗? 如果在@SpringBootApplication中排除DataSourceAutoConfiguration.class,则不会发生数据库初始化。
您可以查看org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration的代码。