欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > springboot mybatis注入不了dao层的bean

springboot mybatis注入不了dao层的bean

2025/5/2 16:32:13 来源:https://blog.csdn.net/fuxiaojun1988/article/details/143837685  浏览:    关键词:springboot mybatis注入不了dao层的bean

报错信息

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'projectController': Unsatisfied dependency expressed through field 'projectService': Error creating bean with name 'projectServiceImpl': Unsatisfied dependency expressed through field 'projectDao': No qualifying bean of type 'org.neo.project.dao.ProjectDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:788)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:768)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:509)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1439)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:599)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:337)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:335)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:975)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:971)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:625)at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:706)at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:720)a

配置信息:spring.-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx"><!-- Data Source Bean --><bean id="dataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init" destroy-method="close"><property name="driverClassName"value="${mysql.jdbc.driverClassName}" /><property name="url" value="${jdbc.url}" /><property name="username"value="${jdbc.username}" /><property name="password"value="${jdbc.password}" /><!-- You could use other data source manager driver such as: class="org.springframework.jdbc.datasource.DriverManagerDataSource". If so, the properties below should be removed based on the schema of xml. -->
<!--        <property name="initialSize"-->
<!--                  value="${mysql.jdbc.initialSize}" />-->
<!--        <property name="maxActive"-->
<!--                  value="${mysql.jdbc.maxActive}" />-->
<!--        <property name="maxIdle" value="${mysql.jdbc.maxIdle}" />-->
<!--        <property name="minIdle" value="${mysql.jdbc.minIdle}" />-->
<!--        <property name="maxWait" value="${mysql.jdbc.maxWait}" />--></bean><!-- SQL Session Factory Bean --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- auto scan mapping xml files --><property name="mapperLocations" value="classpath:org/neo/mapper/project/*.xml" /><property name="configLocation" value="classpath:spring-mybatis.xml" /><!-- If there are special configurations such as alias, could add: <property name="configLocation" value="classpath:mybatis-config.xml"></property> --></bean><!-- DAO interfaces --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.neo.project.dao" /><property name="sqlSessionFactoryBeanName"value="sqlSessionFactory" /></bean>
<!--    <mybatis:scan base-package="org.neo"/>-->
</beans>

项目目录结构

为什么会导致Spring容器识别不了dao层的bean

排查思路

原因排查

1. 配置扫描bean时没有扫描到dao层所在的包

<context:component-scan base-package="org.neo" />

2. dao层类上没有添加@Repository注解或者@Mapper注解

@Repositorypublic interface UserMapper extends BaseMapper<User> {}

3. 配置接口层映射mybatis查询时,没有配置dao层扫描器

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="org.neo.project.dao" /><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词