IDEA: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value
Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value
##
将Eclipse上的Spring+MyBatis项目导入到IDEA上来,出现这种错误,按理说是怎么都不会有这个错误的,因为在eclipse上是可以跑的。
Type Exception Report
Message Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for njupt.mapper.StudentMapper.selectByStuId
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for njupt.mapper.StudentMapper.selectByStuId
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:980)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:859)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:844)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
错误原因是在IDEA中不会将Sources下的XML文件发布
可以看到target文件夹中开始没有我们的StudentMapper.xml文件
解决方法
把xml直接拖到resourse,运行!但是这个方法 ,其实破坏了包结构。
尝试新的方法:不需要移动mapper.xml,推荐在pom文件中找到
<build></build>
在其中添加以下几行,用于扫描到xml文件。
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>