Spring+Mybatis报错: Could not find bean with name"XXX"

Spring+报错: Could not find bean with name"XXX"

最近在制作Spring+Mybtatis+mysql的小项目时,在新建好种子项目(包括controller service dao pojo entity的组件)之后,发现报错找不bean,报错信息如下:

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 'testService': Unsatisfied dependency expressed through field 'testDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testDao' defined in file [/Users/huangruijia/Desktop/intellij/sab/sab-server/target/classes/com/fudan/sab/dao/TestDao.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 [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.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 [/Users/huangruijia/Desktop/intellij/sab/sab-server/target/classes/mappers/test.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [/Users/huangruijia/Desktop/intellij/sab/sab-server/target/classes/mappers/test.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'TestPojo'.  Cause: java.lang.ClassNotFoundException: Cannot find class: TestPojo

这是一行报错信息哦(捂脸),好像显示不完全,大家复制下来可以看完整信息……

我在查找很多解决方法后发现自己的mapper各种配置无误,并且注解也设置正确。(大家可以先看看自己的mapper配置或者注解是否正确设置)但是最后还是没能解决。

最后我自己查看报错信息,发现根本的错误在于最后:

java.lang.ClassNotFoundException: Cannot find class: TestPojo

说明是我的TestPojo类找不到,最后在我的mapper的xml中发现自己returntype 的TestPojo类路径设置错误。

我的项目结构:
Spring+Mybatis报错: Could not find bean with name
原来的test.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.fudan.sab.dao.TestDao" >


    <select id="selectById" parameterType="String" resultType="TestPojo" >
        SELECT *
        FROM test
         WHERE id = #{id}
    </select>

</mapper>

然后将resultType=“TestPojo"改成”“com.fudan.sab.pojo.TestPojo”",就可以正确运行了!

你可能感兴趣的:(问题解决)