我正在使用hibernate,spring boot,jpa 第一个数据库是MS SQL。新要求是连接Oracle JDBC 它是多模块应用程序。在主模块中,我具有带数据库凭据的application-remote.yml文件。在这里
spring:
datasource:
driver-class-name:com.microsoft.sqlserver.jdbc.SQLServerDriver
url: ************
username: *****
password: *****
secondDatasource:
driver-class-name: oracle.jdbc.OracleDriver
url: ***********
username: ******
password: ******
jpa:
hibernate:
ddl-auto: none
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
show-sql: true
properties:
dialect: org.hibernate.dialect.SQLServer2012Dialect
jackson:
date-format: com.fasterxml.jackson.databind.util.StdDateFormat
logging:
config: classpath:logback-remote.xml
file: /usr/share/tomcat/pqa.log
模块应用程序com.my.project中的“我的应用程序”配置
@Configuration
@Import({
ControllerConfig.class,
PersistenceConfig.class
})
public class ApplicationConfig {
}
模块persistence com.my.project中的我的persistenceConfig
@Configuration
public class PersistenceConfig {
@Bean
@Primary
@ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix="spring.secondDatasource")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}
}
我在持久性com.my.project.entity中有很多实体 其中之一是
@Data
@Entity
@Builder
@Table(name = "locationSelection", schema = "dbo")
public class Location {
@Id
@Column(name = "timerName")
private String timerName;
@Column(name = "center")
private String center;
@Column(name = "station")
private String station;
@Column(name = "cell")
private String cell;
@Column(name = "place")
private String place;
}
持久性库com.my.project.repository
@Repository
public interface LocationRepository extends JpaRepository<Location, String> {}
对于第二个数据,请参见Persistence com.my.project.entityForIntegration
@Data
@Builder
@Entity
@Table(name = "PQA_IN")
public class Pqa_In {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private int id;
@Column(name = "STATUS")
private String status;
@Column(name = "UPD_USER")
private String upd_user;
@Column(name = "UPD_DATE")
private Timestamp upd_date;
@Column(name = "INS_USER")
private String ins_user;
@Column(name = "INS_DATE")
private Timestamp ins_date;
}
以及com.my.project.repositoryForIntegration中的存储库
public interface Pqa_In_repository extends JpaRepository<Pqa_In, Long> {
}
现在这不起作用。我有很多错误,上面的类和配置错误是
**************************** 申请无法开始 **************************** 描述: com.my.project.services.impl.AnomaliesServiceImpl中构造函数的参数0需要一个类型为com.my.project.repository.AnomaliesRepository的bean。 行动: 考虑在配置中定义类型为“ com.my.project.repository.AnomaliesRepository”的bean。
我已阅读Spring文档,baeldung,stackoverflow和其他站点,以及有关多个数据源的指南和问题,但我无能为力。 请为我提供连接2个DB的正确解决方案。
AnomaliesServiceImpl
@Service
@Slf4j
public class AnomaliesServiceImpl implements AnomaliesService {
private AnomaliesRepository anomaliesRepository;
@Autowired
public AnomaliesServiceImpl(AnomaliesRepository anomaliesRepository) {
//
this.anomaliesRepository = anomaliesRepository;
}
@Override
public ResponseEntityDTO getAllAnomalies(int currentPageNumber, int pageSize) {
Page<WeldMeasureProt> page = anomaliesRepository.findAllAnomalies(pageable);
return convertToWeldMeasureProtDTO(page, currentPageNumber);
}
AnomaliesRepository
@Repository
public interface AnomaliesRepository extends PagingAndSortingRepository<WeldMeasureProt, WeldMeasurePointPrimaryKey> {
@Query("from WeldMeasureProt wm where wm.detection<>0 " +
"and wm.uirQStoppActCntValue=0 " +
"and wm.monitorState=0 ")
Page<WeldMeasureProt> findAllAnomalies(Pageable pageable);
}