【问题标题】:Spring boot connect with existing JDBC ConnectionSpring Boot 与现有的 JDBC 连接连接
【发布时间】:2019-11-04 12:40:57
【问题描述】:

Spring boot 根据 application.properties 中的配置提供了自己的数据库连接。但是这里我有一个服务,它为我提供了一个 javax.sql.Connection 类型的对象。

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> {
}

控制器代码

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);
}
}

在这里,当我调用 JPA 存储库的保存函数时,它使用 db 对象保存对象,它使用自己的一些包装器计算该对象。

但我必须使用一个 jar 来连接数据库。我必须使用从这个 jar 返回的连接对象,而不是 src/main/resources/application.properties 中的配置。现在我需要覆盖 Spring Boot 在内部使用的连接对象。我无法弄清楚我该如何做到这一点。

【问题讨论】:

  • 只需阅读 Spring-Boot 手册。
  • @Zorglube 我试图通过阅读手册来解决这个问题,我该如何建立一个只有我可以在那里找到的连接,但是如何使用现有的连接?
  • 为了清楚起见请参考这个:stackoverflow.com/questions/56796562/….
  • 在您的配置文件中,您必须引用 JNDI 提供的连接,而不是创建一个。

标签: java spring spring-boot database-connection


【解决方案1】:

你有这个路径:src//main//resoruces//application.properties

这里需要配置

【讨论】:

  • 如果您提及要配置的属性会有所帮助。
  • 我已经更新了我的问题,我可以使用 src//main//resoruces//application.properties 来完成,但我想使用现有的 JDBC 连接而不是创建新的连接。
  • 在您的配置文件中,您必须引用 JNDI 提供的连接,而不是创建一个。
猜你喜欢
  • 2016-08-14
  • 1970-01-01
  • 1970-01-01
  • 2022-11-01
  • 2020-08-07
  • 1970-01-01
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
相关资源
最近更新 更多