NoSuchBeanDefinitionException: No qualifying bean of type 'com.bj186.ssm.mapper.EmployeeMapper' available: expected at least 1 bean which qualifies as autowire candidate


在搭建SSM spring springmvc  mybatis整合的時候, 遇到了這個問題

說說我的問題吧!我在進行單元測試的時候,出現了這個錯誤,網上一查才知道是,配置文件中沒有寫掃描包信息。一看加載的是spring.xml文件,就去這個文件中查找了。

這個錯誤是因為自動掃描包配置出錯。無法自動注入類到容器中

       <!--配置自動掃描的包,用於注解的掃描@Service @Controller-->
       <context:component-scan base-package="com.bj186.crm"></context:component-scan>

       <!--通過bean配置自動掃描並加載mapper-->
       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
              <property name="basePackage" value="com.bj186.crm.mapper"></property>
              <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
       </bean>

正確設置好這段代碼之后, 問題解決!

@Service("UserService")
public class UserServiceImpl implements UserService {

    @Resource
    private EmployeeMapper employeeMapper;

    public Employee selectEmployeeById(Integer eid) {
        return employeeMapper.selectByPrimaryKey(eid);
    }
}

 

 

 

參考資料: https://blog.csdn.net/Q_Sea__/article/details/79832569

https://blog.csdn.net/qq_38003454/article/details/80015389


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM