繁体   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