MyBatis對映錯誤,No qualifying bean of type 'xx.xx.UserDao' available
阿新 • • 發佈:2019-01-03
在學習myBatis持久層框架時,通常遇到一些挫折,下面我遇到一個錯讓我頭疼了兩週,但最後的解決方案卻十分簡單,下面就讓我說說
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'cn.lin.test.dao.UserDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1474 )
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1102)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1064)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585 )
... 41 more
當出現上述錯誤時,有兩種可能,第一種可能是你的對映檔案寫錯,一對一,一對多關係配置出問題,這也是大多新手學習myBatis時遇到的問題,有需要的可以訪問這個連結“http://www.mybatis.org/mybatis-3/zh/index.html”,裡面有MyBatis的詳細介紹。
第二種可能就是沒有掃描介面所在的包,對映所在的包,實體所在的包。
applicationContext.xml配置
<!-- 包掃描 -->
<!-- 連線池配置。。。 -->
<!-- 配置session工廠 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" scope="singleton">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="typeAliasesPackage" value="cn.lin.test.entity"/>
<!-- 自動掃描mapping.xml檔案 -->
<property name="mapperLocations" value="classpath:cn/lin/test/mapper/*.xml"/>
</bean>
<!-- 掃描介面包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.lin.test.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 事務配置。。。 -->
mybatis-config.xml配置
<configuration>
<settings>
<!-- 使用jdbc的getGeneratedKeys 獲取資料庫自增主鍵值 -->
<setting name="useGeneratedKeys" value="true"/>
<!-- 使用列別名替換列名 預設:true -->
<setting name="useColumnLabel" value="true"/>
<!-- 全域性懶載入 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 開啟駝峰命名轉換:Table{create_time} 》 Entity{createTime} -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
我的錯誤解決方法是:
新增“org.mybatis.spring.mapper.MapperScannerConfigurer”bean的配置,裡面配置介面掃描!問題就這樣解決
可以幫助到你的請給我點贊,謝謝!
感興趣的朋友可以關注微信公眾號(會定時推送新的知識):