ibatis mapper文件in参数赋值方法

一、问题描述:

1.在使用ibatis执行下面的sql:

update jc_jiesuan set doing_time = unix_timestamp(curdate()),doing_status = ?           where id in (?) and current_oprerate_type = ?

2.传入的参数是:

Parameters: [1, 444475305,444475300,444475297,444475299, 3]

Types: [java.lang.Integer, java.lang.String, java.lang.Integer]

3.报错信息为:

org.springframework.dao.DataIntegrityViolationException: SqlMapClient operation; SQL [];   
--- The error occurred in com/chl/dao/ibatis/sqlMap/sc_jiesuan-sqlmap.xml.  
--- The error occurred while applying a parameter map.  
--- Check the updateJiesuanDoingStatus-InlineParameterMap.  
--- Check the statement (update failed).  
--- Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value: '444475305,444475300,444475297,444475299'; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:  

二、解决方法:

1.使用“$”

通常在ibatis中传入参数使用的是“#”,如#currentOprerateType:INTEGER#

如果要想in中传入参数,则需要使用“$”符号。

<update id="updateJiesuanDoingStatus" parameterClass="java.util.HashMap"> 
        update jc_jiesuan set doing_time = unix_timestamp(now()),doing_status = #doingStatus:INTEGER# 
         where id in ($ids$)  and current_oprerate_type = #currentOprerateType:INTEGER# 
</update>

但这种方式会增加系统被入侵的可能,因为'$'这个符号会将传进来的值直接组合成查询语句,这样很容易被Sql注入攻击。

2.使用iterate属性。

Iterate:这属性遍历整个集合,并为 List 集合中的元素重复元素体的内容。 
Iterate 的属性: 
      prepend  - 可被覆盖的 SQL 语句组成部分,添加在语句的前面(可选) 
      property  - 类型为 java.util.List 的用于遍历的元素(必选) 
      open  -  整个遍历内容体开始的字符串,用于定义括号(可选) 
      close  -整个遍历内容体结束的字符串,用于定义括号(可选) 
      conjunction -  每次遍历内容之间的字符串,用于定义 AND 或 OR(可选)

示例一:

<select id="selectByIterate" parameterClass="java.util.List" resultClass="user">

SELECT * FROM USERS WHERE USER_ID IN

<iterate conjunction="," open="(" close=")">

  #ids[]#

</iterate>

</select>

ids:为传入List的名称

示例二、

<!-- 删除性别为man,年龄为 11,12 的Person记录,删除相应的person记录 --> 
<delete id="deletePerson" parameterClass="map"> 
delete from 表名 where  sex=#sex#  
<iterate prepend="and" property="personList" open="(" 
close=")" conjunction="or"> 
age=$personList[].age$ 
</iterate> 
</delete>

输出sql如下: 
delete from 表名 where sex='man' and (age =11 or age=12)

参考:http://www.cnblogs.com/sunzhenchao/archive/2012/12/03/2799365.html?ADUIN=834591479&ADSESSION=1406088598&ADTAG=CLIENT.QQ.5329_.0&ADPUBNO=26349

原文地址:https://www.cnblogs.com/tancp/p/3862823.html