mybatis-xml中list和数组,size和length

在MyBatis中遇到错误:IllegalArgumentException,原因是尝试将ArrayList与String进行比较。解决方法是在XML映射文件中正确判断list是否为空,使用size>0。但当传入的是数组时,应使用length属性。最后,size()和size在表达式中等效,可避免括号使用不一致导致的问题。

原因:java.lang.IllegalArgumentException:无效比较:java.util.ArrayList和java.lang.String

这个情况在list集合查找数据的sql中出的问题,在接受list的时候加了判断  list!='' ,引起了集合与String类型的比较

https://blog.youkuaiyun.com/ZHOU_VIP/article/details/97640784

//批量删除考核人员
@RequestMapping("/removecheck")
@ResponseBody
public ClaaAjaxResult removecheck(HttpServletRequest request) throws Exception{
	ClaaAjaxResult ret = new ClaaAjaxResult();
	String projectid = request.getParameter("projectid");
	String personids = request.getParameter("personids");
	personids = (personids == null) ? "" : personids.trim();
	String[] personidsArr = personids.split(",");
	if (personids.equals("") || personidsArr == null || personidsArr.length == 0) {
		ret.setRetCode(ClaaAjaxResult.RET_CODE_ERR);
		ret.setRetRemark("待删除考核人员为空!");
		return ret;
	}
	try {
		int rc = this.changeLogService.batchDelete(personidsArr);
		ret.setRetCode(ClaaAjaxResult.RET_CODE_OK);
		ret.setRetRemark("删除考核人员成功");
	} catch (Exception ex) {
		logger.error("removecheck failed: " + personidsArr, ex);
		ret.setRetCode(ClaaAjaxResult.RET_CODE_ERR);
		ret.setRetRemark(ex.getMessage());
	}
	this.addOpLog(ret, MODULE_NAME, "批量删除考核人员记录", "批量删除考核人员", "personids=" + personids);
	return ret;
}


int batchDelete(@Param("personids") String[] personids) throws Exception;
<delete id="batchDelete" >
  delete from t_whppt_check_person
  <where>
  	<if test="personids != null and personids != ''">
  		personid in
  		<foreach collection="personids" item="personid" open="("  close=")" separator=",">
  			#{personid, jdbcType=VARCHAR} 
  		</foreach>
  	</if>
  </where>
 </delete>

修改为:personids.size>0

<delete id="batchDelete" >
  delete from t_whppt_check_person
  <where>
  	<if test="personids != null and personids.size>0">
  		personid in
  		<foreach collection="personids" item="personid" open="("  close=")" separator=",">
  			#{personid, jdbcType=VARCHAR} 
  		</foreach>
  	</if>
  </where>
</delete>

https://www.cnblogs.com/companionspace/p/10419466.html

但是后来运行还是报错:

Caused by: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'personids != null and personids.size()>0'. Cause: org.apache.ibatis.ognl.MethodFailedException: Method "size" failed for object [Ljava.lang.String;@29f5d4b9 [java.lang.NoSuchMethodException

去掉<if test="personids != null and personids.size()>0">

最终改成如下:

<delete id="batchDelete" >
    delete from t_whppt_check_person 
    <where>
        personid in 
    		<foreach collection="personids" item="personid" open="("  close=")" separator=",">
    			#{personid, jdbcType=VARCHAR} 
    		</foreach>
    </where>
</delete>

后来才发现原因:传过来的数组,而数组没有size方法,改成length即可,personids.length > 0或personids.length != 0

<delete id="batchDelete" >
  delete from t_whppt_check_person 
  <where>
      personid in 
  	<if test="personids != null and personids.length > 0">
  		<foreach collection="personids" item="personid" open="("  close=")" separator=",">
  			#{personid, jdbcType=VARCHAR} 
  		</foreach>
  	</if>
  </where>
</delete>

如果传过来的是list,那就用size,如下:

另外:size() > 0,size() !=0等效,size和size()也等效,系统中带括号和不带括号两种写法都有

<if test="p.placeids != null and p.placeids.size() > 0 ">
    	  	AND t.placeid IN 
    	   	<foreach collection="p.placeids"  open="("  close=")" separator="," item="placeId">  
				#{placeId, jdbcType=VARCHAR} 
		</foreach> 
</if> 

 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZHOU_VIP

您的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值