mybatis-plus 常見的綁定錯誤解決方案
文章目錄
比較長見到的幾個綁定錯誤:
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
- Result Maps collection already contains value for xxx
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
這個異常出現的原因有蠻多的, 不過最終都是一個最本質的原因就是映射關係不對或者沒找到
1. xml 文件配置問題
xml
中注意三個地方
namespace
: 是否正確指向的mapper
文件<mapper namespace="com.wang.mapper.mysql.xxxMapper">
resultMap
: 是否正確指向你的數據庫實體類<resultMap id="BaseResultMap" type="com.wang.entity.xxx">
parameterType
: 每個方法中的parameterType
是否都指向對應的參數實體
2. application 的配置問題
application.properties
中加上 mapper
以及 xml
的路徑配置
properties
版本
mybatis-plus.mapper-locations=classpath:/mapper/mysql/*.xml
mybatis-plus.type-aliases-package=com.wang.mapper.mysql
yaml
版本
mybatis-plus:
mapper-locations: classpath:/mapper/mysql/*.xml
type-aliases-package: com.wang.mapper.mysql
3. mybatis-plus mapper掃描配置問題
方案一:
可以新建一個 mybatis_plus.java
類來專門配置掃描地址
類上加上註解
@Configuration
@MapperScan(value = {"com.wang.a.mapper.mysql", "com.wang.b.mapper.mysql"})
方案二:
在系統啓動類上加上註解
@MapperScan(value = {"com.wang.a.mapper.mysql", "com.wang.b.mapper.mysql"})
注意:
@MapperScan
這個註解要在@SpringBootApplication
上面, 否者可能會無效
方案三:
在每個 mapper
文件上加上 @mapper
掃描註解
4. 是否使用功能的mybatis-plus
這個是比較尷尬的一個問題, 我把一個沒用 mybatis-plus
的項目加入 mybatis-plus
, 然後搞了老半天都沒搞定, 後面發現以前是用自己的驅動類做的數據庫連接, mybatis-plus
是基於 spring
的那套數據庫連接方式(配置中的數據庫配置以 spring.xxx
開頭的)
Result Maps collection already contains value for xxx
這個錯誤比較簡單, 只要檢查下 xxxMapper.xml
文件的內容就好了
主要是 resultMap
或者是方法有重名導致的
使用 mybatis
代碼生成工具多次的話容易導致 xml
中有重複的 ResultMap
或方法
[外鏈圖片轉存失敗(img-BNSOwei9-1565335079431)(https://tool.lu/netcard/)]