mybatis多对一测试时报错argument type mismatch
测试环境:
mybatis+lombok
Teacher和Student使用lombok的@AllArgsConstructor
报错信息:
1 org.apache.ibatis.exceptions.PersistenceException: 2 ### Error querying database. Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.szgl.pojo.StudentPojo with invalid types (Integer,String,TeacherPojo) or values (2,小红,1). Cause: java.lang.IllegalArgumentException: argument type mismatch 3 ### The error may exist in com/szgl/dao/StudentMapper.xml 4 ### The error may involve com.szgl.dao.StudentMapper.getAllStudent 5 ### The error occurred while handling results 6 ### SQL: select * from tbl_student 7 ### Cause: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.szgl.pojo.StudentPojo with invalid types (Integer,String,TeacherPojo) or values (2,小红,1). Cause: java.lang.IllegalArgumentException: argument type mismatch 8 9 at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) 10 at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149) 11 at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140) 12 at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147) 13 at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80) 14 at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:144) 15 at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85) 16 at com.sun.proxy.$Proxy7.getAllStudent(Unknown Source) 17 at TestDemo.testFunc04(TestDemo.java:60) 18 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 19 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 20 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 21 at java.lang.reflect.Method.invoke(Method.java:498) 22 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 23 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 24 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 25 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 26 at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 27 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 28 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 29 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 30 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 31 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 32 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 33 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 34 at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 35 at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 36 at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 37 at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) 38 at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) 39 at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58) 40 Caused by: org.apache.ibatis.reflection.ReflectionException: Error instantiating class com.szgl.pojo.StudentPojo with invalid types (Integer,String,TeacherPojo) or values (2,小红,1). Cause: java.lang.IllegalArgumentException: argument type mismatch 41 at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:88) 42 at org.apache.ibatis.reflection.factory.DefaultObjectFactory.create(DefaultObjectFactory.java:53) 43 at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createUsingConstructor(DefaultResultSetHandler.java:676) 44 at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createByConstructorSignature(DefaultResultSetHandler.java:654) 45 at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:618) 46 at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.createResultObject(DefaultResultSetHandler.java:591) 47 at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.getRowValue(DefaultResultSetHandler.java:397) 48 at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValuesForSimpleResultMap(DefaultResultSetHandler.java:354) 49 at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleRowValues(DefaultResultSetHandler.java:328) 50 at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSet(DefaultResultSetHandler.java:301) 51 at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:194) 52 at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:65) 53 at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79) 54 at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63) 55 at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324) 56 at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) 57 at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) 58 at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83) 59 at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147) 60 ... 29 more 61 Caused by: java.lang.IllegalArgumentException: argument type mismatch 62 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 63 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 64 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 65 at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 66 at org.apache.ibatis.reflection.factory.DefaultObjectFactory.instantiateClass(DefaultObjectFactory.java:74) 67 ... 47 more
解决方法:
在pojo类上添加@NoArgsConstructor添加空参构造即可
NoArgsConstructor