今天踩到了一个自己造成的坑,记录一下异常处理的分析过程。
- 项目是SpringBoot集成mybaits-plus,贴出异常
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userPictureController': Unsatisfied dependency expressed through field 'memberService';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'qwMemberServiceImpl': Unsatisfied dependency expressed through field 'baseMapper';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'qwMemberMapper' defined in file [E:\java\scanner\target\classes\com\sjjd\scanpen\mapper\QwMemberMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/spring/boot/starter/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception;
nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [E:\java\scanner\target\classes\com\sjjd\scanpen\mapper\xml\QwExercisePointMapper.xml]';
nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [E:\java\scanner\target\classes\com\sjjd\scanpen\mapper\xml\QwExercisePointMapper.xml]'.
Cause: org.apache.ibatis.builder.BuilderException: Error resolving class.
Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.sjjd.entity.QwExercisePoint'.
Cause: java.lang.ClassNotFoundException: Cannot find class: com.sjjd.entity.QwExercisePoint - 分析异常:我们从"Cause:"语句开始找原因,直接定位到最后三行
- Cause: org.apache.ibatis.builder.BuilderException: Error resolving class.
Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.sjjd.entity.QwExercisePoint'.
Cause: java.lang.ClassNotFoundException: Cannot find class: com.sjjd.entity.QwExercisePoint - 解释:
- 第一行:类解析错误,导致项目编译异常;
- 第二行:无法解析“com.sjjd.entity.QwExercisePoint”别名,导致ibatis类型异常;
- 第三行:找不到com.sjjd.entity.QwExercisePoint的java类的异常;
- 结论:xxxxmapper.xml文件中 ‘com.sjjd.entity.QwExercisePoint’ java类找不到导致的一系列异常
- Cause: org.apache.ibatis.builder.BuilderException: Error resolving class.
- 代码纠正:
- 贴出异常发生所在的xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sjjd.scanpen.mapper.QwExercisePointMapper"> <!-- 通用查询映射结果 --> <resultMap id="BaseResultMap" type="com.sjjd.entity.QwExercisePoint"> <id column="id" property="id" /> <result column="point_key" property="pointKey" /> <result column="point_value" property="pointValue" /> <result column="eid" property="eid" /> <result column="jy_subject_id" property="jySubjectId" /> </resultMap> </mapper>
- 修改:xml文件中的<resultMap id="BaseResultMap" type="com.sjjd.entity.QwExercisePoint"> 全类名编写错误, 有两种修改方案:
- 将全类名改为类名“QwExercisePoint”,让mybaits以别名处理,自己找实体类。(这种方式可以在程序员没有发现实体类全类名错误的时候使用,很少有这样的情况,本次是由于代码迁移产生的问题)
- 找到实体类位置,改为正确的全类名 。
<resultMap id="BaseResultMap" type="com.sjjd.scanpen.entity.QwExercisePoint">
- 按照第二种方式 修改后的代码
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.sjjd.scanpen.mapper.QwExercisePointMapper"> <!-- 通用查询映射结果 --> <resultMap id="BaseResultMap" type="com.sjjd.scanpen.entity.QwExercisePoint"> <id column="id" property="id" /> <result column="point_key" property="pointKey" /> <result column="point_value" property="pointValue" /> <result column="eid" property="eid" /> <result column="jy_subject_id" property="jySubjectId" /> </resultMap> </mapper>
- 运行结果:以上两种方式都可以编译通过,成功运行
- 贴出异常发生所在的xml文件