springMVC mybatis integrates dao interface (mapper interface) spring injection fails

When integrating springmvc and mybatis, the startup service reports an error as follows:

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

The error is highlighted: 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [demo.dao.PersonMapper] found for dependency

The implementation object of PersonMapper interface is not injected!

My servicer interface implementation class code:

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

    @Autowired  
    private PersonMapper personMapper;  

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

}

At startup, @Autowired personMapper in this piece of code caused an error, and no object was found in the container. 
Reasons for possible errors: 
1. The following annotation configuration is not added to the configuration file: (I added it)

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

2. Is there an error in the configuration file that caused the file not to be loaded, which caused no annotations to come in. So I changed the code to the following code:

@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, it can be run, indicating that there is no problem with the configuration file. 
So did the applicationContext-datasource.xml file not be loaded at all before, so I changed a key value in the jdbc.properties file (referenced in the xml file), then deleted the relevant code in the service implementation class, and started to see if it An error is reported. If an error is reported, the xml file is loaded. If no error is reported, the file is not loaded. As a result, no error was reported! ! !

So it is concluded that the applicationContext-datasource.xml file is not loaded!

Below is my web.xml configuration:

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

The following is my database mybatis related configuration:

 <!-- 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>  

Carefully examine these two configurations, I think there is no problem, and then see how contextConfigLocation in web.xml loads the specified configuration file, and then Baidu, the solution: add a context 
listener to web.xml

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

关于contextConfigLocation了解点击:详解contextConfigLocation

Guess you like

Origin blog.csdn.net/zs520ct/article/details/79656568