【问题整理|| Mybatis】SpringBoot无法启动,测试报错:Failed to load ApplicationContext

一个很奇怪的bug,本来项目写的好好的,在写了一组后台的代码(controller+service+mapper)后,整个模块无法启动。重点是报错的内容很空泛,搜索了五六个关键词发现都不是我遇到的情况,报错的内容节选如下:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'testService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testServiceImpl': Unsatisfied dependency expressed through field 'testMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testMapper' defined in file [D:\GitProject\1101BACK\service\service-rider\target\classes\com\pupu\ppdyf\mapper\TestMapper.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/autoconfigure/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 [D:\GitProject\1101BACK\service\service-rider\target\classes\mapper\RiderMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Parsing error was found in mapping #{}.  Check syntax #{property|(expression), var1=value1, var2=value2, ...} 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'testService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testServiceImpl': Unsatisfied dependency expressed through field 'testMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testMapper' defined in file [D:\GitProject\1101BACK\service\service-rider\target\classes\com\pupu\ppdyf\mapper\TestMapper.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/autoconfigure/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 

这报错百度看了也摇头,简直无从下手,那抱着死马当活马医的想法,我想到了不久前刚用GIT推了一个版本,我立马就想到了一个解决方法:保存好已写的代码,然后使用GIT回退版本,然后一步一步复刻操作,并且写测试类一步一步的测试。

排错的思路如下:

  1. 测试原本的代码 ——正常运行
  2. 自己添加一对Mapper接口 —— 测试正常运行
  3. 将原本自己写的Mapper文件放入后 —— 复刻了错误!!
  4. 定位了文件位置,再进一步排查——定位到一句SQL查询语句上。当时的代码如下
@Select("SELECT * FROM tbl_rider WHERE is_deleted = 0 AND phone = #{}")
TblRider findRiderExist(String phone);

乍一看好像没什么毛病。仔细一看立马拍了自己的脑袋瓜,在#{}里面没有填写参数!!!


BUG解决,自己也要做好总结:

一方面这个纯属是低级错误,必须要自我检讨,认真对待SQL的书写。

另一方面,也是自己利用GIT工具解决问题的一次经验和收获——比如这次的bug报错实在太泛无从下手,我就利用GIT回退版本,然后进行代码复现并逐步排查,看起来很笨,但是比起看着屏幕硬着头皮想其实效率高不少。

学会了这个方法,也进一步提醒自己,代码最好要一步一测,而且也要时常做好备份,以备不时之需!

猜你喜欢

转载自blog.csdn.net/Xcong_Zhu/article/details/127661392