【发布时间】:2021-03-02 22:36:57
【问题描述】:
我正在尝试根据https://javainspires.blogspot.com/2020/07/spring-boot-2-spring-data-jdbc-jdbc.html 上的教程将数据库从 MySQL 更改为 PostgreSQL。我经常遇到标题中显示的错误。
Employee.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>
Application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/database
spring.datasource.username=postgres
spring.datasource.password=admin
spring.datasource.driver-class-name=org.postgresql.Driver
schema.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>
【问题讨论】:
标签: java spring postgresql spring-boot