mybatis 的批量更新操作sql

mybatis的批量更新操作

我们知道mybatis的插入和删除是可以支持批量操作的,但是update也是支持的,代码如下:

<update id="updateBatch" parameterType="list" keyProperty="id" useGeneratedKeys="true">
  <foreach collection="list" index="index" item="item" open="" separator=";" close="">
    update t_acl
    <set>
      acl_desc = #{item.aclDesc,jdbcType=VARCHAR},
      update_time = #{item.updateTime,jdbcType=TIMESTAMP}
    set>
    WHERE device_id = #{item.deviceId,jdbcType=VARCHAR} and acl_number = #{item.aclNumber,jdbcType=VARCHAR}
  foreach>
update>

批量插入:

<insert id="insertBatch" parameterType="list" keyProperty="id" useGeneratedKeys="true">
  insert into t_acl (device_id, acl_number, acl_desc, create_time, update_time)
    values
    <foreach collection="list" separator="," item="item" index="index">
      (#{item.deviceId,jdbcType=VARCHAR}, #{item.aclNumber,jdbcType=VARCHAR},
      #{item.aclDesc,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP}, #{item.updateTime,jdbcType=TIMESTAMP})
    foreach>
insert>

批量删除:

<delete id="deleteBatch">
  delete from t_acl where id in
  <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
    #{item.id}
  foreach>
delete>

注意批量更新的时候,如果使用了druid数据源,则配置的时候要注意:
不能配置wall拦截器,否则批量更新不成功。

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="url" value="jdbc:mysql://10.3.3.133:6789/test?allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8"/>
    <property name="username" value="test"/>
    <property name="password" value="test"/>
    
    <property name="initialSize" value="1"/>
    <property name="minIdle" value="1"/>
    <property name="maxActive" value="20"/>
    
    <property name="maxWait" value="60000"/>
    
    <property name="timeBetweenEvictionRunsMillis" value="60000"/>
    
    <property name="minEvictableIdleTimeMillis" value="300000"/>
    
    <property name="poolPreparedStatements" value="true"/>
    <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
    
   
    <property name="filters" value="stat,log4j"/>
bean>

你可能感兴趣的:(spring,Java,Database,应用服务器,Web开发)