【发布时间】:2019-07-10 13:41:25
【问题描述】:
我对 Java 还很陌生,正在开发 Spring Boot 数据库应用程序 (MySQL),我正在尝试创建一个方法,该方法允许使用 Dao 从我的数据库中删除客户端(Java 对象) /CrudRepository 并按唯一 ID 搜索。运行时收到错误提示“错误:不兼容的类型:int 无法转换为客户端”
我与创建的类似程序进行了比较,在该程序中我使用相同的语法没有问题,所以不确定为什么我在处理时收到此错误。
我的对象是这样设置的: @实体 公共类客户{
@Id
@GeneratedValue
private int id;
@NotNull
@Size(min=1, message = "Must enter client name")
private String name;
@NotNull
@Size(min=1, message= "Must enter contact's name")
private String contact;
@NotNull
@Size(min=1, message="Must enter primary office location")
private String location;
@NotNull(message="Must enter contract start date")
private String startDate;
@NotNull(message="Must enter contract end date")
private String endDate;
@NotNull(message="Must enter employee count")
private Integer employeeCount;
private PhilanthropyInterest interest;
public Client(String name, String contact, String location, String startDate, String endDate, Integer employeeCount) {
this.name = name;
this.contact = contact;
this.location = location;
this.startDate = startDate;
this.endDate = endDate;
this.employeeCount = employeeCount;
}
public Client () { }
public int getId() { return id; }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public Integer getEmployeeCount() {
return employeeCount;
}
public void setEmployeeCount(Integer employeeCount) {
this.employeeCount = employeeCount;
}
public PhilanthropyInterest getInterest() { return interest; }
public void setInterest(PhilanthropyInterest interest) { this.interest = interest; }
我收到错误的控制器方法是:
@RequestMapping(value = "remove", method = RequestMethod.GET)
public String displayRemoveAddClientForm(Model model) {
model.addAttribute("clients", clientDao.findAll());
model.addAttribute("title", "Remove Client");
return "clients/remove";
}
@RequestMapping(value = "remove", method = RequestMethod.POST)
public String processRemoveClientForm(@RequestParam int[] clientIds) {
for (int clientId : clientIds) {
clientDao.delete(clientId);
}
return "redirect:";
}
我的 ClientDao 类是:
@Repository
@Transactional
public interface ClientDao extends CrudRepository<Client, Integer> {
}
IntelliJ 中显示的错误状态:
删除 (org.launchcode.spcdb.models.Client) 在 CrudRepository 中无法应用 到 (整数)
运行时,我收到:
错误:不兼容的类型:int 无法转换为 Client clientDao.delete(clientId);
【问题讨论】:
-
发布你的ClientDao类
-
delete() 方法接受 T 类型的对象,在您的情况下是客户端。不是单个 int 值
标签: java mysql spring-mvc intellij-idea dao