Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data from oracle without jpa on spring boot

I want to get data from oracle database in spring boot, but I dont want to use JPA. Can you give me an example, how I should do it? Thank you.

like image 276
Ropiudin Ropiudin Avatar asked Oct 27 '25 23:10

Ropiudin Ropiudin


1 Answers

Using a Database without JPA in Spring-Boot you can use the JDBC starter of Spring-Boot.

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

For Oracle you also need to use the JDBC driver. For example this one:

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

In the application.properties file you have to configure the datasource:

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

This is all you need for the configuration. To make a select to the database you have to autowire JdbcTemplate in any of your spring bean classes.

@Component
public class DataDao {

    private final JdbcTemplate jdbcTemplate;

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

After autowiring the jdbcTemplate you are able to query the database:

jdbcTemplate.query(yourQuery, RowMapper<?>);
like image 200
Patrick Avatar answered Oct 29 '25 14:10

Patrick