- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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/
我在 application.properties 文件中定义了以下属性。 sample.db.serverName=10.10.10.10 sample.db.serverPort=1234 sam
我是 Spring 和 Spring Boot 的新手。我正在尝试在我的 Spring Boot 应用程序中连接到 Postgresql 数据库。我试图从中汲取灵感 http://spring.io/
我们正在升级我们现有的 Spring Boot (1.5)申请到2.0.0 . 我们连接多个数据库并使用 org.springframework.boot.autoconfigure.jdbc.Dat
我有一个在 Eclipse(STS) 中设置的项目。 在 eclipse 中使用 junit 运行 spring 集成测试时,spring 上下文正确启动并且测试正确执行。 当尝试从 Maven 运行
我是一名优秀的程序员,十分优秀!