1. 程式人生 > >mybatis+spring boot, mapper 提示Could not autowire. No beans of … type found

mybatis+spring boot, mapper 提示Could not autowire. No beans of … type found

工具及背景: IntelliJ IDEA 2017.1.3 Ultimate。spring boot, maven專案,利用mybatis 註解的方式查詢mysql。

業務邏輯關係:controller --> Service介面 ----> serverImpl —>Dao -->DaoImpl —> mapper -->db

問題:mapper提示Could not autowire. No beans of … type found?如圖:
在這裡插入圖片描述
引入警告導致專案編譯的時候會報錯
在這裡插入圖片描述


網上找了下解決辦法,一種是降低spring的警告級別,嘗試了下下發現只是不提示上面的警告了,但編譯的時候還是會報錯導致不通過,第二種是讓實現類實現序列化介面(Serializable),嘗試了下也沒有解決,後來找了第三種,就是下面這種


在這裡插入圖片描述
編譯通過,問題解決,找了下原因,大概就是
當使用@Autowired註解的時候,其實預設就是@Autowired(required=true),表示注入的時候,該bean必須存在,否則就會注入失敗。@Autowired(required=false):表示忽略當前要注入的bean,如果有直接注入,沒有跳過,不會報錯。
具體原因:https://blog.csdn.net/static_coder/article/details/79580981