mybatis foreach批量更新多条sql语句执行的问题

本文介绍如何在SpringBoot项目中使用MyBatis进行数据库批量更新操作,通过XML配置实现动态SQL,解决批量更新时遇到的问题,并分享了在配置文件中加入允许多查询参数的解决方案。

我的工程是springboot的,
mybatis 批量更新mysql数据库,
我的xml如下:
<update id="updateBatch" parameterType="java.util.List">
 <foreach collection="list" index="index" item="item" separator=";">
   update menu_manage
   <set>
       <if test="item.name != null" >
         `name` = #{item.name,jdbcType=VARCHAR},
       </if>
       <if test="item.path != null" >
         path = #{item.path,jdbcType=VARCHAR},
       </if>
       <if test="item.createTime != null" >
         create_time = #{item.createTime,jdbcType=TIMESTAMP},
       </if>
       <if test="item.remark != null" >
         remark = #{item.remark,jdbcType=VARCHAR},
       </if>
   </set>
    where id = #{item.id,jdbcType=INTEGER}
 </foreach>
</update>

配置这些发现没生效:

@Bean(name = "dataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() throws PropertyVetoException {
    DruidDataSource dataSource = new DruidDataSource();
       List<Filter> filters = new ArrayList<>();
       filters.add(wallFilter());
       dataSource.setProxyFilters(filters);
   return dataSource;
}

@Bean
public WallFilter wallFilter() {
   WallFilter wallFilter = new WallFilter();
   wallFilter.setConfig(wallConfig());
   return wallFilter;
}

@Bean
public WallConfig wallConfig() {
   WallConfig wallConfig = new WallConfig();
   //允许一次执行多条语句
   wallConfig.setMultiStatementAllow(true);
   //允许非基本语句的其他语句
   wallConfig.setNoneBaseStatementAllow(true);
   return wallConfig;
}

 

最后在配置文件里,dburl后面加了&allowMultiQueries=true就可以了,也是醉了哦

spring.datasource.url=jdbc:mysql://dev.pc.db:3306/dev_pc_smp2?useUnicode=true&characterEncoding=utf-8&serverTimezone=CTT&allowMultiQueries=true
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值