我想从我的数据库中更新对象,而不插入新的数据库
这里有两个方法:
@RequestMapping("/edit/{id}")
public ModelAndView showEditProductPage(@PathVariable(name = "id") Integer id, Model model) {
ModelAndView modelAndView = new ModelAndView("edit_customer");
Customer customer = customerService.getCustomer(id);
//Customer customer = customerRepository.getOne(id);
//customer.setCustomerId(customerRepository.getOne(id).getCustomerId());
System.out.println(customer.getCustomerId());
System.out.println(customer.toString());
model.addAttribute("customer", customer);
modelAndView.addObject("customer", customer);
return modelAndView;
}
输出为:id=1 Customer{id=1, firstName='Krzysztof', address='ulica 1', phoneNumber='123456789'}
@RequestMapping("/update/{id}")
public String updateCustomer(@PathVariable(name = "id") Integer id,
@ModelAttribute("customer") Customer customer){
//System.out.println(id);
//customer.setCustomerId(id);
System.out.println("____________________");
System.out.println("customer id " + customer.getCustomerId());
System.out.println("____________________");
customer.setCustomerId(id);
System.out.println("customer id " + customer.getCustomerId());
System.out.println("owner " + customer.getOwner());
System.out.println("address " + customer.getAddress());
System.out.println("phone number " + customer.getAddress());
System.out.println("to string: " + customer.toString());
System.out.println("id " + id);
customerService.save(customer);
return "redirect:/";
}
第二个方法后的输出为:id=1 Customer{id=null, firstName='Krzysztof', address='ulica 1zzz', phoneNumber='123456789'}
客户库扩展JPA库,保存方法:
public void save(Customer customer) {
customerRepository.save(customer);
}
thymeleaf模板:
!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>Edit Customer</title>
<link rel="stylesheet" type="text/css" th:href="@{/table.css}" />
</head>
<body>
<div align="center">
<h1>Edit Customer</h1>
<a href="/">Go Back</a>
<br />
<form action="#" th:action="@{'/update/' + ${id}}" th:object="${customer}"
method="post">
<table border="0" cellpadding="10">
<tr>
<td>Customer ID:</td>
<td>
<input type="text" th:field="*{customerId}" readonly="readonly" />
</td>
</tr>
<tr>
<td>First Name:</td>
<td>
<input type="text" th:field="*{owner}" />
</td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" th:field="*{address}" /></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type="text" th:field="*{phoneNumber}" /></td>
</tr>
<tr>
<td colspan="2"><button type="submit">Save</button> </td>
</tr>
</table>
</form>
</div>
</body>
</html>
客户实体类:
package com.praca.manager.entity;
import javax.persistence.*;
import java.util.List;
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer customerId;
@Column(name = "owner")
private String owner;
@Column(name = "address")
private String address;
@Column(name = "phone_number")
private String phoneNumber;
@OneToMany(mappedBy = "customer")
List<Details> details;
public Customer(){
}
public Customer(Integer customerId,
String owner,
String address,
String phoneNumber){
this.customerId = customerId;
this.owner = owner;
this.address = address;
this.phoneNumber = phoneNumber;
}
public Integer getCustomerId() {
return customerId;
}
public void setCustomerId(Integer id){
this.customerId = customerId;
}
public String getOwner() {
return owner;
}
public void setOwner(String firstName) {
this.owner = firstName;
}
public String getAddress() {
return address;
}
public void setAddress(String address){
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Override
public String toString() {
return "Customer{" +
"id=" + customerId +
", firstName='" + owner + ''' +
", address='" + address + ''' +
", phoneNumber='" + phoneNumber + ''' +
'}';
}
}
这里ID字段也是'1'sql脚本:
create table customer(
customer_id int auto_increment primary key not null,
owner varchar(200) not null,
address varchar(200) not null,
phone_number int not null
);
尽管我尝试将id设置为1或任何其他数字,但它仍然为空。谁能告诉我为什么和如何解决它?
问题出在Customer类的setter中:
public void setCustomerId(Integer id){
this.customerId = customerId;
}
你把字段赋给了它自己,这就是为什么它总是空的。应该是这个。customerId = id":)。