首页 > 代码库 > 6.mybatis的javabean

6.mybatis的javabean

6.mybatis的javabean

今天使用mybatis时候无端报错,找了半天也没找出来:

"C:\Program Files\Java\jdk1.8.0_144\bin\java" "-javaagent:D:\IntelliJ IDEA 2017.1.3\lib\idea_rt.jar=54835:D:\IntelliJ IDEA 2017.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\rt.jar;E:\IDEA\Mybatis01\target\test-classes;E:\IDEA\Mybatis01\target\classes;D:\IntelliJ IDEA 2017.1.3\lib\junit-4.12.jar;D:\IntelliJ IDEA 2017.1.3\lib\hamcrest-core-1.3.jar;E:\Java\repo\junit\junit\4.10\junit-4.10.jar;E:\Java\repo\org\hamcrest\hamcrest-core\1.1\hamcrest-core-1.1.jar;E:\Java\repo\org\mybatis\mybatis\3.4.4\mybatis-3.4.4.jar;E:\Java\repo\com\fasterxml\jackson\core\jackson-core\2.2.1\jackson-core-2.2.1.jar;E:\Java\repo\com\fasterxml\jackson\core\jackson-databind\2.2.1\jackson-databind-2.2.1.jar;E:\Java\repo\com\fasterxml\jackson\core\jackson-annotations\2.2.1\jackson-annotations-2.2.1.jar;E:\Java\repo\org\junit\jupiter\junit-jupiter-api\5.0.0-RC2\junit-jupiter-api-5.0.0-RC2.jar;E:\Java\repo\org\opentest4j\opentest4j\1.0.0-RC1\opentest4j-1.0.0-RC1.jar;E:\Java\repo\org\junit\platform\junit-platform-commons\1.0.0-RC2\junit-platform-commons-1.0.0-RC2.jar;E:\Java\repo\mysql\mysql-connector-java\5.1.43\mysql-connector-java-5.1.43.jar" test6
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in entity.Person matching [java.lang.Long, java.lang.String, java.lang.Integer, java.lang.String]
### The error may exist in mappers/PersonMapper.xml
### The error may involve Person.getPersonList
### The error occurred while handling results
### SQL: SELECT id, name, age, remark FROM person LIMIT ?, ?
### Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in entity.Person matching [java.lang.Long, java.lang.String, java.lang.Integer, java.lang.String]
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
    at dao.PersonDao.getPersonList(PersonDao.java:79)
    at test6.main(test6.java:15)
Caused by: org.apache.ibatis.executor.ExecutorException: No constructor found in entity.Person matching [java.lang.Long, java.lang.String, java.lang.Integer, java.lang.String]
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createByConstructorSignature(DefaultResultSetHandler.java:660)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:613)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:586)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:388)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:347)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:322)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:295)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:192)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
    at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
    ... 3 more
null
null
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in entity.Person matching [java.lang.Long, java.lang.String, java.lang.Integer, java.lang.String]
### The error may exist in mappers/PersonMapper.xml
### The error may involve Person.getPersonList
### The error occurred while handling results
### SQL: SELECT id, name, age, remark FROM person LIMIT ?, ?
### Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in entity.Person matching [java.lang.Long, java.lang.String, java.lang.Integer, java.lang.String]
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
    at dao.PersonDao.getPersonList(PersonDao.java:79)
    at test6.main(test6.java:17)
Caused by: org.apache.ibatis.executor.ExecutorException: No constructor found in entity.Person matching [java.lang.Long, java.lang.String, java.lang.Integer, java.lang.String]
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createByConstructorSignature(DefaultResultSetHandler.java:660)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:613)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:586)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:388)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:347)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:322)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:295)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:192)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
    at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
    ... 3 more
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in entity.Person matching [java.lang.Long, java.lang.String, java.lang.Integer, java.lang.String]
### The error may exist in mappers/PersonMapper.xml
### The error may involve Person.getPersonList
### The error occurred while handling results
### SQL: SELECT id, name, age, remark FROM person LIMIT ?, ?
### Cause: org.apache.ibatis.executor.ExecutorException: No constructor found in entity.Person matching [java.lang.Long, java.lang.String, java.lang.Integer, java.lang.String]
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
    at dao.PersonDao.getPersonList(PersonDao.java:79)
    at test6.main(test6.java:19)
Caused by: org.apache.ibatis.executor.ExecutorException: No constructor found in entity.Person matching [java.lang.Long, java.lang.String, java.lang.Integer, java.lang.String]
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createByConstructorSignature(DefaultResultSetHandler.java:660)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:613)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:586)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:388)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:347)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:322)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:295)
    at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:192)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:64)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
    at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
    at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
    at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
    at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
    ... 3 more
null

Process finished with exit code 0

后来才知道是我在javabean中写了自定义的构造函数,这样就会导致覆盖默认的无参的构造函数。而mybatis查询之后将结果对应在javabean时会调用默认的构造函数,这时候就会找不到了。解决办法就是在定义了自定义的构造函数后需将默认的无参构造函数显性的写出来即可。

package entity;

/**
 * Created by Administrator on 2017/8/5.
 * Person的实体类
 */
public class Person {
    private Integer id;
    private String name;
    private Integer age;
    private String remark;

    /*mybatis对应的javabean中必须得有一个默认无参的构造函数,
    * 如果我们自定义了有参的构造函数时,会将其覆盖,执行查询时,
    *mybatis无法将参数存入,所以要将其显性的写出来
    */
    public Person () {
        
    }

    //全参数的构造函数
    public Person (Integer id, String name, Integer age,String  remark) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
        this.remark = remark;
    }

    //不含id的构造函数
    public Person (String name, Integer age,String  remark) {
        super();
        this.name = name;
        this.age = age;
        this.remark = remark;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}

 

6.mybatis的javabean