Syntax 伊巴蒂斯到mybatis的迁移
Syntax 伊巴蒂斯到mybatis的迁移,syntax,mybatis,ibatis,spring-mybatis,Syntax,Mybatis,Ibatis,Spring Mybatis,的ibatis转换为 上面的转换对于字符串很有效。但是如何将列表与空检查进行比较以检查列表是否为空 我试着做以下事情 <if test='List != null and List.size() > 0'> <if test='List != null and List.isNotEmpty()'>(because isEmpty() worked fine) <if test='List != null and !(List.isEmpty())'&
的ibatis转换为
上面的转换对于字符串很有效。但是如何将列表与空检查进行比较以检查列表是否为空
我试着做以下事情
<if test='List != null and List.size() > 0'>
<if test='List != null and List.isNotEmpty()'>(because isEmpty() worked fine)
<if test='List != null and !(List.isEmpty())'>
<if test='List != null and !List.isEmpty()'>
(因为isEmpty()工作得很好)
但是如果我使用isEmpty()和null检查来替代isNotEmpty,那么我在这方面有点困惑
任何关于这个的解释都会很棒 这是。操作数是java表达式
为了达到你的目的,最后一个似乎是最好的:
<if test='List != null and !List.isEmpty()'>
或空安全util方法,例如:
<if test='org.apache.commons.collections4.CollectionUtils.isNotEmpty(List)'>
需要使用类全名来替换导入语句。在myBatis中,您可以通过多种方式验证条件列表是否为空 从下面选择符合您要求的合适答案 基本检查
<if test="list != null and list.size()>0">
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</if>
#{item}
如果您在某些SQL where语句中执行相同的操作,则可以执行以下操作
WHERE
<choose>
<when test="list==null || list.isEmpty()">
1 = 0 <!-- a test returning false, to adapt depending on you DB vendor -->
</when>
<otherwise>
ID IN <foreach item="item" collection="list" open="(" separator="," close=")">#{item}</foreach>
</otherwise>
</choose>
在哪里
1 = 0
#{item}中的ID
使用动态SQL
WHERE <*condition1*>
<dynamic>
<isNotEmpty prepend="AND" property="list">
yourTbl.pk_ID IN
<iterate property="list" open="(" close=")" conjunction=",">
#list[]#
</iterate>
</isNotEmpty>
</dynamic>
在哪里
你的tbl.pk_ID在
#列表[]#