mybatis遇见错误:
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.ding.test.User with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.ding.test.User.<init>() ### The error may exist in com/ding/test/userMapper.xml ### The error may involve com.ding.test.User.userMapper.getUser-Inline ### The error occurred while setting parameters ### SQL: select * from users where id=? ### Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.ding.test.User with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.ding.test.User.<init>() at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:104) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:95) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:59) at com.ding.test.Test.main(Test.java:45) Caused by: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.ding.test.User with invalid types () or values (). Cause: java.lang.NoSuchMethodException: com.ding.test.User.<init>() at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:83) at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:45) at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:38) at org.apache.ibatis.executor.resultset.FastResultSetHandler.createResultObject(FastResultSetHandler.java:376) at org.apache.ibatis.executor.resultset.FastResultSetHandler.createResultObject(FastResultSetHandler.java:355) at org.apache.ibatis.executor.resultset.FastResultSetHandler.getRowValue(FastResultSetHandler.java:255) at org.apache.ibatis.executor.resultset.FastResultSetHandler.handleRowValues(FastResultSetHandler.java:214) at org.apache.ibatis.executor.resultset.FastResultSetHandler.handleResultSet(FastResultSetHandler.java:186) at org.apache.ibatis.executor.resultset.FastResultSetHandler.handleResultSets(FastResultSetHandler.java:152) at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:57) at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:70) at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:57) at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:267) at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:141) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:101) ... 3 more Caused by: java.lang.NoSuchMethodException: com.ding.test.User.<init>() at java.lang.Class.getConstructor0(Class.java:2892) at java.lang.Class.getDeclaredConstructor(Class.java:2058) at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:57) ... 19 more
1) model中的get/set方法与成员变量不一。
2) 构造函数被重载过,但是没有空的构造函数。
3) 最好不要使用简单类型,如int, long等,改用对象模式Integer, Long等。在写条件查询时会用到判断<if xxx != null > … </if>的