Mybatis的批量更新

今天遇到一个问题,在mybatis 里面执行批量更新操作的问题。
mybatis本身没有提供对于批量操作的接口。所以对于批量的操作都需要自己写。
正常情况下,批量的插入,都是执行一条Sql 比如:
 <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true">  
   <selectKey keyProperty="id" order="AFTER" resultType="long">  
       SELECT  
       LAST_INSERT_ID()  
   </selectKey>
     insert into lable_tag_log (author, operate_sence, 
     comment, update_time, create_time
     )
 values 
     <foreach collection="list" index="index" item="item" separator=",">  
    (#{item.author,jdbcType=VARCHAR}, #{item.operateSence,jdbcType=VARCHAR}, 
     #{item.comment,jdbcType=VARCHAR}, #{item.updateTime,jdbcType=TIMESTAMP}, #{item.createTime,jdbcType=TIMESTAMP}
     )
    </foreach>  
</insert>

mybatis会将代码翻译成:insert into lable_tag_log (author, operate_sence, comment, update_time, create_time ) values (?, ?, ?, ?, ? ) , (?, ?, ?, ?, ? )
底层执行的是一条sql代码。
但是在执行批量更新的时候,由于更新的方式不同,结构也有有所不同;
<update id="batchUpdate" parameterType="java.util.List">


    <foreach collection="list" item="item" index="index" open="" close="" separator=";"> 
       update lable_type 
      <set>
      <if test="item.lableTypeName != null">
        lable_type_name = #{item.lableTypeName,jdbcType=VARCHAR},
      </if>
      <if test="item.ifCanRepeat != null">
        if_can_repeat = #{item.ifCanRepeat,jdbcType=INTEGER},
      </if>
      <if test="item.author != null">
        author = #{item.author,jdbcType=VARCHAR},
      </if>
      <if test="item.status != null">
        status = #{item.status,jdbcType=INTEGER},
      </if>
      <if test="item.comment != null">
        comment = #{item.comment,jdbcType=VARCHAR},
      </if>
      <if test="item.updateTime != null">
        update_time = #{item.updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="item.createTime != null">
        create_time = #{item.createTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{item.id,jdbcType=INTEGER}
    </foreach>
  </update>
批量更新的时候,一次执行的过程中会执行多条sql代码,每条update之间用“;”分割,如下:
update lable_type SET lable_type_name = ?, author = ?, comment = ?, update_time = ? where id = ? ; 
update lable_type SET lable_type_name = ?, author = ?, status = ?, comment = ?, update_time = ? where id = ?
在执行的时候,由于执行了多条sql代码,在mysql的一次连接的时候,默认的预处理的statement执行一条sql,如果发现有多条执行的sql就会抛异常(个人理解)
org.springframework.jdbc.BadSqlGrammarException:
### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manu
al that corresponds to your MySQL server version for the right syntax to use near 'update lable_info...........'
这里面的解决办法就是:在MySQL的连接字符串URL连接中设置allowMultiQueries参数置为true。(只有MySQL Connector/J 3.1.1以上版本才支持)
url=jdbc:mysql://rds2mazmjavuruy.mysql.rds.aliyuncs.com/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;allowMultiQueries=true
但是值得注意的是 allowMultiQueries 是一个mysql对外提供的连接api里的一个参数
在Spring的xml文件中 需要将 “&”符号修改成“&amp;”
到此spring mybatis的批量更新操作完成。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值