(1) mapper接口类和mapper.xml是否在同一个包下,文件名称是否一致(仅后缀不同):经过实际检验,这个并不需要在同一个包下面,通常mapper.xml文件是存放在src/main/resources目录下,而mapper接口类是在src/main/java目录下面;名称是否相同也不重要,我的命名分贝为NumberDao.java和NumberMapper.xml,实际也可以运行成功
(2)mapper.xml的namespace是否是对应接口类的全名(包括包名和类名):这个是必须要保证相同,需要进行检查,而且全类名最好通过复制,不要自己手动拼写,容易出错
(3)mapper接口类的方法名是否与mapper.xml中sql标签的id相同:这个也需要进行检查,必须保证一致
(4)mapper.xml文件中用resultMap,而不用resultType(当sql操作结果是List或其他复杂结果集时):我经过实践检验,我的查询结果是一个Integer的List集合,用resultMap反而报错,异常信息如下:
org.apache.ibatis.builder.IncompleteElementException: Could not find result map java.lang.Integer
at org.apache.ibatis.builder.MapperBuilderAssistant.getStatementResultMaps(MapperBuilderAssistant.java:346)
at org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(MapperBuilderAssistant.java:290)
at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:109)
at org.apache.ibatis.session.Configuration.buildAllStatements(Configuration.java:788)
at org.apache.ibatis.session.Configuration.hasStatement(Configuration.java:758)
at org.apache.ibatis.session.Configuration.hasStatement(Configuration.java:753)
at org.apache.ibatis.binding.MapperMethod$SqlCommand.resolveMappedStatement(MapperMethod.java:247)
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:217)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48)
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)
at com.sun.proxy.$Proxy99.qryLockNumber(Unknown Source)
at org.NumberRecovery.service.RecoveryService.qryLockNumber(RecoveryService.java:23)
at org.NumberRecovery.service.RecoveryService$$FastClassBySpringCGLIB$$dc43da81.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at org.NumberRecovery.service.RecoveryService$$EnhancerBySpringCGLIB$$a9c68bd2.qryLockNumber(<generated>)
at org.NumberRecovery.controller.RecoveryController.updateLockNumber(RecoveryController.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain value for java.lang.Integer
at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:888)
at org.apache.ibatis.session.Configuration.getResultMap(Configuration.java:640)
at org.apache.ibatis.builder.MapperBuilderAssistant.getStatementResultMaps(MapperBuilderAssistant.java:344)
... 36 common frames omitted
(5) 到target目录下看是否有mapper.xml文件生成(按照自己对mapper.xml文件的路径配置到target下classes目录下找),如果没有可以在pom.xml文件的<build></build>
之间添加
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
也可以再在<resources></resources>
之间再添加一项
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
使mapper.xml文件不论是放在src/main/resources
或src/main/java
下都可以被打包到classes目录下。