【问题标题】:Connect to PostgreSQL from SpringBootApplication从 SpringBootApplication 连接到 PostgreSQL
【发布时间】:2016-01-28 10:51:40
【问题描述】:

我正在使用 Maven 和 PostgreSQL 从头开始​​构建一个简单的 Spring 应用程序。

我已经学习了数千个教程,但我不清楚在哪里存储不同的配置以连接和使用 PostgreSQL 数据库。

我的pom.xml 文件:

<?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-rest-service</artifactId>
  <version>0.1.0</version>

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

  <dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
          <groupId>org.postgresql</groupId>
          <artifactId>postgresql</artifactId>
          <version>9.4-1201-jdbc4</version>
      </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>

  <repositories>
      <repository>
          <id>spring-releases</id>
          <url>https://repo.spring.io/libs-release</url>
      </repository>
  </repositories>
  <pluginRepositories>
      <pluginRepository>
          <id>spring-releases</id>
          <url>https://repo.spring.io/libs-release</url>
      </pluginRepository>
  </pluginRepositories>
</project>

我的src/main/java/quotes/Application.java 文件:

package quotes;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

我的src/main/java/quotes/Quote.java 文件:

package quotes;

public class Quote {

    private String content;
    private String author;

    public Quote(String content, String author) {
        this.content = content;
        this.author = author;
    }

    public String getContent() {
        return this.content;
    }

    public Quote setContent(String content) {
        this.content = content;
        return this;
    }

    public String getAuthor() {
        return this.author;
    }

    public Quote setAuthor(String author) {
        this.author = author;
        return this;
    }
}

还有我的src/main/java/quotes/QuoteController.java 文件:

package quotes;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.CrossOrigin;

@Controller
public class QuoteController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @CrossOrigin(origins = "*")
    @RequestMapping("/random")
    public @ResponseBody Quote randomQuote() {
        return new Quote("¿A dónde vas? Patatas traigo", "Ortega y Pacheco");
    }

}

我可以使用 Maven 构建和运行应用程序:

mvn clean package
java -jar target/*.jar

现在的想法是修改QuoteController.java上的代码以连接到PostgreSQL并返回一个随机存储的报价。

您能否提供一些建议/线索?

【问题讨论】:

  • 搜索 Spring Data 教程。
  • 我保证我做到了。我在这里疯了。

标签: java spring postgresql maven


【解决方案1】:

【讨论】:

    【解决方案2】:

    需要在src/main/resources下添加application.properties文件

    对于 PostgreSQL,它应该如下所示:

    spring.jpa.database=POSTGRESQL
    spring.datasource.platform=postgres
    spring.database.driverClassName=org.postgresql.Driver
    spring.datasource.url=jdbc:postgresql://localhost:5432/your-db-name
    spring.datasource.username=username
    spring.datasource.password=password
    

    查看此链接了解更多信息:

    enter link description here

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 2020-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-24
      • 2020-11-06
      • 2012-10-21
      • 1970-01-01
      相关资源
      最近更新 更多