• 使用ibatis时 sql中 in 的参数赋值(转)


    转:http://www.cnblogs.com/sunzhenchao/archive/2012/12/03/2799365.html

    一、问题描述:

    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

  • 相关阅读:
    2003系统IIS上传文件不能超过200K的解决方案
    ASP从编辑框中获取图片路径
    ASP 编码转换大全 UTF8、GB2312、二进制、十进制代码、十六进制
    解决IE6、IE7、IE8样式不兼容问题
    py2exe setup.py
    Python to 2bit
    python访问ACCESS
    Pamie Web自动化
    Perl 笔记
    常用工具全盗版 汗颜了
  • 原文地址:https://www.cnblogs.com/shixm/p/7150003.html
Copyright © 2020-2023  润新知