我正在学习Spring Boot,并且正在使用jdbcTemplate执行CRUD操作的项目。
每当我在查询中引入getObject()参数时,就会显示异常。 IDE建议添加异常或用try catch包围;两者都导致错误
"Incompatible types
Expected: java.lang.throwable
Found:org.springframework.dao.dataaccessexception"
这是我的服务方法:
import com.vaidiksanatansewa.gurusewa.model.Appointment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class AppointmentService {
private JdbcTemplate jdbcTemplate;
public void add(Appointment appointment) throws DataAccessException {
jdbcTemplate.update("insert into appointment(name, address, contact,
sewa_fid, guru_fid,date,
hour , status, created_date, updated_date)
values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
appointment.getName(),appointment.getAddress(),appointment.getPhone(),
appointment.getSewa_fid(),appointment.getGuru_fid(),
appointment.getDate(), appointment.getHour(),appointment.getStatus(),
appointment.getCreated_date(),appointment.getUpdated_date());
}
}
继承人模型:
package com.vaidiksanatansewa.gurusewa.model;
import java.util.Date;
public class Appointment {
Long id;
String name;
String address;
String phone;
Integer sewa_fid;
Integer guru_fid;
Date date;
Integer hour;
Integer status;
Date created_date;
Date updated_date;
public Appointment() {
}
public Appointment(Long id, String name, String address, String phone, Integer sewa_fid, Integer guru_fid, Date date, Integer hour, Integer status, Date created_date, Date updated_date) {
this.id = id;
this.name = name;
this.address = address;
this.phone = phone;
this.sewa_fid = sewa_fid;
this.guru_fid = guru_fid;
this.date = date;
this.hour = hour;
this.status = status;
this.created_date = created_date;
this.updated_date = updated_date;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getSewa_fid() {
return sewa_fid;
}
public void setSewa_fid(Integer sewa_fid) {
this.sewa_fid = sewa_fid;
}
public Integer getGuru_fid() {
return guru_fid;
}
public void setGuru_fid(Integer guru_fid) {
this.guru_fid = guru_fid;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Integer getHour() {
return hour;
}
public void setHour(Integer hour) {
this.hour = hour;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Date getCreated_date() {
return created_date;
}
public void setCreated_date(Date created_date) {
this.created_date = created_date;
}
public Date getUpdated_date() {
return updated_date;
}
public void setUpdated_date(Date updated_date) {
this.updated_date = updated_date;
}
}
和控制器:
package com.vaidiksanatansewa.gurusewa.controller;
import com.vaidiksanatansewa.gurusewa.model.Appointment;
import com.vaidiksanatansewa.gurusewa.service.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class AppointmentController {
@Autowired
AppointmentService appointmentService;
@RequestMapping("/Appointment")
public List<Appointment> getAll() {
return appointmentService.getAll();
}
@RequestMapping("/Appointment/{id}")
public Appointment getById(@PathVariable Long id) {
return appointmentService.getById(id);
}
@RequestMapping(method = RequestMethod.POST, value= "/student")
public void add(@RequestBody Appointment Appointment) {
appointmentService.add(Appointment);
}
@RequestMapping(method = RequestMethod.PUT, value= "/student/{id}")
public void markAsAccepted(@RequestBody Appointment Appointment,@PathVariable Long id){
appointmentService.markAsAccepted(id,Appointment);
}
}
会是什么原因呢?任何人都可以阐明这一点吗?
最佳答案
错误很奇怪,可能是项目状态不佳。尝试构建项目(Maven构建或安装),并验证在已编译的战争中错误仍然存在(并且相同)。
关于java - 无法克服此org.springframework.dao.dataaccessexception,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50583092/