在配置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默认要求符合驼峰命名法且第一字母必须小写而导致的。
所以,在这里奉劝写技术书籍的作者不要浮躁,要对得起读者啊!
以上就是配置mybatis时报错信息的解决方案的详细内容。