mybatis 如何判断数组是否为空,空时不循环数组

mybatis代码
--------------------------------------------
<if test="search.dataforArray != null">
and datefor in
<foreach item="search.dataforArray" index="index" collection="search.dataforArray" open="(" separator="," close=")">#{search.dataforArray}</foreach>
</if>
---------------------------------------------
如果数组为空时出现错误
nested exception is org.apache.ibatis.builder.BuilderException: The expression 'search.dataforArray' evaluated to a null value.

请问如何修改呢?

答案:

    &lt;if test="search.dataforArray != null"&gt; 
    and datefor in ;

    &lt;foreach item="item" index="index";

    collection="search.dataforArray";

    open="("separator="," close=")"&gt;#{item}&lt;/foreach&gt; 

    &lt;/if&gt。

循环体中的具体对象:支持属性的点路径访问,如item.age,item.info.details,具体说明:在list和数组中是其中的对象,在map中是value,collection,要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象没有默认的键。当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array将会失效。
除了入参这种情况外,还有一种作为参数对象的某个字段的时候。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-09-05
试试
<if test="search.dataforArray != null">
and datefor in
<foreach item="item" index="index" collection="search.dataforArray" open="(" separator="," close=")">#{item}</foreach>
</if>本回答被提问者和网友采纳
第2个回答  2018-01-24
这个<if test="search.dataforArray != null">
改为:
<if test="search.dataforArray != null and search.dataforArray !=''" >
第3个回答  2018-01-24
朋友,请问最后是怎么解决的呢 这样貌似也不行 为空的时候还是进入了下面的条件语句
第4个回答  2017-11-06
<if test="search.dataforArray != null and search.dataforArray.length>0 ">

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网