繁体   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