我尝试在我的应用上配置2个数据库但是你成功了。
我这样做了:
我的application.properties
#BANCO
spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
#spring.datasource.driverClassName=org.postgresql.Driver
datasource.dbdnaso.driver-class-name=org.postgresql.Driver
datasource.dbdnaso.platform=postgres
datasource.dbdnaso.url=jdbc:postgresql://localhost:5432/db_dnaso
datasource.dbdnaso.username=postgres
datasource.dbdnaso.password=dna44100
datasource.dbregistro.platform=postgres
datasource.dbregistro.driver-class-name=org.postgresql.Driver
datasource.dbregistro.url=jdbc:postgresql://localhost:5432/db_registro
datasource.dbregistro.username=postgres
datasource.dbregistro.password=dna44100
我的DbRegistroConfiguration.java
@Configuration
@EnableJpaRepositories(basePackages = "br.com.lumera.data.dbregistro", entityManagerFactoryRef = "dbRegistroEntityManagerFactory", transactionManagerRef = "dbRegistroTransactionManager")
@EnableTransactionManagement
public class DbRegistroConfiguration {
@Bean
@ConfigurationProperties(prefix = "datasource.admin")
@Primary
public DataSource dbRegistroDataSource()
{
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean dbRegistroEntityManagerFactory(final EntityManagerFactoryBuilder builder)
{
return builder
.dataSource(dbRegistroDataSource())
.packages("br.com.lumera.data.dbregistro")
.persistenceUnit("dbRegistroPersistenceUnit")
.build();
}
@Bean
@Primary
public JpaTransactionManager dbRegistroTransactionManager(@Qualifier("dbRegistroEntityManagerFactory") final EntityManagerFactory factory)
{
return new JpaTransactionManager(factory);
}
}
我的DbDnasoConfiguration.java
@Configuration
@EnableJpaRepositories(basePackages = "br.com.lumera.data.dbdnaso", entityManagerFactoryRef = "dbDnasoEntityManagerFactory", transactionManagerRef = "dbDnasoTransactionManager")
@EnableTransactionManagement
public class DbDnasoConfiguration {
@Bean
@ConfigurationProperties(prefix = "datasource.dbdnaso")
public DataSource dbDnasoDataSource()
{
return DataSourceBuilder.create().build();
}
@Bean
public LocalContainerEntityManagerFactoryBean dbDnasoEntityManagerFactory(final EntityManagerFactoryBuilder builder)
{
return builder
.dataSource(dbDnasoDataSource())
.packages("br.com.lumera.data.dbdnaso")
.persistenceUnit("dbDnasoPersistenceUnit")
.build();
}
@Bean
public JpaTransactionManager dbDnasoTransactionManager(@Qualifier("dbDnasoEntityManagerFactory") final EntityManagerFactory factory)
{
return new JpaTransactionManager(factory);
}
}
我的Application.java
@Configuration
@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
@EnableConfigurationProperties
@EnableTransactionManagement
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我的2个Repostory ITbItemPendencia
@Repository
public class ITbItemPendencia{
@PersistenceContext(unitName = "dbDnasoPersistenceUnit")
EntityManager em;
public Integer getTbItemPendenciaByNmNaturezaTabelaCusta() {
return this.em.createQuery("Select t FROM TbItemPendencia t").getResultList().size();
}
}
IViewPendenciasImovelPC
@Repository
public class IViewPendenciasImovelPC {
@PersistenceContext(unitName = "dbRegistroPersistenceUnit")
EntityManager em;
public Integer total(){
return em.createQuery("SELECT t FROM ViewPendenciasImovelPC t ").getResultList().size();
}
}
当我尝试启动我的应用时出现此错误:
0 00:38:24.841 WARN 7464 --- [ main] o.a.tomcat.jdbc.pool.PooledConnection : Not loading a JDBC driver as driverClassName property is null.
2015-08-10 00:38:24.852 ERROR 7464 --- [ main] o.a.tomcat.jdbc.pool.ConnectionPool : Unable to create initial connections of pool.
java.sql.SQLException: The url cannot be null
at java.sql.DriverManager.getConnection(DriverManager.java:649)
at java.sql.DriverManager.getConnection(DriverManager.java:208)
at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:305)
at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:200)
at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:699)
at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:633)
at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:484)
at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:142)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:115)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:102)
at org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:126)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcServicesImpl.java:279)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:124)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at br.com.lumera.Application.main(Application.java:39)
但我的应用程序,如果我尝试执行我的iViewPendenciasImovelPC.total我得到了同样的错误,但我的ITbItemPendencia.getTbItemPendenciaByNmNaturezaTabelaCusta工作正常。
有人能帮帮我吗? TKS答案 0 :(得分:3)
正如我在DbRegistroConfiguration.java
中看到的那样,您必须按@ConfigurationProperties(prefix = "datasource.admin")
更改@ConfigurationProperties(prefix = "datasource.dbregistro")
。
答案 1 :(得分:2)
此demo project应该为您提供足够的信息。