Caused by: java.lang.NoSuchMethodException: com.fendo.entity.Person.()
at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(...)
MyBatis 結果映射時,需透過**無參建構子** (Default Constructor) 實例化實體類,再透過 Setter/欄位注入屬性。若類別**僅定義有參建構子**或**無任何建構子但被標記為 `final`/私有**,則拋出此異常。
// 錯誤:只有有參建構子,編譯器不再自動生成無參建構子
public class Person {
private String name;
private int age;
public Person(String name, int age) { // 唯一建構子
this.name = name;
this.age = age;
}
// Getter/Setter...
}
public class Person {
private String name;
private int age;
public Person() {} // 显式無參建構子
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Getter/Setter...
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
private String name;
private int age;
}
需確保建構子參數名與 `
| 錯誤訊息關鍵字 | 可能原因 | 解決 |
|---|---|---|
| Could not set parameters for mapping | 參數型別不匹配 / 缺少 TypeHandler | 檢查 `#{param, jdbcType=...}`、自訂 TypeHandler |
| Invalid bound statement (not found) | Mapper XML 未掃描 / namespace 錯誤 / id 拼寫 | 檢查 `mapper-locations`、namespace=介面全限定名 |
| Column 'xxx' not found | SQL 查詢欄位與 resultMap/resultType 不符 | 補全 SELECT 列、別名對應、resultMap 映射 |
「NoSuchMethodException: