使用ibatis时 sql中 in 的参数赋值
2012-12-03 11:53
393 查看
一、问题描述:
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>
示例二、
<!-- 删除性别为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://hongzhguan.iteye.com/blog/1222353
相关文章推荐
- 使用ibatis时 sql中 in 的参数赋值
- mybatis 使用一个参数传递数值 sql主要是使用到 in
- 使用PreparedStatement为有in的sql语句动态赋值
- jasperreports ireport使用sql语句中有in时动态赋值
- iBATIS中使用List做参数生成in语句
- JDBC的SQL中使用IN,参数不确定
- ibatis使用HashMap传递SQL参数
- 使用Ibatis时,向后台传参去数据库中取值,参数传到后台,sql也正确,却取不到值!
- <Ibatis in action>中使用动态SQL的一个小细节提示(与CDATA)
- ibatis中写SQL语句时使用in遇到的问题描述
- 执行动态 sql 并为 输出参数赋值、sp_executesql 使用方法
- mysql 按年度、季度、月度、周、日SQL统计查询,mysql 存储过程 中 in 和 FIND_IN_SET 传递多个参数的使用
- jfinal3.0+在SQL语句中使用in传递参数的方法
- 执行动态 sql 并为 输出参数赋值、sp_executesql 使用方法
- Sql语句中in(...)使用参数变量
- ibatis中使用in写SQL语句
- Oracle中使用PL/SQL如何定义参数、参数赋值、输出参数和 if 判断
- PB数据窗口使用数组参数执行SQL的IN方法[转]
- SQL中in参数在存储过程中传递及使用的方法
- 执行动态 sql 并为 输出参数赋值、sp_executesql 使用方法