gpt4 book ai didi

java - 从使用 Spring 的 DataSourceBuilder 创建的数据源获取到 Postgresql 数据库的连接

转载 作者:行者123 更新时间:2023-11-29 12:54:09 25 4
gpt4 key购买 nike

我是 Spring 和 Spring Boot 的新手。我正在尝试在我的 Spring Boot 应用程序中连接到 Postgresql 数据库。我试图从中汲取灵感

http://spring.io/guides/gs/accessing-data-mysql/

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html

基本上我有一个 application.properties 文件,其中包含:

app.datasource.url=jdbc:postgresql://localhost:5432/db_example
app.datasource.username=myusername
app.datasource.password=mypassword
app.datasource.driver-class-name=org.postgresql.Driver

使用 psql 工具,我可以使用 5432 端口和这些用户名和密码正常连接到本地主机上的 db_example 数据库。

然后我有一个 MyConfig.java,其中包含:

package hello;

import javax.sql.DataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class MyConfig {

@Bean
@Primary
@ConfigurationProperties(prefix = "app.datasource")
public DataSource dataSource() {
DataSourceBuilder dsb = DataSourceBuilder.create();
if (dsb == null) {
return null;
}
return dsb.build();
}

}

然后在 MainController.java 中的一个方法中有一个 @GetMapping 注释,我调用了 dataSource() 方法。在返回的对象上,我调用了 getConnection(),但这引发了一个 SQLException 消息 The url cannot be null

同样在控制台中,Spring Boot 应用程序写入了一些内容:

Not loading a JDBC driver as driverClassName property is null.

Unable to create initial connections of pool

我错过了什么?我猜 MyConfig.java 中有一些注释?

编辑:您可能还希望看到我的 pom.xml 用于使用 mvn 打包:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-mysql-data</artifactId>
<version>0.1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->

<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>-->

<!-- Use MySQL Connector-J -->

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

最佳答案

似乎没有设置 DataSourceBuilder 的属性。你能尝试在 Spring 中使用默认配置吗:

spring.datasource.url=jdbc:postgresql://localhost:5432/db_example
spring.datasource.username=myusername
spring.datasource.password=mypassword
spring.datasource.driver-class-name=org.postgresql.Driver

这里有更多详细信息:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

然后在您的代码中,您必须获取并设置这些属性:

@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driver-class-name}")
private String driver;

DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName(driver);
dataSourceBuilder.username(username);
dataSourceBuilder.password(password);
dataSourceBuilder.url(url)

希望这对您有所帮助。

关于java - 从使用 Spring 的 DataSourceBuilder 创建的数据源获取到 Postgresql 数据库的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47402278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com