我正在使用Maven和PostgreSQL从头开始构建一个简单的Spring应用程序。
我一直在关注数千个教程,但我不清楚在哪里存储不同的配置来连接和使用PostgreSQL数据库。
我的<?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");
}
}
文件:
mvn clean package
java -jar target/*.jar
我可以使用Maven构建和运行应用程序:
QuoteController.java
现在的想法是修改.popover-navigation *[data-role="next"]{
background-color:#c6c6c6;
}
上的代码以连接到PostgreSQL并返回随机存储的引用。
你能提出一些建议/线索吗?
答案 0 :(得分:2)
答案 1 :(得分:1)
您需要在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
查看此链接以获取更多信息: