胖胖的半山兄 2016-02-15 06:08 采纳率: 80%
浏览 10796
已采纳

Spring整合Mybatis关于Dao注入失败的问题求解

抛出异常

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pers.zww.grad.dao.IAdminDao pers.zww.grad.service.impl.AdminServiceImpl.adminDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pers.zww.grad.dao.IAdminDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=adminDao)}

此为自动生成的Mapper,我加了注解@Repository
package pers.zww.grad.dao;

import org.springframework.stereotype.Repository;

import pers.zww.grad.vo.Admin;

@Repository("adminDao")
public interface IAdminDao {
int deleteByPrimaryKey(String id);

int insert(Admin record);

int insertSelective(Admin record);

Admin selectByPrimaryKey(String id);

int updateByPrimaryKeySelective(Admin record);

int updateByPrimaryKey(Admin record);

Integer getCountByName(String name);

Integer getCountByNameAndPass(String name,String password);

}
这是Service里面注入
package pers.zww.grad.service.impl;
、、、
@Service("adminService")
public class AdminServiceImpl implements IAdminService {

@Autowired
@Qualifier("adminDao")
private IAdminDao adminDao;

@Override
public int deleteByPrimaryKey(String id) {
    // TODO Auto-generated method stub
    return 0;
}

配置文件都已扫描过了,而且相关mybatis的如下:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <!-- 自动扫描mapping.xml文件 -->
    <property name="mapperLocations" value="classpath:pers/zww/grad/mapping/*.xml"></property>
</bean>

<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="pers.zww.grad.dao" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
  • 写回答

9条回答 默认 最新

  • Yiran8935 2016-02-15 06:28
    关注

    自动扫描的包有没有加进去
    <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 晓呆同学 2016-02-15 06:56
    关注
     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
            <property name="typeAliasesPackage">
                <value>
                    pers.zww.grad.vo
                </value>
            </property>
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:pers/zww/grad/mapping/*.xml"></property>
    </bean>
    
    评论
  • *长铗归来* 2016-02-15 07:03
    关注

    你的注解加到实现类中,不要加接口类

    评论
  • DreamTHT 2016-02-15 08:37
    关注

    @Repository("adminDao")为什么注解在接口上呢

    评论
  • fanst_ 2016-02-15 14:49
    关注
    <context:component-scan base-package="pers.zww.grad.*">
    只写pers.zww.grad试下,我自己的工程加上.*后就报找不到bean了,可能是不支持这样写。
    另外@Autowired默认按照类型注入,可以不指定bean的名称。
    
    评论
  • Royal_lr 2016-02-16 03:40
    关注

    估计是包扫描的问题吧,,

    评论
  • 胖胖的半山兄 2016-02-16 05:57
    关注

    问题已解决,谢谢大家。

    评论
  • 张菌菌 2016-11-18 15:54
    关注

    大哥你这个问题解决了么,我遇到了,求教

    评论
  • axiaoming 2016-12-05 15:33
    关注

    我也想知道,我的也是不能注入呢,不是不需要实现么

    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥15 C语言用正向迭代,反向迭代解决问题
  • ¥15 想要卸载软件(maltab)但是在控制面板的程序和功能中没有要卸载的软件怎么办?
  • ¥15 关于sp验证的一些东西,求告知如何解决,
  • ¥20 关于#javascript#的问题:但是我写的只能接码数字和字符,帮我写一个解码JS问题
  • ¥15 prophet运行报错,如何解决?
  • ¥15 用GPU跑pytorch搭建的LSTM的时候出现了奇怪的报错
  • ¥20 前端数据是从session等作用域拿到的,如何取值继续传递后端呢
  • ¥15 eclipse无法正常运行
  • ¥15 定义了函数,但是无法根据函数定义触发器
  • ¥20 5变量卡诺图化简得出与非门电路图