MyBatis小問題-Mapper中錯誤No constructor found...
阿新 • • 發佈:2018-02-11
copy sys pan wrap @override 增加 exist pub best
前兩天又被公司叫去修改其他產品的一些問題了,沒有看java相關的,今天周六,看了看MyBatis東西。
就是簡單的在MySql中建了個users表,很簡單,包含id,name,age,寫了個bean。如下:(註,由於初學java,可能很多名詞用的並不是特別準確,大概理解一下就行了,待學成之後,再行修改。所謂大行不顧細謹,大禮不拘小節,O(∩_∩)O)
package com.tszhao.dao;
public class User {
private int id;
private String name;
private int age;
publicUser(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public voidsetName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
在JUnit中寫了一個測試程序,用來查詢。
@Testpublic void testSelect() {
SqlSessionFactory factory = MyBatisUtils.getFactory();
SqlSession openSession = factory.openSession();
UserMapper mapper = openSession.getMapper(UserMapper.class);
User selectUser = mapper.selectUser(1);
System.out.println(selectUser);
}
結果,運行報錯。。。
org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in com.tszhao.dao.User matching [java.lang.Integer, java.lang.String, java.lang.Integer]
### The error may exist in com/tszhao/mapper/UserMapper.java (best guess)
### The error may involve com.tszhao.mapper.UserMapper.selectUser
### The error occurred while handling results
### SQL: select * from users where id=?
### Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in com.tszhao.dao.User matching [java.lang.Integer, java.lang.String, java.lang.Integer]
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
...
看樣子,應該跟構造函數相關。找不到與User相關的構造函數。試著在User中增加了一個默認的構造函數,通過。。。
可能問題都不值得一提,但是,特此記錄一下學習歷程,也是一個很有意思的事。。。
MyBatis小問題-Mapper中錯誤No constructor found...