【问题标题】:How to get data from oracle without jpa on spring boot如何在没有 jpa 的情况下在 spring boot 上从 oracle 获取数据
【发布时间】:2018-12-14 06:46:16
【问题描述】:

我想在 Spring Boot 中从 oracle 数据库中获取数据,但我不想使用 JPA。你能给我一个例子,我应该怎么做?谢谢你。

【问题讨论】:

标签: java oracle spring-boot


【解决方案1】:

在 Spring-Boot 中使用没有 JPA 的数据库可以使用 Spring-Boot 的 JDBC starter。

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

对于 Oracle,您还需要使用 JDBC 驱动程序。比如这个:

<dependency>
    <groupId>oracle.jdbc</groupId>
    <artifactId>ojdbc7</artifactId>         
    <version>12.1.0.2</version>
    <classifier>jdk17</classifier>
</dependency>

在 application.properties 文件中你必须配置数据源:

spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=

这就是配置所需的全部内容。要对数据库进行选择,您必须在任何 spring bean 类中自动装配JdbcTemplate

@Component
public class DataDao {

    private final JdbcTemplate jdbcTemplate;

    public DataDao(JdbcTemplate jdbcTemplate) {
        super();
        this.jdbcTemplate = jdbcTemplate;
    }

自动装配jdbcTemplate 后,您就可以查询数据库了:

jdbcTemplate.query(yourQuery, RowMapper<?>);

【讨论】:

    猜你喜欢
    • 2019-01-26
    • 2018-07-12
    • 2020-12-23
    • 2019-09-16
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2018-11-26
    相关资源
    最近更新 更多