org.apache.ibatis.binding.BindingException: Invalid bound statement (not found) 是 MyBatis 開發中常見錯誤,通常因 Mapper 介面與 XML 對應不一致導致。本文提供系統排查步驟與一個實戰案例(WAR 包遺漏 XML)。
namespace 是否等於 Mapper 介面全限定名。id 屬性)是否完全對應。執行上述步驟通常能解決 90% 的情況。
Should roll back transaction but cannot - no transaction available
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
WAR 包中找不到 MyBatis 的 *Mapper.xml 檔案。Maven 打包時預設只包含 src/main/resources 下的資源,若 XML 放在 src/main/java 需額外配置。
pom.xml 資源配置<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include> <!-- 關鍵:包含 java 目錄下的 XML -->
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
重新打包部署即可解決。
開發時嚴格對應 namespace、package、方法名;打包時確保資源檔案被納入 WAR/JAR。建議統一將 Mapper XML 放置於 src/main/resources 對應 package 下,避免額外配置。