30s了解一个知识点之mybatis报错信息大全

1
映射文件没有配置namespace

org.apache.ibatis.binding.BindingException: Type interface com.sxt.interface_bind.mapper.UserMapper is not known to the MapperRegistry.

在这里插入图片描述
2
映射文件id不匹配

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.bupt.interface_bind.mapper.UserMapper.selById

在这里插入图片描述
3
索引越界异常
这个十分隐蔽,即使不调用其他的SQL语句,当其他语句#{}内为赋值就会报如下错误,原因是这里我们用的#{}作为占位符,底层其实是用
PreparedStatement发射器进行SQL语句的发送,那么该发射器会进行预编译,并且格式就不能改变了,因此会报该错误

Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 0

在这里插入图片描述
4

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.bupt.mapper.UserMapper.selAll

代码一定不满足
1 必要规范
1)namespace的值必须和对应接口的全限定路径一致;
2)SQL语句标签的id属性必须和当前绑定的接口中对用方法的名称一致;
2 可选规范
1)接口的名称和映射文件的名称一致;
2)将接口和映射文件放在相同的位置;.
其中一条
在这里插入图片描述
5
Resultmap集不包含student值,这个错误也比较隐蔽
因为是映射文件的问题并不属于4条必须要执行的规范
原因是当使用< ResultMap>标签时,主要是为了当数据库列名和POJO的属性名差很多相匹配时使用,当定义了resultMap时默认自动映射关闭,使用resultType时默认自动开启,因此当我使用了resultMap却并没有指定映射关系时,就会报结果集的sell()方法找不到,没有名为student的结果集当

org.apache.ibatis.builder.IncompleteElementException: Could not find result map 'com.bupt.mapper.StudentMapper.student' referenced from 'com.bupt.mapper.StudentMapper.selAll'

Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain value for com.bupt.mapper.StudentMapper.student

6
当我们将代码和配置文件分别放到src和resources时,此时我们可以通过< mapper resource>来扫描映射文件,但是此时注意是目录而不是包,需要用/来分割不能用点,或者可以使用package标签来扫描整个映射文件

 Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com.bupt.mapper.UserMapper.xml

Caused by: java.io.IOException: Could not find resource com.bupt.mapper.UserMapper.xml

在这里插入图片描述
7
结果集已经被定义,这个和端口号已经被占用挺像,就是同一个namespace下ResultSet不能重名

Caused by: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/bupt/mapper/StudentMapper.xml'. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.bupt.mapper.StudentMapper.stuMap

在这里插入图片描述
8
没有名为students属性的getter方法,我们知道NyBatis是通过反射拼接getter/setter方法来实现属性的赋值和查询的.故需要在POJO中生成getter/setter方法

Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'students' in 'class com.bupt.pojo.Clazz'

此处属性写错成students,应该是list属性,而student只是其泛型
在这里插入图片描述

发布了219 篇原创文章 · 获赞 352 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_42859864/article/details/103722159