簡體   English   中英

Java MyBatis 如何找到案例的總和,當值在列表中時,將列表作為參數傳遞給映射器的方法

[英]Java MyBatis How to find SUM of the Cases WHEN value is IN the list passing the list as a parameter to the mapper's method

在我的 Mapper Class 中,我有一個帶有示例查詢的方法:

 "<script>......"
    “GROUP BY acct, call, score HAVING sum(case when code in(\n" +
    "'PTP' \n" +
    "'RPCNPTP', \n" +
    "'LM3P', \n" +
    "'OM_SF',\n" +
    "'OM_SS',\n" +
    "'OM_UNV',\n" +
    "'OM_WR') then 1 else 0 end) <= intnsy ")        
"</script> "
List<Object> getData();

這個查詢工作得很好。 但實際的字符串列表包含大約 70 個不同的字符串,將它們保留在查詢中並不是一個好主意。 我需要將該列表作為參數傳遞給該方法並創建一個循環。 像這樣:

"<script>......"
    “GROUP BY acct, call, score HAVING sum(case when code in(\n" +
     "<foreach item='code' collection='myCodes' separator=',' open='(' close=')'>" +
        " (" +
        " #{code} " +
        " )" +
        " </foreach> " +
        ") then 1 else 0 end) <= intnsy " +
        "</script> ")
       List<Object> getData(@Param("myCodes") List<String> myCodes);

但我無法讓它發揮作用。 錯誤是:

org.apache.ibatis.builder.BuilderException: Could not find value method on SQL annotation.  Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 933; The content of elements must consist of well-formed character data or markup.

顯然我在這里遺漏了一些簡單的東西,但不幸的是我的團隊中沒有其他人可以問請指出正確的方向。 謝謝你。

似乎您必須在字符串列表中處理特殊字符。

例如,你必須對字符<進行轉義,因為 mybatis 將其視為未打開的標簽, &lt將起作用。

這一行:

") then 1 else 0 end) <= intnsy "

將“<”更改為“<”

") then 1 else 0 end) &lt;= intnsy "

現在查詢按預期工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM