解決使用intellij idea開發MAVEN專案在target目錄下不存在mapper.xml檔案
阿新 • • 發佈:2019-02-10
這個問題卡了我一上午的時間,由於不服輸的性格,導致必須幹掉他,查了各種資料,問了各種人終於讓我找到這位老兄的部落格
原本在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裡的手工配置 -->
- <propertyname="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檔案。