解决找不到mapper.xml文件的问题
Posted hzdzkjdxygz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决找不到mapper.xml文件的问题相关的知识,希望对你有一定的参考价值。
先看问题长啥样吧!下面是通过逆向工程生成的Mapper.xml文件,包路径什么的都没有错!
但是每次调用Mapper中的方法的操作数据库的时候都失败:
贴出来详细的报错信息:
1 三月 19, 2017 10:16:45 下午 org.apache.catalina.core.ApplicationContext log 2 信息: No Spring WebApplicationInitializer types detected on classpath 3 三月 19, 2017 10:16:45 下午 org.apache.catalina.core.ApplicationContext log 4 信息: Initializing Spring root WebApplicationContext 5 三月 19, 2017 10:16:48 下午 org.apache.catalina.core.ApplicationContext log 6 信息: Initializing Spring FrameworkServlet \'xuebusi-manager\' 7 三月 19, 2017 10:20:40 下午 org.apache.catalina.core.StandardWrapperValve invoke 8 严重: Servlet.service() for servlet [xuebusi-manager] in context with path [] threw exception [Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xuebusi.mapper.TbUserMapper.selectByExample] with root cause 9 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.xuebusi.mapper.TbUserMapper.selectByExample 10 at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:189) 11 at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:43) 12 at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:58) 13 at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:51) 14 at com.sun.proxy.$Proxy19.selectByExample(Unknown Source) 15 at com.xuebusi.service.impl.UserServiceImpl.getUserById(UserServiceImpl.java:66) 16 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 17 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 18 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 19 at java.lang.reflect.Method.invoke(Method.java:606) 20 at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317) 21 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 22 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 23 at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) 24 at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:267) 25 at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) 26 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 27 at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) 28 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 29 at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 30 at com.sun.proxy.$Proxy20.getUserById(Unknown Source) 31 at com.xuebusi.controller.TestController.getUserById(TestController.java:50) 32 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 33 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 34 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 35 at java.lang.reflect.Method.invoke(Method.java:606) 36 at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) 37 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) 38 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) 39 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777) 40 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706) 41 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 42 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) 43 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) 44 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) 45 at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) 46 at javax.servlet.http.HttpServlet.service(HttpServlet.java:624) 47 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) 48 at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 49 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 50 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 51 at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 52 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 53 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 54 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 55 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 56 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 57 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 58 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) 59 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 60 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) 61 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169) 62 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 63 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) 64 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 65 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:436) 66 at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078) 67 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) 68 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) 69 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 70 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 71 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 72 at java.lang.Thread.run(Thread.java:745)
折腾了两天,偶尔在网上见网友如下解决方案,才算是解决了(想大哭一场):
原因是:maven默认会把src/main/resources下的所有配置文件以及src/main/java下的所有java文件打包或发布到target\\classes下面,但是现实我们可能会在src/main/java下面也放置一些配置文件如hibernate配置文件或mybatis mapper配置文件等,如果不做一些额外配置,那我们打包后的项目可能找不到这些必须的资源文件,因此在pom.xml中增加类似如下配置,这样就会将src/main/java下的xml文件随同java编译后的class文件一同copy到相应的class目录。
1 <build> 2 <resources> 3 <resource> 4 <directory>src/main/java</directory> 5 <includes> 6 <include>**/mapper/*Mapper.xml</include> 7 </includes> 8 </resource> 9 </resources> 10 </build>
以上是关于解决找不到mapper.xml文件的问题的主要内容,如果未能解决你的问题,请参考以下文章
关于mapper接口与mapper.xml文件在同包运行找不到问题解决
记录idea maven项目打包部署web项目mapper扫描失败