天天看點

批量更新 multi-statement not allow

MyBatis批量更新時所需配置。

第一步:JDBC (allowMultiQueries=true)

url: jdbc:mysql://localhost:3306/test?...&allowMultiQueries=true      
@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
public class DruidConfig {
    @Bean
    public WallFilter wallFilter() {
        WallFilter wallFilter = new WallFilter();
        wallFilter.setConfig(wallConfig());
        return wallFilter;
    }

    @Bean
    public WallConfig wallConfig() {
        WallConfig config = new WallConfig();
        //允許一次執行多條語句
        config.setMultiStatementAllow(true);
        //允許非基本語句的其他語句
        config.setNoneBaseStatementAllow(true);
        return config;
    }
}      
<update id="batchUpdateStudents" parameterType="java.util.List">
  <foreach collection="list" index="index" item="item" separator=";">
    update cell set
    <if test="item.sdudentId != null">`sdudent_id` = #{item.sdudentId},</if>
    <if test="item.sdudentName != null">`sdudent_name` = #{item.sdudentName},</if>
      ...
    <if test="item.updatedBy != null">`updated_by` = #{item.updatedBy},</if>
    <if test="item.updatedDate != null">`updated_date` = #{item.updatedDate}</if>
    where id = #{item.id, jdbcType=INTEGER}
  </foreach>
</update>