Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从使用Spring创建的数据源获取到Postgresql数据库的连接;s数据源生成器_Java_Spring_Postgresql_Maven_Spring Boot - Fatal编程技术网

Java 从使用Spring创建的数据源获取到Postgresql数据库的连接;s数据源生成器

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

我不熟悉弹簧和弹簧靴。我正在尝试在我的Spring Boot应用程序中连接到Postgresql数据库。 我试着从中汲取灵感

基本上,我有一个
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,我会尝试一下,如果有帮助,我会告诉你。