执行sql时出现com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value错误:
这个错误主要出现在in操作中,如以下操作:
#hibernate
update tablename set status=1 where id in (:ID);
#jdbc
update tablename set status=1 where id in (?);
#mybatis
update tablename set status=1 where id in (#{ID});
ID值类似于1,2,3这种,就会报错:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Truncated incorrect DOUBLE value : '1,2,3'
原因是因为这种替换方式直接将1,2,3当一个值传入,然后转化为相应类型时出错
解决办法:
#1、直接拼接sql
"update tablename set status=1 where id in (" + idStr + ")"
#2、使用直接替换跳过jdbc的保护机制,关键点在于让jdbc直接用你传入的值进行替换不进行其他处理
update tablename set status=1 where id in (::ID)
update tablename set status=1 where id in (${ID})
#3、使用orm框架的list拼接操作,例如mybatis的foreach标签