今天使用Mybatis连接数据库时报出了java.lang.NoSuchMethodException 的错误,错误信息如下,
错误提示,出错地点是Player的BEAN类实例化出了错,Player代码如下
public class Player {
private int pid;
private String pname;
private String position;
private int cid;
private Club club;//多对一关系
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public Club getClub() {
return club;
}
public void setClub(Club club) {
this.club = club;
}
public Player(int pid, String pname, String position, int cid, Club club) {
super();
this.pid = pid;
this.pname = pname;
this.position = position;
this.cid = cid;
this.club = club;
}
@Override
public String toString() {
return "Player [pid=" + pid + ", pname=" + pname + ", position=" + position + ", cid=" + cid + "]";
}
}
最后发现是我在代码最后写了个有参构造,重载了构造函数,而Mybatis在load进一个bean类时,需要无参构造从而导致了该错误,
解决方法,给代码加上午参构造即可!
public Player() {
super();
}