解決SpringBoot整合Mybatis掃描不到Mapper的問題
閑來無事,想學(xué)學(xué)springboot,開始搭建一個項目,但是一直顯示mapper掃描不到的錯誤:
“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)上找解決方案,都是治標不治本,不得不說現(xiàn)在的博客質(zhì)量下降的厲害,太誤導(dǎo)人了,打開好多個都是一樣的內(nèi)容,難道沒人遇到的錯誤一樣,連包名和類名也一樣嗎?有的人抄別人博客,類名和包名都懶得換一下?
最后解決方案是在Application.java主類上加注解@MapperScan(“com.minibike.bike.user.dao”)。括號中引號里邊是mapper所在包路徑。
加上注解后不再報錯,也能正常輸出oooo
第一次接觸springboot,昨天下午到今早一直沒找到問題所在,后來查文檔、注解,然后不斷嘗試,最后終于解決。
當(dāng)然這個只是我個人項目開發(fā)中遇到的問題解決辦法,不一定通用,但是如過遇到這樣的問題,也可以一試。
補充知識:springboot 項目配置掃描mapper接口類的兩種方法
一種是在啟動類上加@MapperScan({'com.**.dao','com.**.dao.*'}) 掃描mapper接口文件所在的包,多個包的時候用英文半角逗號隔開;
一種是在mapper接口文件上加@Mapper注解。如下
import org.apache.ibatis.annotations.Mapper;
以上這篇解決SpringBoot整合Mybatis掃描不到Mapper的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法2. php測試程序運行速度和頁面執(zhí)行速度的代碼3. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究4. 三個不常見的 HTML5 實用新特性簡介5. 無線標記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁6. ajax請求添加自定義header參數(shù)代碼7. Python使用jupyter notebook查看ipynb文件過程解析8. 解決Python 進程池Pool中一些坑9. 解決python腳本中error: unrecognized arguments: True錯誤10. IntelliJ IDEA創(chuàng)建普通的Java 項目及創(chuàng)建 Java 文件并運行的教程
