Java 从使用Spring创建的数据源获取到Postgresql数据库的连接;s数据源生成器
我不熟悉弹簧和弹簧靴。我正在尝试在我的Spring Boot应用程序中连接到Postgresql数据库。 我试着从中汲取灵感 及 基本上,我有一个Java 从使用Spring创建的数据源获取到Postgresql数据库的连接;s数据源生成器,java,spring,postgresql,maven,spring-boot,Java,Spring,Postgresql,Maven,Spring Boot,我不熟悉弹簧和弹簧靴。我正在尝试在我的Spring Boot应用程序中连接到Postgresql数据库。 我试着从中汲取灵感 及 基本上,我有一个application.properties文件,其中包含: app.datasource.url=jdbc:postgresql://localhost:5432/db_example app.datasource.username=myusername app.datasource.password=mypassword app.datasou
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
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();
}
}
使用psql
工具,我通常可以使用5432端口和那些用户名和密码连接到本地主机上的db_示例
数据库
然后我有一个MyConfig.java
,它包含:
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
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();
}
}
然后在具有@GetMapping
注释的方法之一MainController.java中,我调用dataSource()
方法。在返回的对象上,我调用getConnection()
,但这会抛出带有消息的SQLException
,url不能为null
在控制台中,Spring Boot应用程序还写入了以下内容:
Not loading a JDBC driver as driverClassName property is null.
及
我错过了什么?我猜是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>
4.0.0
org.springframework
gs mysql数据
0.1.0
org.springframework.boot
spring启动程序父级
1.5.8.1发布
org.springframework.boot
SpringBootStarterWeb
org.postgresql
postgresql
org.springframework.boot
弹簧靴启动器jdbc
org.springframework.boot
弹簧起动试验
测试
1.8
org.springframework.boot
springbootmaven插件
似乎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
更多详情请参见此处:
然后在代码中,您必须获取并设置这些属性:
@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)
希望这有帮助。我想驱动程序类名
应该是应用程序中的驱动程序类名
。属性似乎没有帮助…谢谢Kenny,我会尝试一下,如果有帮助,我会告诉你。