您的位置:首页 > 移动开发

Invalid bound statement (not found): com.shizongger.chapter2.mapper.UserMapper.insertUser 解决方案

2017-06-08 22:00 435 查看
在配置MyBatis时报错信息如下:


Invalid bound statement (not found): com.shizongger.chapter2.mapper.UserMapper.insertUser


简单的理解就是找不到正确的语句。一般是由于mapper.xml和mapper.java的文件不匹配造成的。引入映射器大致有四种方法:

1.用文件路径引入映射器

<mapper resource="com/shizongger/chapter2/mapper/RoleMapper.xml" />
<mapper resource="com/shizongger/chapter2/mapper/UserMapper.xml" />

2.用包名引入映射器

<package name="com.shizongger.chapter2.mapper" />

3.用类注册引入映射器

<mapper class="com.shizongger.chapter2.mapper.UserMapper" />
<mapper class="com.shizongger.chapter2.mapper.RoleMapper"/>

4.xxxMapper.xml引入映射器

<mapper url="file:/home/shizongger/workspace/Chapter3/src/com/shizongger/chapter2/mapper/RoleMapper.xml" />
<mapper url="file:/home/shizongger/workspace/Chapter3/src/com/shizongger/chapter2/mapper/UserMapper.xml"/>

这四种引入mapper的方式各有特点,我建议使用第一种或者第二种使用扫描的包的方式。但是在使用第二种的时候要注意:一定要把xxxMapper.java和xxxMapper.xml两者名字一模一样!

本次报错的原因是因为本人把UserMapper.xml错写成userMapper.xml,开头使用了小写,导致程序扫描包的时候扫描不到该mapper而出错。



我为什么会犯此错误?因为我在参考《深入浅出MyBatis技术原理与实践》(电子工业出版社-杨开振著)第3章57页的时候,作者给出了userMapper.xml的代码案例,并以小写作为userMapper.xml文件的开头。且在72页用到mapper.xml的地方,作者都有小写作为mapper.xml开头的习惯。本人误认为是MyBatis默认要求符合驼峰命名法且第一字母必须小写而导致的。

所以,在这里奉劝写技术书籍的作者不要浮躁,要对得起读者啊!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: