简体   繁体   中英

Spring boot connect with existing JDBC Connection

Spring boot provided it's own database connection according to configuration in application.properties. But here I have a service which provided me an object of javax.sql.Connection type.

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

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.

But I have to use a jar which gives me Database connection. Instead of configuration in src/main/resources/application.properties, I have to use connection object returned from this jar. Now I'll need to override the connection object that spring boot is using internally. I am not able to figure out how I can do this.

you have this path: src//main//resoruces//application.properties

and here you need to configure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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