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

在Spring+Mybatis项目中遇到报错:Could not find bean with name 'XXX'。检查后发现mapper配置和注解无误,但问题根源在于TestPojo类的路径设置错误。修正TestPojo在mapper.xml中的returnType为全限定类名,即'com.fudan.sab.pojo.TestPojo',解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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类路径设置错误。

我的项目结构:
在这里插入图片描述
原来的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”",就可以正确运行了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值