我是Spring&的新手Spring启动,我正在尝试访问两个数据源,但我只能连接到一个数据源。
当我尝试调用访问第二个数据源的函数时,调用转到第一个数据源,我得到以下错误,
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Table 'testify.test'不存在
我已经按照以下教程进行多数据库访问,我的代码类似于 Roufid 链接中的代码,
有人可以指出我错过了什么或我在做错了吗 ?
下面是代码文件,
AConfig.java
@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "aEntityManager",
transactionManagerRef = "aTransactionManager",
basePackages = "test.serv")
public class AConfig {
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource.adb")
public DataSource aDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name="aEntityManager")
public LocalContainerEntityManagerFactoryBean mysqlEntManFactory(EntityManagerFactoryBuilder build) {
return build.dataSource(aDataSource()).persistenceUnit("aPU").build();
}
@Primary
@Bean(name="aTransactionManager")
public PlatformTransactionManager mysqlTransactionManager(@Qualifier("aEntityManager") EntityManagerFactory enManFact) {
return new JpaTransactionManager(enManFact);
}
}
BConfig.java
@Configuration
@EnableJpaRepositories(entityManagerFactoryRef = "bEntityManager",
transactionManagerRef = "bTransactionManager",
basePackages = "test.serv")
public class BConfig {
@Bean
@ConfigurationProperties(prefix="spring.datasource.bdb")
public DataSource bDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name="bEntityManager")
public LocalContainerEntityManagerFactoryBean mysqlEntManFactory(EntityManagerFactoryBuilder build) {
return build.dataSource(bDataSource()).persistenceUnit("bPU").build();
}
@Bean(name="bTransactionManager")
public PlatformTransactionManager mysqlTransactionManager(@Qualifier("bEntityManager") EntityManagerFactory enManFact) {
return new JpaTransactionManager(enManFact);
}
}
的persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="aPU" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
<persistence-unit name="bPU" transaction-type="RESOURCE_LOCAL">
</persistence-unit>
</persistence>
Service.java
public interface Service extends CrudRepository<X, Integer> {
@Query(value = "select y from test where x = :x", nativeQuery = true)
List<Object[]> findByName(@Param("x") String x);
}
答案 0 :(得分:0)
问题是你的存储库类(Service
)在同一个包中
@EnableJpaRepositories(entityManagerFactoryRef = "bEntityManager",
transactionManagerRef = "bTransactionManager",
basePackages = "test.serv")
basePackages =“test.serv”
将其移至seprate包中。这应该工作