今天使用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();
}