[Problem sorting || Mybatis] SpringBoot cannot start, and the test reports an error: Failed to load ApplicationContext

A very strange bug. Originally the project was well written. After writing a set of background codes (controller+service+mapper), the whole module could not be started. The point is that the content of the error report is very vague. After searching for five or six keywords, I found that none of them were in the situation I encountered. The content of the error report is excerpted as follows:

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 

Baidu shook his head after seeing this error report, and it was impossible to start. With the idea of ​​​​being a dead horse as a living horse doctor, I thought of a version that I just pushed with GIT not long ago, and I immediately thought of a stupid solution: save it . Write the code, then use GIT to roll back the version, and then copy the operation step by step, and write the test class to test step by step.

The idea of ​​troubleshooting is as follows:

  1. Test the original code - it works
  2. Add a pair of Mapper interfaces yourself - the test runs normally
  3. After putting in the Mapper file originally written by myself - the error was reproduced! !
  4. Locate the location of the file, and then check further—locate to a SQL query statement. The code at that time was as follows
@Select("SELECT * FROM tbl_rider WHERE is_deleted = 0 AND phone = #{}")
TblRider findRiderExist(String phone);

At first glance, there seems to be nothing wrong with it. After taking a closer look, I immediately slapped myself on the head, because there are no parameters filled in #{} ! ! !


To solve the bug, you have to make a summary:

On the one hand, this is purely a low-level mistake, and we must self-reflect and take SQL writing seriously.

On the other hand, it is also an experience and harvest from my own use of GIT tools to solve problems. For example, the bug report this time is too general to start, so I use GIT to roll back the version, and then reproduce the code and gradually check it. Stupid, but it is actually much more efficient than looking at the screen and thinking hard.

After learning this method, I further remind myself that it is best to test the code step by step, and make backups from time to time in case of emergency!

Guess you like

Origin blog.csdn.net/Xcong_Zhu/article/details/127661392