问题描述
- myBatis中的实体类一旦给它构造方法,使用就会报错,为什么
-
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class cn.lyh.dto.Order with invalid types () or values (). Cause: java.lang.NoSuchMethodException: cn.lyh.dto.Order.()
The error may exist in cn/lyh/mapper/OrderMapper.xml
The error may involve cn.lyh.mapper.OrderMapper.getOrder-Inline
The error occurred while setting parameters
SQL: select *from orders where order_id=?
Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class cn.lyh.dto.Order with invalid types () or values (). Cause: java.lang.NoSuchMethodException: cn.lyh.dto.Order.()
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:107) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:98) at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:62) at cn.lyh.test.test.main(test.java:14)
Caused by: org.apache.ibatis.reflection.ReflectionException: Error instantiating class cn.lyh.dto.Order with invalid types () or values (). Cause: java.lang.NoSuchMethodException: cn.lyh.dto.Order.()
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.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:530)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:509)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:329)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:289)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:264)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:234)
at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.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:259)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:132)
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:104)
... 3 more
Caused by: java.lang.NoSuchMethodException: cn.lyh.dto.Order.()
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getDeclaredConstructor(Unknown Source)
at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:57)
... 20 more
解决方案
你的构造方法是有参的吗? 实体类必须有一个无参构造方法,否则java能过反射找不到这个类 所以不能被实例化