使用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='视频浓缩&压缩任务关联视频表' |
报错信息显示在task_id=3812808338727632896,video_name='vName1',这里,估计下是不是task_id的类型问题,19位应该为char类型。不是的.我传一个对象过来是可以update成功的.程序中发送的SQL语句是不能带分号的!但是你在mybatis中拼接的SQL中是有分号的!额,问题是我程序里面没有写分号啊....<preclass="language-markup"><spanstyle="color:#E2777A;"><spanstyle="color:#CCCCCC;"><iftest<spanstyle="color:#7EC699;"><spanstyle="color:#CCCCCC;">=<spanstyle="color:#CCCCCC;">"studentName!<spanstyle="color:#CCCCCC;">=nullandstudentName!<spanstyle="color:#CCCCCC;">=''<spanstyle="color:#CCCCCC;">"<spanstyle="color:#CCCCCC;">>
是不是test里面取变量不对呢?
没问题,我传一个对象就可以执行成功,2个对象就不行<spanstyle="font-size:13.3333px;">貌似你的批量操作开关没打开,在你的dataSource里面看看url改成这样子的:<spanstyle="font-size:13.3333px;">url<spanstyle="font-size:13.3333px;"> =<spanstyle="font-size:13.3333px;">jdbc:mysql://127.0.0.1:3306/item<spanstyle="font-size:13.3333px;"><spanstyle="color:#009900;">?<spanstyle="color:#009900;">allowMultiQueries=true${item.xxx}改为#{item.xxx}是不准带分号的。另外我还没见过用这种循环方式执行多个SQL的,一般这个是用来in()集合的。
另外要在myibatis中执行的话,一般是批量处理。即sqlMapClient.startBatch()去做。你这错误是已经很明显的告诉了我们update前有;号。
<imgsrc="https://static.oschina.net/uploads/space/2016/0928/091124_kpD1_1859292.png"alt="">
分号是你在这里加上的。
很明显啊如果<spanstyle="font-family:'MicrosoftYaHei',Verdana,sans-serif,宋体;font-size:14px;line-height:22px;background-color:#FFFFFF;">task_id=${item.taskId} 为空 后面凭借的sql的就会多一个,(逗号)sql肯定有错啦,另外for里面写的也你的动态sql有问题!!!
注意下面是错误的
<iftest="null!=#{item.taskId}">task_id=${item.taskId}</if><iftest="null!=#{item.videoName}">,video_name=${item.videoName}</if><iftest="null!=#{item.videoPath}">,video_path=${item.videoPath}</if><iftest="null!=#{item.rstpDriver}">,rstp_driver=${item.rstpDriver}</if><iftest="null!=#{item.rstpIp}">,rstp_ip=${item.rstpIp}</if><iftest="null!=#{item.rstpPort}">,rstp_port=${item.rstpPort}</if><iftest="null!=#{item.rstpAccount}">,rstp_account=${item.rstpAccount}</if><iftest="null!=#{item.rstpPwd}">,rstp_pwd=${item.rstpPwd}</if><iftest="null!=#{item.status}">,status=${item.status}</if>应该修改过为:<preclass="brush:xml;toolbar:true;auto-links:false;"><iftest="null!=taskId">task_id=#{item.taskId}</if><iftest="null!=videoName">,video_name=#{item.videoName}</if><iftest="null!=videoPath">,video_path=#{item.videoPath}</if><iftest="null!=rstpDriver">,rstp_driver=#{item.rstpDriver}</if><iftest="null!=rstpIp">,rstp_ip=#{item.rstpIp}</if><iftest="null!=rstpPort">,rstp_port=#{item.rstpPort}</if><iftest="null!=rstpAccount">,rstp_account=#{item.rstpAccount}</if><iftest="null!=rstpPwd">,rstp_pwd=#{item.rstpPwd}</if><iftest="null!=status">,status=#{item.status}</if>
<imgsrc="https://static.oschina.net/uploads/space/2016/0928/091124_kpD1_1859292.png"alt="">
分号是你在这里加上的。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。