Spring Boot无法从已配置的外部oracle数据库中获取数据

时间:2019-03-22 09:28:46

标签: spring spring-boot jpa spring-data-jpa datasource

我想用spring boot2配置oracle数据库。 我已经开发了dao层代码,并已成功使用嵌入的H2数据库进行了单元测试。但是现在当我在applicaiton.yaml文件中提供数据源配置详细信息时,它无法从外部oracle数据库检索数据。没有显示错误。

这是我的配置

pom.xml

<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <!-- <scope>runtime</scope> -->
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc</artifactId>
            <version>11.2.0.3</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/ojdbc6.jar</systemPath>
        </dependency>

服务层:

@Autowired
SampleActivityLogRepository repository;

//inside a method
Optional<List<SampleActivityLog>> listOfActivityForToken = repository.findLogByAccNoandTokenIDOrderByDate(userId, example.getKey());

存储层:

@Repository
@Transactional
public interface SampleActivityLogRepository extends JpaRepository<SampleActivityLog, Long> {

    @Query("SELECT L FROM SampleActivityLog L WHERE L.accountId = ?1 AND L.userId = ?2 ORDER BY L.createdDate desc")
    Optional<List<SampleActivityLog>> findLogByAccNoandTokenIDOrderByDate(String accNo, String userId);
}

application.yaml:

spring:
  profiles: dev

logging:
  level:
    org.springframework: ERROR
    com.example.rest.Tokennotification: DEBUG

  datasource:
    url: jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = example.com)(PORT = 1875))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = sit)))
    username: mvcschenam
    password: support$12ab
    driverClassName: oracle.jdbc.OracleDriver
  jpa:
    database-platform: org.hibernate.dialect.Oracle10gDialect
    database: H2
    #openInView: false
    show_sql: true
    generate-ddl: false

我尝试过的事情:

  1. 我从pom.xml中删除了h2依赖项。但是它抛出错误,没有为数据源配置“ URL”

  2. 我尝试使用@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})删除自动配置 但是,它抛出了一个错误,即SampleActivityLogRepository只是一个需要实现的接口。但是我在这里看不到太多逻辑..因为连接到引导集成数据库时,相同的代码可以工作

我需要连接到外部数据库,但是我不确定我缺少什么。我不想更改我的代码,因为我认为它非常简洁并且可以正常工作。 请指导我在这里可以做什么。预先感谢

1 个答案:

答案 0 :(得分:0)

尝试将您的yml更改为:

spring:
  profiles: dev

  logging:
    level:
      org.springframework: ERROR
      com.example.rest.Tokennotification: DEBUG

  datasource:
    url: jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = example.com)(PORT = 1875))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = sit)))
    username: mvcschenam
    password: support$12ab
    driverClassName: oracle.jdbc.OracleDriver
  jpa:
    database-platform: org.hibernate.dialect.Oracle10gDialect
    database: H2
    #openInView: false
    show_sql: true
    generate-ddl: false

看来您的数据源和jpa不在日志记录节点下,而不是spring