解决使用intellij idea开发MAVEN项目在target目录下不存在mapper.xml文件
原
解决使用intellij idea开发MAVEN项目在target目录下不存在mapper.xml文件
原本在Eclipse中好好的MAVEN项目,导入intellij idea中,服务器启动正常,但在调用userMapper接口时,报以下异常:
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.beauxie.wxj.dao.UserMapper.findUserByCondition
- at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:178)
- at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:38)
- at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:49)
- at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:42)
- at com.sun.proxy.$Proxy10.findUserByCondition(Unknown Source)
- at com.beauxie.wxj.service.impl.UserServiceImpl.validata(UserServiceImpl.java:61)
- at com.beauxie.wxj.service.impl.UserServiceImpl.validataUsername(UserServiceImpl.java:35)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:606)
- at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
- at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
- at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
- at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
- at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
- at com.sun.proxy.$Proxy13.validataUsername(Unknown Source)
- at com.beauxie.wxj.controller.UserController.checkUsername(UserController.java:83)
- at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
- at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
- at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
- at java.lang.reflect.Method.invoke(Method.java:606)
- at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
- at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
- at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
- at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
- at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
- at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
- at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
- at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
- at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
- at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
- at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
- at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
- at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
- at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
- at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
- at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
- at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
- at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
- at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
- at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
- at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
- at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
- at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
- at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
- at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
- at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
- at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
- at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
- at java.lang.Thread.run(Thread.java:745)
- Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.beauxie.wxj.dao.UserMapper.findUserByCondition
- at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:775)
- at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:615)
- at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:608)
- at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:176)
- ... 60 more
“Invalid bound statement (not found):com.beauxie.wxj.dao.UserMapper.findUserByCondition ”,说明这个异常是在调用Mapper接口时发生的,看到这个异常时,首选想到的是配置可能出了问题,或者没有加注解,但反复检查以后,发现配置啥的都没问题,其中xml配置文件中,关于DAO层与Mapper文件的映射配置如下:
- <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
- <property name="mapperLocations" value="classpath:com/beauxie/**/mapping/*.xml" />
配置文件没有问题,因为在Eclipse中都是正常的,查看控制台输出:
以上的信息表示,服务器正在搜索文件以此来匹配:“com/beauxie/**/mapping/*.xml”,但最后的结果却是:
也就是说没有匹配到!我***,最后静下心来发现是在target目录下对应的位置找不到这个文件,然后去对应的目录下发现其他的包都在(比如controller包),就是没有mapper包(存放Mapper文件的),这让我很纳闷。最后自己百度了一下,终于找到了原因:
对于MAVEN项目,Eclipse会自动把项目src\main\java\目录下的配置文件(.xml)和资源文件(.properties)搬运到target目录下,而intellij idea默认是不会帮我们做这件事的。
解决办法:
在pom.xml文件中加入以下配置:
对应代码如下:
- <!--解决Intellij构建项目时,target/classes目录下不存在mapper.xml文件-->
- <resources>
- <resource>
- <directory>${basedir}/src/main/java</directory>
- <includes>
- <include>**/*.xml</include>
- </includes>
- </resource>
- </resources>
这段代码的意思就是把src/main/java目录下所有的xml文件都包含进去,其中${basedir} 是MAVEN的内置变量,表示项目根目录。
同样,想包含其他什么文件,比如.properties文件,再加一个<include>标签类似的写法即可。
做了以上工作以后,再把项目“Reimport”(右键选中项目->Maven->Reimport)一下,启动服务器,调用Mapper接口便不会报错了,而且target目录下对应的位置也有了mapper.xml文件。