java - org.postgresql.util.PSQLException:错误:关系“员工”不存在位置:13
问题描述
我正在尝试根据https://javainspires.blogspot.com/2020/07/spring-boot-2-spring-data-jdbc-jdbc.html上给出的教程将数据库从 MySQL 更改为 PostgreSQL 。我经常遇到标题中显示的错误。
雇员.java
package com. example.demo;
import javax.persistence.Entity;
import javax.persistence.Table;
public class Employee {
private String username;
private String email;
private String password;
public Employee() {
super();
// TODO Auto-generated constructor stub
}
public Employee(String username, String email, String password) {
super();
this.username = username;
this.email = email;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
员工控制器
package com. example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class EmployeeController {
@Autowired
JdbcTemplate jdbcTemplate;
@PostMapping(path = "addUser")
public String addUser(Employee user) {
String insert_query = "INSERT INTO employee (username,email,password) VALUES(?,?,?)";
int rows = jdbcTemplate.update(insert_query,
user.getUsername(),
user.getEmail(),
user.getPassword());
if(rows == 1) {
return "success";
} else {
return "error";
}
}
}
Html 文件:index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Java Inspires</title>
</head>
<body>
<form th:action="@{/addUser}" th:object="${user}" method="post" >
<p>User Name :<input type="text" name="username" /></p>
<p>Email :<input type="email" name="email" /></p>
<p>Password :<input type="password" name="password" /></p>
<p><input type="submit" value="Sign Up" /></p>
</form>
</body>
</html>
应用程序属性
spring.datasource.url=jdbc:postgresql://localhost:5432/database
spring.datasource.username=postgres
spring.datasource.password=admin
spring.datasource.driver-class-name=org.postgresql.Driver
架构.sql
CREATE TABLE employee
(
username varchar(100) NOT NULL,
email varchar(100) NOT NULL,
password varchar(100) NOT NULL
);
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>JDBC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JDBC</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</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-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
解决方案
我删除了 schema.sql 文件并在 PgAdmin4 中手动创建了它。上面的代码在那之后工作了。
推荐阅读
- javascript - 过滤 JavaScript 数组中大于零的元素
- python - 如何评估 YAML 中定义的代数表达式?
- python-2.7 - 使用 CTE 在 Redshift 中的 Python UDF 比直接选择查询快
- javascript - 无法从段落中的脚本中获取值()
- javascript - 如何在 react-native-navigation 中将背景图像添加到导航器
- php - laravel 5.6中增加count的值
- apache-kafka - 在附加新主题的消费者之前,我在 apache kafka 中创建新主题并生成消息
- jquery - firebase 迭代子节点并获取他们的快照网络
- javascript - Highchart 上的图像
- javascript - ngForm.controls 以角度打印未定义