从今天早上开始搜索SO,但我找不到这个问题的正确答案,我正在使用ID作为参考对记录进行更新,而不是更新应用程序插入新的输入现有记录,控制台显示"插入"而不是"更新"。这里是应用程序的片段:
控制器:
@PostMapping("/saveCustomer")
public String saveCustomer(@ModelAttribute("customer") Customer customer) {
customerService.saveCustomer(customer);
return "redirect:/erp/list";
}
@GetMapping("/showFormForUpdate")
public String showFormForUpdate(@RequestParam("customerId") int id, Model model) {
Customer customer = customerService.getCustomer(id);
model.addAttribute("customer", customer);
return "customer-form";
}
DAO实施:
@Override
public void saveCustomer(Customer customer) {
Session currentSession = sessionFactory.getCurrentSession();
currentSession.saveOrUpdate(customer);
}
实体:
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="company_name")
private String companyName;
@Column(name="company_email")
private String companyEmail;
@Column(name="company_address")
private String companyAddress;
@Column(name="contact_no")
private String contactNo;
@Column(name="company_tin")
private String companyTin;
public Customer() {
}
public String getContactNo() {
return contactNo;
}
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getCompanyEmail() {
return companyEmail;
}
public void setCompanyEmail(String companyEmail) {
this.companyEmail = companyEmail;
}
public String getCompanyAddress() {
return companyAddress;
}
public void setCompanyAddress(String companyAddress) {
this.companyAddress = companyAddress;
}
public String getCompanyTin() {
return companyTin;
}
public void setCompanyTin(String companyTin) {
this.companyTin = companyTin;
}
public int getId() {
return id;
}
答案 0 :(得分:0)
我认为你缺少bean中的setter方法:
public int setId(int id) {
this.id = id;
}
这就是为什么当你添加模型属性时,它没有在你的bean中设置id值。