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/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop.xsd
     http://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注解

@Repository

public 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>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值