簡體   English   中英

Spring 引導與現有 JDBC 連接

[英]Spring boot connect with existing JDBC Connection

Spring 引導根據 application.properties 中的配置提供了自己的數據庫連接。 但是這里我有一項服務,它為我提供了 javax.sql.Connection 類型的 object。

src/main/resources/application.properties

server.port=9090
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

這是存儲庫的代碼

package com.example.springbootdemo.repositories;
import org.springframework.data.repository.CrudRepository;
import com.example.springbootdemo.model.Box;
public interface BoxRepository extends CrudRepository<Box, Long> {
}

controller 的代碼

package com.example.springbootdemo.controllers;

import com.example.springbootdemo.model.Box;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.springbootdemo.repositories.BoxRepository;

@RestController
public class BoxController {

@Autowired
BoxRepository boxrepository;

@PostMapping("/box")
public Box addBox(Box box){
    return this.boxrepository.save(box);
}
}

Here when I am calling save function of JPA repository it saves the object using db object which it is calculating by using some of its own wrapper.

但我必須使用 jar 來連接數據庫。 我必須使用從該 jar 返回的連接 object,而不是 src/main/resources/application.properties 中的配置。 現在我需要覆蓋 spring 引導在內部使用的連接 object。 我無法弄清楚我該如何做到這一點。

你有這個路徑:src//main//resoruces//application.properties

在這里你需要配置

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM