Java 将Spring Boot web app连接到postgresql server
我正在使用Spring Boot制作一个web应用程序,我通过使用内存数据库(H2)实现了我想要的功能,但我无法将它连接到我在计算机上设置的postgresql服务器。我做这件事已经有一段时间了,尝试了一堆不起作用的东西,所以我把一切都恢复到原来的状态,只是为了让它重新工作 这是我的UploadController.java,它处理来自服务器的上传并将其放入内存数据库:Java 将Spring Boot web app连接到postgresql server,java,postgresql,spring-mvc,jpa,spring-boot,Java,Postgresql,Spring Mvc,Jpa,Spring Boot,我正在使用Spring Boot制作一个web应用程序,我通过使用内存数据库(H2)实现了我想要的功能,但我无法将它连接到我在计算机上设置的postgresql服务器。我做这件事已经有一段时间了,尝试了一堆不起作用的东西,所以我把一切都恢复到原来的状态,只是为了让它重新工作 这是我的UploadController.java,它处理来自服务器的上传并将其放入内存数据库: import java.io.BufferedOutputStream; import java.io.File; impor
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import project.service.MediaFile;
import project.service.MediaFileRepository;
@Controller
public class UploadController {
@Autowired
private MediaFileRepository repository;
@RequestMapping(value = "/uploadmedia", method = RequestMethod.GET)
public String uploadForm() {
return "upload";
}
@RequestMapping(value = "/uploadmedia", method = RequestMethod.POST)
public String uploadSubmit(@RequestParam(value="files[]") MultipartFile[] files,
@RequestParam("tags") String tags, @RequestParam("type") String type)
{
String[] tagsArray = tags.split("\\s+");
MultipartFile file;
String name;
String tag;
String path;
for (int i = 0; i < files.length; i++) {
file = files[i];
name = file.getOriginalFilename();
path = "/Users/johannesthorkell/Developer/spring_prufa/images/" + name;
System.out.println(name);
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(path)));
stream.write(bytes);
stream.close();
for (int j = 0; j < tagsArray.length; j++) {
tag = tagsArray[j].toLowerCase();
repository.save(new MediaFile(name, tag, path, type));
}
System.out.println("Success!");
} catch (Exception e) {
System.out.println("Failure... " + e.getMessage());
}
} else {
System.out.println("No file");
}
}
return "upload";
}
}
…这是我的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>HBV501G</groupId>
<artifactId>Spring_Web_MVC</artifactId>
<version>0.1</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在这种设置下,一切正常。我尝试在application.properties中添加以下内容:
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
multipart.maxFileSize=-1
spring.datasource.url=jdbc:postgresql://localhost/test
spring.datasource.username=myusername
spring.datasource.password=mypassword
spring.datasource.driver-class-name=org.postgresql.jdbc.Driver
logging.level. = INFO
myapp.dataSource.driver =
myapp.dataSource.url =
myapp.dataSource.username =
myapp.dataSource.password =
…同时将以下依赖项添加到pom.xml:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1200-jdbc41</version>
</dependency>
…我的pom.xml如下所示:
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
multipart.maxFileSize=-1
debug=true
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=username
spring.datasource.password=password
<?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>HBV501G</groupId>
<artifactId>Spring_Web_MVC</artifactId>
<version>0.1</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1200-jdbc41</version>
</dependency>
<!--<dependency>-->
<!--<groupId>com.h2database</groupId>-->
<!--<artifactId>h2</artifactId>-->
<!--</dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我将GenerationType更改为IDENTITY,并出现以下错误:
2015-10-06 12:56:32.496 DEBUG 22746 --- [nio-8080-exec-7] org.hibernate.SQL : insert into media_file (name, resource, tag, type) values (?, ?, ?, ?)
2015-10-06 12:56:32.505 DEBUG 22746 --- [nio-8080-exec-7] org.hibernate.SQL : insert into media_file (name, resource, tag, type) values (?, ?, ?, ?)
Failure... A different object with the same identifier value was already associated with the session : [project.service.MediaFile#0]; nested exception is javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [project.service.MediaFile#0]
如果我是你,我会创建自己的数据源bean,比如:
@Configuration
public class MyConfig{
@Autowired
Environment env;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("myapp.dataSource.driver"));
dataSource.setUrl(env.getProperty("myapp.dataSource.url"));
dataSource.setUsername(env.getProperty("myapp.dataSource.username"));
dataSource.setPassword(env.getProperty("myapp.dataSource.password"));
return dataSource;
}
}
application.properties:
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
multipart.maxFileSize=-1
spring.datasource.url=jdbc:postgresql://localhost/test
spring.datasource.username=myusername
spring.datasource.password=mypassword
spring.datasource.driver-class-name=org.postgresql.jdbc.Driver
logging.level. = INFO
myapp.dataSource.driver =
myapp.dataSource.url =
myapp.dataSource.username =
myapp.dataSource.password =
如果不想尝试,可以尝试将驱动程序org.postgresql.jdbc.driver更改为org.postgresql.driver
没有任何日志很难提供帮助。错误的关键部分是:
2015-10-06 11:32:49.760 ERROR 22287 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: relation "hibernate_sequence" does not exist
Hibernate正在寻找一个名为Hibernate\u sequence
的表,以支持MediaFile
上的@GeneratedValue
。您目前已将其配置为AUTO
。然后,实际行为会因您使用的数据库而异
我认为你有几个选择:
Create sequence
),名为hibernate\u sequence
GenerationType.IDENTITY
当你尝试使用Postgres时,你看到了什么失败?你不需要指定驱动程序,它是自动检测的。您还应该在启动应用程序时添加“-debug”(或将debug放入
application.properties
)以显示自动配置报告。它将准确地告诉您什么被启用/禁用以及为什么。@StépaneNicoll我做了您建议的更改,现在我的应用程序实际运行,但当我尝试将数据放入数据库时,它仍然失败。我添加了application.properties和pom.xml作为“现在看”以及我在主要帖子中遇到的错误,并且我发布了一个问题的答案,其中包含自动配置报告,因为它不适合我的主要帖子。@AndyWilkinson我用我得到的错误消息更新了我的帖子(在底部).我将生成类型更改为identity,并根据收到的错误消息更改了我的关系,但我遇到了另一个我在OP中输入的错误。我是否生成的id不正确?我认为最好在另一个问题中解决,这与连接Postgres无关。我可以吻你。。。这让我快发疯了,再次感谢你。如果可以,我会给你更多的选票:)