Mybatis在idea中错误:Invalid bound statement (not found)

简介: 学习mybatis的过程中,测试mapper自动代理的时候一直出错,在eclipse中可以正常运行,而同样的代码在idea中却无法成功。虽然可以继续调试,但心里总是纠结原因。百度了好久,终于找到一个合适的原因。

学习mybatis的过程中,测试mapper自动代理的时候一直出错,在eclipse中可以正常运行,而同样的代码在idea中却无法成功。虽然可以继续调试,但心里总是纠结原因。百度了好久,终于找到一个合适的原因。参考:http://blog.csdn.net/z69183787/article/details/48933481;

原因:(参考:http://www.linuxidc.com/Linux/2015-06/118877.htm)

IDEA的maven项目中,默认源代码目录下的xml等资源文件并不会在编译的时候一块打包进classes文件夹,而是直接舍弃掉。

如果使用的是Eclipse,Eclipse的src目录下的xml等资源文件在编译的时候会自动打包进输出到classes文件夹。Hibernate和Spring有时会将配置文件放置在src目录下,编译后要一块打包进classes文件夹,所以存在着需要将xml等资源文件放置在源代码目录下的需求。

解决:

方法1:将xml或properties等配置文件放到resource下,并修改获取配置文件的代码,比如注册mapper.xml的位置等;

方法2:在maven中添加过滤:

 

 1  <!--配置Maven 对resource文件 过滤 -->
 2         <resources>
 3             <resource>
 4                 <directory>src/main/resources</directory>
 5                 <includes>
 6                     <include>**/*.properties</include>
 7                     <include>**/*.xml</include>
 8                 </includes>
 9                 <filtering>true</filtering>
10             </resource>
11             <resource>
12                 <directory>src/main/java</directory>
13                 <includes>
14                     <include>**/*.properties</include>
15                     <include>**/*.xml</include>
16                 </includes>
17                 <filtering>true</filtering>
18             </resource>
19         </resources>

 

我采用mybatis的自动代理设置,将mapper文件和xml文件同名同包下,并在sqlMap.xml(mybatis-config.xml)中配置mapper自动注册扫描包:

 1 <mappers>
 2         <mapper resource="sqlmap/User.xml"/>
 3         <!--通过resource方法一次加载一个映射文件 -->
 4          <!--<mapper resource="sqlmap/UserMapper.xml"/>-->
 5         
 6         <!-- 通过mapper接口加载单个 映射文件
 7         遵循一些规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致,且在一个目录 中
 8         上边规范的前提是:使用的是mapper代理方法
 9          -->
10          <!--<mapper class="cn.itcast.mybatis.mapper.UserMapper"/>-->
11         
12         <!-- 批量加载mapper
13         指定mapper接口的包名,mybatis自动扫描包下边所有mapper接口进行加载
14         遵循一些规范:需要将mapper接口类名和mapper.xml映射文件名称保持一致,且在一个目录 中
15         上边规范的前提是:使用的是mapper代理方法
16          -->
17         <package name="cn.itcast.mybatis.mapper"/>
18 
19     </mappers>

测试通过,解决了心头的疙瘩。

 

  





唯有不断学习方能改变! -- Ryan Miao
目录
相关文章
|
1月前
|
SQL JSON Java
【bug日记】已解决:Invalid bound statement (not found): 找不到对应的Mapper映射类
【bug日记】已解决:Invalid bound statement (not found): 找不到对应的Mapper映射类
|
9月前
|
Java Spring
2022年1月版IntelliJ Idea报错Could not autowire. No beans of ‘xxxx‘ type found的错误
2022年1月版IntelliJ Idea报错Could not autowire. No beans of ‘xxxx‘ type found的错误
|
9月前
|
SQL XML Java
Mybatis提示Tag name expected的问题
Mybatis提示Tag name expected的问题
173 0
|
10月前
|
JSON Java 数据库连接
IDEA在写Mybatis时遇到了Could not autowire. No beans of 'xxxx' type found 的错误提示
IDEA在写Mybatis时遇到了Could not autowire. No beans of 'xxxx' type found 的错误提示
158 0
解决IDEA中 Could not autowire. No beans of 'xxxx' type found 的错误提示
解决IDEA中 Could not autowire. No beans of 'xxxx' type found 的错误提示
11131 3
解决IDEA中 Could not autowire. No beans of 'xxxx' type found 的错误提示
|
Java 数据库连接 mybatis
idea Mybatis报错statement or DELIMITER expected, got ‘user‘
idea Mybatis报错statement or DELIMITER expected, got ‘user‘
idea Mybatis报错statement or DELIMITER expected, got ‘user‘
|
IDE Java 开发工具
深入Spring Boot:怎样排查expected single matching bean but found 2的异常
写在前面 这个demo来说明怎么排查一个常见的spring expected single matching bean but found 2的异常。
6161 0
SpringBoot项目出现: Invalid bound statement (not found)可能原因
SpringBoot项目出现: Invalid bound statement (not found)可能原因
198 0
SpringBoot项目出现: Invalid bound statement (not found)可能原因

热门文章

最新文章