mybatis,mysql,诡异的错误. sql语句可以执行,但是用mybatis操作就报错.
使用mybatis批量更新数据,用mybatis操作一直报错,但是把日志里的sql语句复制出来,就可以执行.
另外,如果我只传一个对象过来,是可以操作成功的.如果传2个对象就报下面这个错误.
xml配置文件:
<update id="updateList" parameterType="list">
<foreach collection="list" item="item" index="index" open="" close="" separator=";">
update v_compress_video
<set>
<if test="null != #{item.taskId}">task_id = ${item.taskId}</if>
<if test="null != #{item.videoName}">,video_name = ${item.videoName}</if>
<if test="null != #{item.videoPath}">,video_path = ${item.videoPath}</if>
<if test="null != #{item.rstpDriver}">,rstp_driver = ${item.rstpDriver}</if>
<if test="null != #{item.rstpIp}">,rstp_ip = ${item.rstpIp}</if>
<if test="null != #{item.rstpPort}">,rstp_port = ${item.rstpPort}</if>
<if test="null != #{item.rstpAccount}">,rstp_account = ${item.rstpAccount}</if>
<if test="null != #{item.rstpPwd}">,rstp_pwd = ${item.rstpPwd}</if>
<if test="null != #{item.status}">,status = ${item.status}</if>
</set>
<where>
id = ${item.id}
</where>
</foreach>
</update>
错误信息:
2016-09-27 19:35:46,904 [DEBUG]:[org.mybatis.spring.transaction.SpringManagedTransaction.openConnection(SpringManagedTransaction.java:85)] JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6a8c256f] will be managed by Spring
2016-09-27 19:35:46,904 [DEBUG]:[org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:132)] ooo Using Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@6a8c256f]
2016-09-27 19:35:46,919 [DEBUG]:[org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:132)] ==> Preparing: update v_compress_video SET task_id = 3812808338727632896 ,video_name = 'vName1' ,video_path = 'vPath1' ,rstp_driver = 'vDriver1' ,rstp_ip = 'vIP1' ,rstp_port = 'vPort1' ,rstp_account = 'vAccount1' ,rstp_pwd = 'vPwd1' ,status = 1 WHERE id = 3812808338790547456 ; update v_compress_video SET task_id = 3812808338727632896 ,video_name = 'vName2' ,video_path = 'vPath2' ,rstp_driver = 'vDriver2' ,rstp_ip = 'vIP2' ,rstp_port = 'vPort2' ,rstp_account = 'vAccount2' ,rstp_pwd = 'vPwd2' ,status = 1 WHERE id = 3812808338857656320
2016-09-27 19:35:46,919 [DEBUG]:[org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(BaseJdbcLogger.java:132)] ==> Parameters:
2016-09-27 19:35:46,919 [DEBUG]:[org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.logTranslation(SQLErrorCodeSQLExceptionTranslator.java:397)] Translating SQLException with SQL state '42000', error code '1064', message [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update v_compress_video
SET task_id = 3812808338727632896
,video_name =' at line 13]; SQL was [] for task [
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update v_compress_video
SET task_id = 3812808338727632896
,video_name =' at line 13
### The error may involve com.zhixin.dao.compress.impl.CompressVideoDaoImpl.updateList-Inline
### The error occurred while setting parameters
### SQL: update v_compress_video SET task_id = 3812808338727632896 ,video_name = 'vName1' ,video_path = 'vPath1' ,rstp_driver = 'vDriver1' ,rstp_ip = 'vIP1' ,rstp_port = 'vPort1' ,rstp_account = 'vAccount1' ,rstp_pwd = 'vPwd1' ,status = 1 WHERE id = 3812808338790547456 ; update v_compress_video SET task_id = 3812808338727632896 ,video_name = 'vName2' ,video_path = 'vPath2' ,rstp_driver = 'vDriver2' ,rstp_ip = 'vIP2' ,rstp_port = 'vPort2' ,rstp_account = 'vAccount2' ,rstp_pwd = 'vPwd2' ,status = 1 WHERE id = 3812808338857656320
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update v_compress_video
SET task_id = 3812808338727632896
,video_name =' at line 13
日志里面复制出来的sql
UPDATE
v_compress_video
SET
task_id = 3812808338727632896,
video_name = 'vName1',
video_path = 'vPath1',
rstp_driver = 'vDriver1',
rstp_ip = 'vIP1',
rstp_port = 'vPort1',
rstp_account = 'vAccount1',
rstp_pwd = 'vPwd1',
STATUS = 1
WHERE id = 3812808338790547456 ;
UPDATE
v_compress_video
SET
task_id = 3812808338727632896,
video_name = 'vName2',
video_path = 'vPath2',
rstp_driver = 'vDriver2',
rstp_ip = 'vIP2',
rstp_port = 'vPort2',
rstp_account = 'vAccount2',
rstp_pwd = 'vPwd2',
STATUS = 1
WHERE id = 3812808338857656320
数据库信息
Create Table |
CREATE TABLE `v_compress_video` ( `id` bigint(20) NOT NULL COMMENT '主键', `task_id` bigint(20) NOT NULL COMMENT '任务id', `video_name` varchar(50) DEFAULT NULL COMMENT '视频名称', `video_path` varchar(100) DEFAULT NULL COMMENT '视频文件存放地址;视频文件使用', `rstp_driver` varchar(20) DEFAULT NULL COMMENT '实时视频驱动类型;', `rstp_ip` varchar(32) DEFAULT NULL COMMENT '实时视频ip', `rstp_port` varchar(10) DEFAULT NULL COMMENT '实时视频端口', `rstp_account` varchar(20) DEFAULT NULL COMMENT '实时视频登录账号', `rstp_pwd` varchar(20) DEFAULT NULL COMMENT '实时视频登录密码', `status` int(2) DEFAULT NULL COMMENT '状态', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='视频浓缩&压缩任务关联视频表' |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
引用来自“liu13430”的评论
分号是你在这里加上的。
谢谢回答. 不过还是不行. 因为我传过来的list里面是个java对象. 不能直接识别taskId 啥的. 需要使用#{item.taskId}
你的动态sql有问题!!!
注意下面是错误的
应该修改过为:
很明显啊 如果
task_id = ${item.taskId} 为空 后面凭借的sql的就会多一个,(逗号) sql肯定有错啦,另外for里面写的也
分号是你在这里加上的。
是不准带分号的。另外我还没见过用这种循环方式执行多个SQL的,一般这个是用来in()集合的。
另外要在myibatis中执行的话,一般是批量处理。即sqlMapClient.startBatch()去做。你这错误是已经很明显的告诉了我们update前有;号。
用#号不能识别.因为我传过来的list里面是java对象.
回复
@java_oschina : 兄弟 百度下 # 和 $ 区别
貌似你的批量操作开关没打开,在你的dataSource里面看看url 改成这样子的:
url
=
jdbc:mysql://127.0.0.1:3306/item
?allowMultiQueries=true
没问题,我传一个对象就可以执行成功,2个对象就不行
是不是 test 里面取变量不对呢?
额, 问题是我程序里面没有写 分号啊....
但是你在mybatis中拼接的SQL中是有分号的!
程序中发送的SQL语句是不能带分号的!
不是的.我传一个对象过来是可以update成功的.
报错信息显示在task_id = 3812808338727632896,video_name = 'vName1',这里,估计下是不是task_id的类型问题,19位应该为char类型。