程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 注解失敗-Spring整合Mybatis關於Dao注入失敗的問題求解

注解失敗-Spring整合Mybatis關於Dao注入失敗的問題求解

編輯:編程解疑
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>

最佳回答:


自動掃描的包有沒有加進去
<!-- 對web包中的所有類進行掃描,以完成Bean創建和自動依賴注入的功能 -->

Yiran8935
Yiran8935
Yiran8935
Yiran8935
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved