半半半山 2016-02-15 06:08 采纳率: 80%
浏览 10800
已采纳

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创建和自动依赖注入的功能 -->

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧