mybatis,mysql,诡异的错误. sql语句可以执行,但是用mybatis操作就报错.

发布于 2022-01-04 09:19:44 字数 8595 浏览 943 评论 16

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

风柔一江水 2022-01-07 18:58:47

引用来自“liu13430”的评论

分号是你在这里加上的。

悟红尘 2022-01-07 18:58:46

谢谢回答. 不过还是不行. 因为我传过来的list里面是个java对象. 不能直接识别taskId 啥的. 需要使用#{item.taskId}

猫性小仙女 2022-01-07 18:58:44

你的动态sql有问题!!!

注意下面是错误的

<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>

应该修改过为:

<if test="null != taskId">task_id = #{item.taskId}</if>
<if test="null !=videoName">,video_name = #{item.videoName}</if>
<if test="null != videoPath">,video_path = #{item.videoPath}</if>
<if test="null != rstpDriver">,rstp_driver = #{item.rstpDriver}</if>
<if test="null != rstpIp">,rstp_ip = #{item.rstpIp}</if>
<if test="null != rstpPort">,rstp_port = #{item.rstpPort}</if>
<if test="null != rstpAccount">,rstp_account = #{item.rstpAccount}</if>
<if test="null != rstpPwd">,rstp_pwd = #{item.rstpPwd}</if>
<if test="null != status">,status = #{item.status}</if>

疑心病 2022-01-07 18:58:43

很明显啊 如果
task_id = ${item.taskId}  为空  后面凭借的sql的就会多一个,(逗号) sql肯定有错啦,另外for里面写的也

南冥有猫 2022-01-07 18:58:37

分号是你在这里加上的。

情痴 2022-01-07 18:58:30

是不准带分号的。另外我还没见过用这种循环方式执行多个SQL的,一般这个是用来in()集合的。

另外要在myibatis中执行的话,一般是批量处理。即sqlMapClient.startBatch()去做。你这错误是已经很明显的告诉了我们update前有;号。

女中豪杰 2022-01-07 18:58:29

用#号不能识别.因为我传过来的list里面是java对象.

明月松间行 2022-01-07 18:56:42

回复
@java_oschina : 兄弟 百度下 # 和 $ 区别

断爱 2022-01-07 18:54:40

貌似你的批量操作开关没打开,在你的dataSource里面看看url 改成这样子的:
url
 =
jdbc:mysql://127.0.0.1:3306/item
?allowMultiQueries=true

如此安好 2022-01-07 18:53:10

没问题,我传一个对象就可以执行成功,2个对象就不行

秋意浓 2022-01-07 18:46:01
<if test="studentName!=null and studentName!='' ">

是不是 test 里面取变量不对呢?

海之角 2022-01-07 18:29:36

额, 问题是我程序里面没有写 分号啊....

坏尐絯 2022-01-07 17:40:53

但是你在mybatis中拼接的SQL中是有分号的!

清欢 2022-01-07 17:12:44

程序中发送的SQL语句是不能带分号的!

三月梨花 2022-01-07 16:03:46

不是的.我传一个对象过来是可以update成功的.

孤独患者 2022-01-07 02:33:25

报错信息显示在task_id = 3812808338727632896,video_name = 'vName1',这里,估计下是不是task_id的类型问题,19位应该为char类型。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文