使用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
使用ibatis时 sql中 in 的参数赋值(转)的更多相关文章
- 使用ibatis时 sql中 in 的参数赋值
一.问题描述: 1.在使用ibatis执行下面的sql: update jc_jiesuan set doing_time = unix_timestamp(curdate()),doing_stat ...
- mybatis在动态 SQL 中使用了参数作为变量,必须要用 @Param 注解
如果在动态 SQL 中使用了参数作为变量,那么就要用 @Param 注解,即使你只有一个参数.如果我们在动态 SQL 中用到了 参数作为判断条件,那么也是一定要加 @Param 注解的,例如如下方法: ...
- sql 中条件in参数问题
经常遇到条件为in的模糊查询,sql传参可以在service中直接传递参数,不必使用占位符 select * from ud_order where status in ("+status+ ...
- GBK 编码时 url 中带中文参数的问题
项目中遇到的 GBK 编码问题,记录如下. 将代码精简为: <!DOCTYPE HTML> <html> <meta charset="gb2312" ...
- MyBatis-防止Sql注入以及sql中#{}与${}取参数的区别
#{}能够更安全的取出参数 ${}取出的参数不安全 尽量不要使用${}取参数 原因: A:select * from table where a = '10001' and b = ${paramet ...
- form表单以get方式提交时action中?后面的参数部分不生效
form表单的提交方式是get方式,action="?sss=test",问号后面参数是接受不到的,谨记!
- 动态SQL中 实现条件参数 varchar类型的参数名称 以及模糊查询实现
set @strSQL='select * from testtable AS P WHERE P.Type='+@PType+' and P.PName ='''+@PName+''' and P. ...
- 在PL/SQL中使用带参数的游标
需求:查询并输出部门名称为SALES的员工信息 SET serveroutput ON; DECLARE CURSOR c_emp(paramName VARCHAR2) IS SELECT * FR ...
- SQL中SET和SELECT赋值的区别
最近的项目写的SQL比较多,经常会用到对变量赋值,而我使用SET和SELECT都会达到效果. 那就有些迷惑,这两者有什么区别呢?什么时候哪该哪个呢? 经过网上的查询,及个人练习,总结两者有以下几点主要 ...
随机推荐
- 学习笔记TF033:实现ResNet
ResNet(Residual Neural Network),微软研究院 Kaiming He等4名华人提出.通过Residual Unit训练152层深神经网络,ILSVRC 2015比赛冠军,3 ...
- spring mvc 为什么这么多xml
spring web mvc 处理流程 Architecture web.xml (webapp必要配置) 作用:spring web mvc 使用dispatcherServlet 分发reques ...
- vue学习笔记——组件的优化
Vue 应用性能优化指南 1 给组件定义name,然后在同级目录新建index文件: import Count from './count.vue' export Count; 2 优化大数据的列表 ...
- Vue-项目之免费课和购物车实现
调整首页细节 固定头部 App.vue中代码 <style> body{ padding: 0; margin:0; margin-top: 80px; } </style> ...
- Parallel Programming for FPGAs 学习笔记(1)
Parallel Programming for FPGAs 学习笔记(1)
- 初识TypeScript
环境配置 1,全局安装node和npm 2,安装TypeScript包 npm install typescript -g tsc --version 编写第一个ts程序 1,初始化项目 新建文件夹d ...
- 使用docker compose编排容器
一.安装docker compose 二进制包安装 1.安装 Docker Compose 从 官方 GitHub Release 处直接下载编译好的二进制文件即可 # curl -L https:/ ...
- SQL查数据库有哪些触发器,存储过程...
select name from sysobjects where xtype='TR' --所有触发器select name from sysobjects where xtype='P' --所有 ...
- linux服务之ntp与dns篇
ntp复习: 简介:对于计算机时间的同步管理操作服务器 部署:(服务端和客户端或者说集群) 1.服务端下载ntp 2.打开配置文件/etc/ntp.conf: server 127.127.1.0 ...
- Vue+webpack项目配置便于维护的目录结构
新建项目的时候创建合理的目录结构便于后期的维护是很重要 环境:vue.webpack 目录结构: 项目子目录结构 子目录结构都差不多,主要目录是在src下面操作 src目录结构 src/common ...