mybatis的常见错误

mybatis错误

      • 错误原因:参数匹配不上
      • 错误原因:找不到资源映射器
      • 错误原因:找不到注册文件
      • 错误原因:找不到驱动名称
      • 错误原因:找不到方法,方法名错误
      • 错误原因:没有返回类型
      • 错误原因:数据库账号密码错误
      • 错误原因:找不到表
      • 错误原因:数据库时区错误
      • 错误原因:没有找到文件
      • 错误原因:未知原因

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'classId' of 'class com.zz.entity.Student' with value 'Clazz(cid=1, cname=bj_a, teacherId=1, teacher=null, students=null)' Cause: java.lang.IllegalArgumentException: argument type mismatch
### The error may exist in mapper/StudentMapper.xml
### The error may involve com.zz.dao.StudentDao.selectAllStudent
### The error occurred while handling results
### SQL: SELECT * from student,class where class_id=c_id and s_id=?
### Cause: org.apache.ibatis.reflection.ReflectionException: Could not set property 'classId' of 'class com.zz.entity.Student' with value 'Clazz(cid=1, cname=bj_a, teacherId=1, teacher=null, students=null)' Cause: java.lang.IllegalArgumentException: argument type mismatch

错误原因:参数匹配不上

解决方法:排查每个映射中的字段名是否对应
mybatis的常见错误_第1张图片发现字段名没有对应,进行修改,错误解决
mybatis的常见错误_第2张图片


Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in mapper/Clamapper
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource mapper/Clamapper

错误原因:找不到资源映射器

解决方法:
1、排查是否在配置文件中配置需要加载的 sql 映射配置文件路径。

mybatis的常见错误_第3张图片
发现文件类型没有标注。修改文件后缀名为.xml,修改后错误解决
mybatis的常见错误_第4张图片


Exception in thread "main" org.apache.ibatis.binding.BindingException: Type interface com.aaa.qy129.dao.ClazzDao is not known to the MapperRegistry.

错误原因:找不到注册文件

解决方法:排查是否在config.xml文件注册了映射文件
发现未注册文件,进行注册文件
mybatis的常见错误_第5张图片添加后错误解决


### Error querying database.  Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: ${
     jdbc.driverName}
### The error may exist in mapper/Clamapper.xml
### The error may involve com.aaa.qy129.dao.ClazzDao.findClassById
### The error occurred while executing a query
### Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: ${
     jdbc.driverName}

错误原因:找不到驱动名称

解决方法:排查是否在config.xml添加db.properties文件
发现未添加db.properties文件,进行添加
mybatis的常见错误_第6张图片添加后错误解决


Exception in thread "main" org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.aaa.qy129.dao.ClazzDao.findClassById

错误原因:找不到方法,方法名错误

解决方法:排查配置文件中的id方法名和dao接口中的方法名是否一致
发现使用错误的方法

mybatis的常见错误_第7张图片进行修改后,错误解决。


Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.aaa.qy129.dao.ClazzDao.findClassById'.  It's likely that neither a Result Type nor a Result Map was specified.
### The error may exist in mapper/Clamapper.xml
### The error may involve com.aaa.qy129.dao.ClazzDao.findClassById
### The error occurred while handling results
### SQL: select * from class c,teacher t,student s where c.teacher_id = t.t_id         and c.c_id =s.class_id         and c.c_id=?
### Cause: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.aaa.qy129.dao.ClazzDao.findClassById'.  It's likely that neither a Result Type nor a Result Map was specified.

错误原因:没有返回类型

解决方法:检查sql语句是否添加了resultMap
发现未添加返回类型,进行添加
mybatis的常见错误_第8张图片再次运行,错误解决


Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
### The error may exist in mapper/Clamapper.xml
### The error may involve com.aaa.qy129.dao.ClazzDao.findClassById
### The error occurred while executing a query
### Cause: java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

错误原因:数据库账号密码错误

解决方法:在db.properties文件中设置正确的账号密码
mybatis的常见错误_第9张图片


Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'shop.class' doesn't exist

错误原因:找不到表

解决方法:排查是否使用了错误的数据库表
mybatis的常见错误_第10张图片修改正确的数据库后,错误解决


org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
### The error may exist in mapper/ClassMapper.xml
### The error may involve com.zz.dao.ClassDao.findClassById
### The error occurred while executing a query
### Cause: java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

错误原因:数据库时区错误

解决方法:在url后面拼接serverTimezone=UTC

mybatis的常见错误_第11张图片添加后,错误消失。


Exception in thread "main" java.io.IOException: Could not find resource mybatis-config.xml

错误原因:没有找到文件

解决方法:检查测试类是否使用了错误的名称
mybatis的常见错误_第12张图片修改文件名,错误消失


记录一个奇葩bug

ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [util.c:840]

mybatis的常见错误_第13张图片

错误原因:未知原因

不影响运行结果,再次运行时就消失了,记录一下,如果有大佬知道给科普科普。
目前我知道的解决方法:重新跑一遍,就解决了

你可能感兴趣的:(bug,bug,mybatis)