在弄springboot整合Mybatis时遇到了下面的异常:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: No constructor found in com.example.demo.entity.User matching [java.lang.Long, java.sql.Timestamp, java.lang.String]
意思说在com.example.demo.entity.User里没有匹配的构造函数
我的User类代码:
import java.util.Date;
public class User implements Serializable {
private static final Long serialVersionUID=8655851615465363473L;
private Long id;
private String userName;
private Date date;
public User(String userName, Date date) {
this.userName = userName;
this.date = date;
}
public User(Long id, String userNam, Date date) {
this.id=id;
this.userName = userName;
this.date = date;
}
解决方法:
1,把java.util.Date换为java.sql.Date,就是把构造函数改成相匹配的类型
import java.sql.Date;
public class User implements Serializable {
private static final Long serialVersionUID=8655851615465363473L;
private Long id;
private String userName;
private Date date;
public User(String userName, Date date) {
this.userName = userName;
this.date = date;
}
public User(Long id, String userNam, Date date) {
this.id=id;
this.userName = userName;
this.date = date;
}
2,添加一个无参构造函数;
public class User implements Serializable {
private static final Long serialVersionUID=8655851615465363473L;
private Long id;
private String userName;
private Date date;
public User() {
}
public User(String userName, Date date) {
this.userName = userName;
this.date = date;
}
public User(Long id, String userNam, Date date) {
this.id=id;
this.userName = userName;
this.date = date;
}