开发者社区> 问答> 正文

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>

展开
收起
小旋风柴进 2016-03-04 17:21:29 6639 0
1 条回答
写回答
取消 提交回答
  • 个人对建模比较关注

    IAdminDao 没有被init,看下是否有这个bean,如果没有加上,如果有则看下xml有没有关联到spring的xml上

    2019-07-17 18:52:36
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
workshop专场-微服务专场-开发者动手实践营-微服务-Spring Cloud Alibaba 微服务全家桶体验 立即下载
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载

相关实验场景

更多