MyBatis小问题(1)-Mapper中错误No constructor found...

前两天又被公司叫去修改其他产品的一些问题了,没有看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;
    
    public User(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 void setName(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中写了一个测试程序,用来查询。

复制代码
@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中增加了一个默认的构造函数,通过。。。

  可能问题都不值得一提,但是,特此记录一下学习历程,也是一个很有意思的事。。。

你可能感兴趣的:(mybatis)