文章目錄
解決辦法
- 新增
application.properties
配置 或者application.yml
配置 - 注意檢查下配置完成後,是否有警告!
application.properties:
# mapper 映射地址(路徑改成自己的哦)
mybatis.mapperLocations=classpath:/com/example/springbootdemo/mapper/*.xml
mybatis.type-aliases-package=com.example.domain
application.yml:
# 配置 Mybatis.xml 位置
mybatis:
mapper-locations: classpath:/com/example/springbootdemo/mapper/*.xml
type-aliases-package: com.example.domain
錯誤原因:xxMapper 注入失敗!
報錯截圖:
驗證:是否是 Mapper 注入的問題(未添加上述配置文件時)
@Autowired
後面追加(required = false)
: 表示支持null
- 運行,然後拋出
空指針異常
。
required 屬性
@Autowired(required=true)
:當使用@Autowired
註解的時候,其實默認就是@Autowired(required=true)
,表示注入的時候,該bean
必須存在,否則就會注入失敗。@Autowired(required=false)
:表示忽略當前要注入的bean
,如果有,直接注入;沒有跳過,不會報錯。