springMVC mybatis整合dao接口(mapper接口) spring注入失败

整合springmvc和mybatis时,启动服务报错如下:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [demo.dao.PersonMapper] 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)}
  • 1

错误重点挑出来: 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [demo.dao.PersonMapper] found for dependency

PersonMapper这个接口的实现对象没有注入!

我的servicer接口实现类代码:

@Service("personService")  
public class PersonServiceImpl implements IPersonService {
    
    

    @Autowired  
    private PersonMapper personMapper;  

    @Override
    public Person queryById(Integer id) {
        return this.personMapper.selectByPrimaryKey(id);
    }

}

启动时这一段代码中@Autowired personMapper引起错误的,没有在容器中找到对象。 
可能出现错误的原因: 
1、没有在配置文件中添加如下注解配置:(我添加了)

 <!-- 默认的注解映射的支持 -->
    <mvc:annotation-driven />
    <!--启用自动扫描  -->
    <context:component-scan base-package="demo.*" />

2、那么是不是配置文件错误导致文件没有加载,从而引起没有注解进来。于是我把代码改成了下段代码:

@Service("personService")  
public class PersonServiceImpl implements IPersonService {
    
    

    private PersonMapper personMapper;  

    @Override
    public Person queryById(Integer id) {

        //加载xml文件
        ApplicationContext  ac = new ClassPathXmlApplicationContext("classpath:application/applicationContext-datasource.xml");
        personMapper = (PersonMapper) ac.getBean("personMapper");

        return this.personMapper.selectByPrimaryKey(id);
    }

}

ok,可以运行,说明配置文件没有问题。 
那么之前那样是不是根本没有加载applicationContext-datasource.xml文件,于是我在jdbc.properties文件中改了一个key值(xml文件中引用了),然后把service实现类中的有关代码删除,启动看是否报错,如果报错说明加载了xml文件,如果不报错说明没有加载文件。结果木有报错!!!

于是得出木有加载applicationContext-datasource.xml文件!

下面是我的web.xml配置:

<!-- 加载spring配置文件 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:application/applicationContext*.xml
        </param-value>
    </context-param>

下面是我的数据库mybatis相关配置:

 <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="defaultDataSource" />  
        <!-- 自动扫描mapping.xml文件  --> 
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>  
    </bean>  


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

    <!-- 事务管理,不支持aop的事务管理方式-->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="defaultDataSource" />  
    </bean>  

仔细考察这两个配置,觉得没啥问题,然后看到web.xml中contextConfigLocation这个是怎么加载指定的配置文件的,然后百度了,解决方法: 
在web.xml中添加上下文监听器

<!-- 上下文监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 

关于contextConfigLocation了解点击:详解contextConfigLocation

猜你喜欢

转载自blog.csdn.net/zs520ct/article/details/79656568