Today I am using Springboot to integrate Mybatis and report an error at runtime:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception
...
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [E:\IDEA
When I first saw this error, I guessed that the bean was not created successfully. I also searched various answers on the Internet. Some said that @Service
may not be added to the service layer. I looked at it and it was not the reason, so I started to read the error. information
Look directly at the first line to report the error, because the first line is likely to be the main reason for the error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [E:\IDEA Project\demo\target\classes\com\example\ demo\mapper\UserMapper.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 [E:\IDEA Project \demo\target\classes\mapper\VersionMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [E:\IDEA Project\demo\target\ classes\mapper\VersionMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.libv.entity. Version'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.libv.entity.Version
At first I thought it was a userController
problem, but it wasn't, then looked at this part:
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl'
I looked at userServiceImpl
again and found no problems, so I wondered if I misunderstood this error, so I searched for the meaning of nest, and it turned out that it means nesting in addition to nesting! That means that this section of the error is actually an exception nested with another exception! That is to say, it is the error of the innermost layer that causes the error of each layer above to throw an exception! So I go straight to the next part:
nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [E:\IDEA Project\demo\target\classes\mapper\VersionMapper.xml]'. Cause: org.apache .ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.libv.entity.Version'. Cause: java.lang.ClassNotFoundException: Cannot find class: com.libv.entity.Version
It is found that there is a problem with VersionMapper.xml
, the reason is Error resolving class
. That means there is a problem with a class, and then look at the end: Cannot find class: com.libv.entity.Version
.
I looked at the xml again:
<mapper namespace="com.libv.mapper.VersionMapper">
<insert id="release" parameterType="com.libv.entity.Version">
insert into version(id,version,download_url,release_time)
values (#{id},#{version},#{downloadUrl},NOW())
</insert>
</mapper>
Now I understand, because I don't have the com.libv.entity
package at all! That is to say, the path where my Version
class is located is wrong, resulting in an error in the xml, resulting in layer by layer exceptions.
Change the directory path of the corresponding class of parameterType
in xml
<mapper namespace="com.example.demo.mapper.VersionMapper">
<insert id="release" parameterType="com.example.demo.Entity.Version">
insert into version(id,version,download_url,release_time)
values (#{id},#{version},#{downloadUrl},NOW())
</insert>
</mapper>
Run it again, success!