MyBatis小问题-Mapper中错误No constructor found...
前两天又被公司叫去修改其他产品的一些问题了,没有看java相关的,今天周六,看了看MyBatis东西。
就是简单的在MySql中建了个users表,很简单,包含id,name,age,写了个bean。如下:(注,由于初学java,可能很多名词用的并不是特别准确,大概理解一下就行了,待学成之后,再行修改。所谓大行不顾细谨,大礼不拘小节,O(∩_∩)O)
package com.tszhao.dao;
public class User {
</span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> id;</br></br> </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> String name;</br> </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> age;</br></br> </span><span style="color: #0000ff;">public</span> User(<span style="color: #0000ff;">int</span> id, String name, <span style="color: #0000ff;">int</span><span style="color: #000000;"> age) {</br> </span><span style="color: #0000ff;">super</span><span style="color: #000000;">();</br> </span><span style="color: #0000ff;">this</span>.id =<span style="color: #000000;"> id;</br> </span><span style="color: #0000ff;">this</span>.name =<span style="color: #000000;"> name;</br> </span><span style="color: #0000ff;">this</span>.age =<span style="color: #000000;"> age;</br> }</br></br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getId() {</br> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> id;</br> }</br>
public void setId(int id) {
this.id = id;
}
</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String getName() {</br> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> name;</br> }</br></br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> setName(String name) {</br> </span><span style="color: #0000ff;">this</span>.name =<span style="color: #000000;"> name;</br> }</br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> getAge() {</br> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> age;</br> }</br></br> </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> setAge(<span style="color: #0000ff;">int</span><span style="color: #000000;"> age) {</br> </span><span style="color: #0000ff;">this</span>.age =<span style="color: #000000;"> age;</br> }</br></br> @Override</br> </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> String toString() {</br> </span><span style="color: #0000ff;">return</span> "User [id=" + id + ", name=" + name + ", age=" + age + "]"<span style="color: #000000;">;</br> }</br>
}
在JUnit中写了一个测试程序,用来查询。
@Test public 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中增加了一个默认的构造函数,通过。。。
可能问题都不值得一提,但是,特此记录一下学习历程,也是一个很有意思的事。。。