6.mybatis的javabean;
今天使用mybatis时候无端报错,找了半天也没找出来:
"C:Program FilesJavajdk1.8.0_144injava" "-javaagent:D:IntelliJ IDEA 2017.1.3libidea_rt.jar=54835:D:IntelliJ IDEA 2017.1.3in" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_144jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_144jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_144jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_144jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_144jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_144jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_144jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_144jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_144jrelibext ashorn.jar;C:Program FilesJavajdk1.8.0_144jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_144jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_144jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_144jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_144jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_144jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_144jrelibjce.jar;C:Program FilesJavajdk1.8.0_144jrelibjfr.jar;C:Program FilesJavajdk1.8.0_144jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_144jrelibjsse.jar;C:Program FilesJavajdk1.8.0_144jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_144jrelibplugin.jar;C:Program FilesJavajdk1.8.0_144jrelib esources.jar;C:Program FilesJavajdk1.8.0_144jrelib t.jar;E:IDEAMybatis01 arget est-classes;E:IDEAMybatis01 argetclasses;D:IntelliJ IDEA 2017.1.3libjunit-4.12.jar;D:IntelliJ IDEA 2017.1.3libhamcrest-core-1.3.jar;E:Java epojunitjunit4.10junit-4.10.jar;E:Java epoorghamcresthamcrest-core1.1hamcrest-core-1.1.jar;E:Java epoorgmybatismybatis3.4.4mybatis-3.4.4.jar;E:Java epocomfasterxmljacksoncorejackson-core2.2.1jackson-core-2.2.1.jar;E:Java epocomfasterxmljacksoncorejackson-databind2.2.1jackson-databind-2.2.1.jar;E:Java epocomfasterxmljacksoncorejackson-annotations2.2.1jackson-annotations-2.2.1.jar;E:Java epoorgjunitjupiterjunit-jupiter-api5.0.0-RC2junit-jupiter-api-5.0.0-RC2.jar;E:Java epoorgopentest4jopentest4j1.0.0-RC1opentest4j-1.0.0-RC1.jar;E:Java epoorgjunitplatformjunit-platform-commons1.0.0-RC2junit-platform-commons-1.0.0-RC2.jar;E:Java epomysqlmysql-connector-java5.1.43mysql-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; } }