You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

非反应式Spring Boot应用能否不使用Hibernate连接Oracle数据库?

Absolutely! You absolutely can connect to an Oracle database in a non-reactive Spring Boot application without relying on Hibernate. There are several solid, lightweight alternatives that fit different use cases—let me walk you through the most practical options:

This is Spring's built-in, lightweight solution for JDBC operations, no ORM required. It handles connection pooling, resource management, and simplifies raw JDBC boilerplate.

Steps to implement:

  • Add dependencies to your pom.xml (Maven) or build.gradle (Gradle):
<dependencies>
    <!-- Spring Boot JDBC Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <!-- Oracle JDBC Driver -->
    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>
  • Configure the Oracle datasource in application.properties:
spring.datasource.url=jdbc:oracle:thin:@//your-oracle-host:1521/your-service-name
spring.datasource.username=your-db-username
spring.datasource.password=your-db-password
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
  • Use JdbcTemplate in your repository:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepository {
    private final JdbcTemplate jdbcTemplate;

    // Constructor injection (preferred over field injection)
    public UserRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public String getUserNameById(Long userId) {
        String sql = "SELECT username FROM users WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{userId}, String.class);
    }
}
2. MyBatis (For Complex SQL Mappings)

If you need more control over SQL queries and want to map results to objects without full ORM overhead, MyBatis is a great choice. It's a SQL mapper framework that lets you define queries in XML or annotations.

Steps to implement:

  • Add dependencies:
<dependencies>
    <!-- MyBatis Spring Boot Starter -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.3.1</version> <!-- Use the latest stable version -->
    </dependency>
    <!-- Oracle JDBC Driver -->
    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>
  • Configure datasource and MyBatis settings:
spring.datasource.url=jdbc:oracle:thin:@//your-oracle-host:1521/your-service-name
spring.datasource.username=your-db-username
spring.datasource.password=your-db-password
# Path to your MyBatis mapper XML files (optional if using annotations)
mybatis.mapper-locations=classpath:mappers/*.xml
  • Create a mapper interface:
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper {
    @Select("SELECT username FROM users WHERE id = #{userId}")
    String getUserNameById(Long userId);
}

You can technically use raw Connection, Statement, and ResultSet objects, but this is not advised for production. It requires manual resource management (closing connections, statements) and lacks connection pooling, which leads to poor performance and stability issues. Spring Boot's built-in solutions handle all this for you automatically.

Key Note:

No matter which approach you choose, Spring Boot automatically configures a connection pool (default is HikariCP) for your datasource, so you don't have to manage database connections manually.

内容的提问来源于stack exchange,提问作者user1354825

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 15:18:12