我正在尝试使用@OneToOne向我的2个表中添加2个值``名称''和``城市''。我想我在@Controller上写了不完整的代码,在Thymeleaf代码上写错了。帮我 。非常感谢
Student.java
package com.example.demo.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "student_id")
private long studentId;
@Column(name = "name")
private String name;
@OneToOne
@JoinColumn(name = "home_address_id")
private Address address;
public long getStudentId() {
return studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Address.java
package com.example.demo.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "address")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "address_id")
private long addressId;
@Column(name = "city")
private String city;
public long getAddressId() {
return addressId;
}
public void setAddressId(long addressId) {
this.addressId = addressId;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
StudentRepository.java
package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.models.Student;
public interface StudentRepository extends JpaRepository<Student, Long> {
Student findByName (String name);
}
StudentService.java
package com.example.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.models.Student;
import com.example.demo.repository.StudentRepository;
@Service
public class StudentService {
@Autowired
private StudentRepository studentRepository;
public List<Student> findAll (){
return studentRepository.findAll();
}
public Student findByName (String name) {
return studentRepository.findByName(name);
}
public Student save (Student student) {
return studentRepository.save(student);
}
}
StudentController.Java
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import com.example.demo.models.Student;
import com.example.demo.service.StudentService;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/all")
public ModelAndView getAll (ModelAndView modelAndView, Student student) {
modelAndView.addObject("student", studentService.findAll());
modelAndView.setViewName("all");
return modelAndView;
}
@GetMapping("/add")
public ModelAndView add (ModelAndView modelAndView, Student student) {
modelAndView.addObject("add", student);
modelAndView.setViewName("add");
return modelAndView;
}
@PostMapping("/add")
public ModelAndView postAdd (ModelAndView modelAndView, Student student) {
Student existStudent= studentService.findByName(student.getName());
if (existStudent!=null) {
modelAndView.setViewName("add");
}
else {
studentService.save(student);
modelAndView.setViewName("success");
}
return modelAndView;
}
}
add.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Add</title>
</head>
<body>
<center>
<form action="#" th:action="@{/add}" th:object="${add}" method="post">
<table>
<tr>
<td><label for="name">Name</label></td>
<td><input th:field="*{name}" type="text" name="name"></input></td>
</tr>
<tr>
<td><label for="city">City</label></td>
<td><input th:field="*{address.city}" type="text" name="address.city"></input>
</td>
</tr>
<tr>
<td><input type="submit" value="Submit"></input></td>
</tr>
</table>
</form>
</center>
</body>
</html>
student.sql
create table student (
student_id BIGINT NOT NULL AUTO_INCREMENT,
home_address_id BIGINT NOT NULL,
name VARCHAR(30) NOT NULL,
PRIMARY KEY (student_id),
CONSTRAINT student_address FOREIGN KEY (home_address_id) REFERENCES ADDRESS ( address_id)
);
Address.sql
create table ADDRESS (
address_id BIGINT NOT NULL AUTO_INCREMENT,
city VARCHAR(30) NOT NULL,
PRIMARY KEY (address_id)
);
我认为我错了,可能写得不完整。如果可能,重写我需要修复或添加的代码段。非常感谢