简体   繁体   English

Spring 引导与现有 JDBC 连接

[英]Spring boot connect with existing JDBC Connection

Spring boot provided it's own database connection according to configuration in application.properties. Spring 引导根据 application.properties 中的配置提供了自己的数据库连接。 But here I have a service which provided me an object of javax.sql.Connection type.但是这里我有一项服务,它为我提供了 javax.sql.Connection 类型的 object。

src/main/resources/application.properties 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

Here is code for repository这是存储库的代码

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

Code for controller 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. 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.

But I have to use a jar which gives me Database connection.但我必须使用 jar 来连接数据库。 Instead of configuration in src/main/resources/application.properties, I have to use connection object returned from this jar.我必须使用从该 jar 返回的连接 object,而不是 src/main/resources/application.properties 中的配置。 Now I'll need to override the connection object that spring boot is using internally.现在我需要覆盖 spring 引导在内部使用的连接 object。 I am not able to figure out how I can do this.我无法弄清楚我该如何做到这一点。

you have this path: src//main//resoruces//application.properties你有这个路径:src//main//resoruces//application.properties

and here you need to configure在这里你需要配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM