1. 程式人生 > >Spring Boot 2.0.3 JDBC整合Oracle 12

Spring Boot 2.0.3 JDBC整合Oracle 12

整合步驟

1. Oracle驅動引入

Oracle驅動一般不能通過maven倉庫直接下載得到,需自行下載並匯入到專案的lib目錄下,建議通過如下pom依賴引入下載的Oracle驅動

        <!-- Oracle 驅動 -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1
.0.2</version> <scope>system</scope> <!-- 在專案根目錄下建立lib目錄,引入Oracle驅動相關jar包 --> <systemPath>${basedir}/lib/ojdbc7-12.1.0.2.jar</systemPath> </dependency>

2. POM依賴

        <!-- Spring Boot JDBC 模組 -->
        <dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- Oracle 驅動 --> <dependency> <groupId>com.oracle</groupId> <artifactId>
ojdbc7</artifactId> <version>12.1.0.2</version> <scope>system</scope> <!-- 在專案根目錄下建立lib目錄,引入Oracle驅動相關jar包 --> <systemPath>${basedir}/lib/ojdbc7-12.1.0.2.jar</systemPath> </dependency>

3. application.properties檔案完善

spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe
spring.datasource.username=dev
spring.datasource.password=dev
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver

4.Controller層實現


@Controller
public class OneController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/")
    @ResponseBody
    public String index(){

        String sql = "select sysdate from dual";
        jdbcTemplate.execute(sql);
        System.out.println("執行完成");

        return "hello spring boot";
    }
}

5. 測試

http://localhost:8080/

參考文件

Spring boot with Oracle

spring-boot jdbc 連線Oracle