參數為 List<List<String>> countryCodesList,結構如 [[US,ZH,XX],[ALL]]。需在 SQL 中遍歷二維列表,生成類似:
(authCountryCodes LIKE '%|US|%' AND authCountryCodes LIKE '%|ZH|%' AND authCountryCodes LIKE '%|XX|%')
OR (authCountryCodes LIKE '%|ALL|%')
嘗試使用 #countryCodesList[][]# 但報 NullPointerException。
iBatis (MyBatis 2.x) 的 OGNL 表達式不支援直接雙重下標遍歷二維集合。#countryCodesList[][]# 會嘗試將整個二維列表當作單層迭代,導致參數映射失敗。
// 原始二維列表
List<List<String>> countryCodesList = ...;
// 轉換為一維列表,並在元素前添加分組標識(可選)
List<String> flatList = new ArrayList<>();
for (List<String> group : countryCodesList) {
for (String code : group) {
flatList.add(code); // 或 "GROUP1:" + code
}
}
// 傳入 flatList,SQL 中用 <iterate> 遍歷
iBatis 不直接支援嵌套 iterate。建議改用 MyBatis 3.x,利用 <foreach> 支援嵌套集合:
<foreach collection="countryCodesList" item="group" separator="OR">
(<foreach collection="group" item="code" separator="AND">
authCountryCodes LIKE '%|#{code}|%'
</foreach>)
</foreach>
在 DAO 層手動拼接 SQL 片段,作為字串參數傳入 $sqlFragment$(注意:$ 為直接替換,需自行防注入)。
--- The error occurred while applying a parameter map.
--- Check the parameter mapping for the 'countryCodesList[0][0]' property.
--- Cause: java.lang.NullPointerException
確認是 OGNL 解析 [0][0] 時內部對象為空導致。
iBatis 無法直接處理二維 List 參數。推薦升級 MyBatis 3.x 使用 <foreach> 嵌套,或在 Java 層扁平化數據結構後再傳入 SQL。切勿在 SQL 中硬編碼複雜遍歷邏輯。