1. 程式人生 > >關於 SpringMvc+Mybatis專案的No qualifying bean of type org.mybatis.spring.SqlSessionTemplate

關於 SpringMvc+Mybatis專案的No qualifying bean of type org.mybatis.spring.SqlSessionTemplate

專案啟動時,控制檯輸出

No qualifying bean of type org.mybatis.spring.SqlSessionTemplate...

且提示某某Dao 註解失敗,某某srevice註解失敗。

原因:

1:檢查配置檔案中註解掃描路徑是否正確

   如:applicationContext.xml檔案(該檔案物理位置 專案\src\applicationContext.xml)中:

 <!-- 配置mybitas 的sqlSession工廠 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
   <property name="dataSource" ref="dataSource" />
   <property name="configLocation" value="classpath:mybatis-config.xml"></property>
   <property name="mapperLocations" value="classpath:mapper/*.xml" />
 </bean>    <!-- 配置mybitas mapper自動掃描 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xxx.xxx.dao" />
        <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> -->
    </bean>

2:檢查web.xml中是否配置了監聽器

    如:<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener
  </listener-class>
  </listener>
  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

以及

<!--SpringMVC servlet begin-->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <!--SpringMVC servlet  end-->
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>