解決SpringBoot整合Mybatis掃描不到Mapper的問(wèn)題
閑來(lái)無(wú)事,想學(xué)學(xué)springboot,開(kāi)始搭建一個(gè)項(xiàng)目,但是一直顯示mapper掃描不到的錯(cuò)誤:
“Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userController’: Unsatisfied dependency expressed through field ‘userMapper’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.minibike.bike.user.dao.UserMapper’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}”
“Field userMapper in com.minibike.bike.user.controller.UserController required a bean of type ‘com.minibike.bike.user.dao.UserMapper’ that could not be found.”
于是在網(wǎng)上找解決方案,都是治標(biāo)不治本,不得不說(shuō)現(xiàn)在的博客質(zhì)量下降的厲害,太誤導(dǎo)人了,打開(kāi)好多個(gè)都是一樣的內(nèi)容,難道沒(méi)人遇到的錯(cuò)誤一樣,連包名和類名也一樣嗎?有的人抄別人博客,類名和包名都懶得換一下?
最后解決方案是在Application.java主類上加注解@MapperScan(“com.minibike.bike.user.dao”)。括號(hào)中引號(hào)里邊是mapper所在包路徑。
加上注解后不再報(bào)錯(cuò),也能正常輸出oooo
第一次接觸springboot,昨天下午到今早一直沒(méi)找到問(wèn)題所在,后來(lái)查文檔、注解,然后不斷嘗試,最后終于解決。
當(dāng)然這個(gè)只是我個(gè)人項(xiàng)目開(kāi)發(fā)中遇到的問(wèn)題解決辦法,不一定通用,但是如過(guò)遇到這樣的問(wèn)題,也可以一試。
補(bǔ)充知識(shí):springboot 項(xiàng)目配置掃描mapper接口類的兩種方法
一種是在啟動(dòng)類上加@MapperScan({'com.**.dao','com.**.dao.*'}) 掃描mapper接口文件所在的包,多個(gè)包的時(shí)候用英文半角逗號(hào)隔開(kāi);
一種是在mapper接口文件上加@Mapper注解。如下
import org.apache.ibatis.annotations.Mapper;
以上這篇解決SpringBoot整合Mybatis掃描不到Mapper的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP基礎(chǔ)入門第四篇(腳本變量、函數(shù)、過(guò)程和條件語(yǔ)句)2. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)3. jscript與vbscript 操作XML元素屬性的代碼4. Jsp servlet驗(yàn)證碼工具類分享5. XML在語(yǔ)音合成中的應(yīng)用6. 基于PHP做個(gè)圖片防盜鏈7. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)8. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)9. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)10. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)
