AND arrayValCol IN $-6ren">
gpt4 book ai didi

java - 有没有办法在 MyBatis 的 foreach 中使用枚举序数

转载 作者:行者123 更新时间:2023-12-02 09:25:08 24 4
gpt4 key购买 nike

我正在尝试在 foreach 循环中使用 mybatis 动态过滤选择查询。

这是我的 xml 映射器的示例:

<if test="array!= null and array.length > 0">
AND arrayValCol IN
<foreach item="item" collection="array" separator="','" open="('" close="')">
${item}
</foreach>
</if>

但是数组包含枚举作为值,我在 mybatis 文档或谷歌中找不到任何解决方案。

有办法解决这个问题吗?

最佳答案

  • 默认情况下,MyBatis 使用 EnumTypeHandler它在绑定(bind)枚举参数时调用 name() 方法。你需要告诉MyBatis使用EnumOrdinalTypeHandler相反。
  • 您应该尽可能使用 #{} 而不是 ${}。请参阅FAQ .
<if test="array!= null and array.length > 0">
AND arrayValCol IN
<foreach item="item" collection="array" separator="," open="(" close=")">
#{item,typeHandler=org.apache.ibatis.type.EnumOrdinalTypeHandler}
</foreach>
</if>

还可以通过指定defaultEnumTypeHandler来更改默认的枚举类型处理程序。在配置中。

<settings>
<setting name="defaultEnumTypeHandler"
value="org.apache.ibatis.type.EnumOrdinalTypeHandler"/>
</settings>

关于java - 有没有办法在 MyBatis 的 foreach 中使用枚举序数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58394646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com