MyBatis 一次执行多条语句

MyBatis 支持一次执行多条语句,方法是在 mapper 中使用 foreach 遍历产生多条 SQL,同时在架构中设置数据库连接属性为 allowMultiQueries=true 即可实现

在 Mapper 文件中使用 foreach

在 mapper.java 中如下定义,传入 list

@Update({"<script>",
    "<foreach collection='list' item= 'item' index ='index' separator=';'>",
    "update main_orderm a set a.orderNumber = #{item.orderNumber} where a.fid=#{item.fid}",
    "</foreach>",
    "</script>"})
public int updateATableForeach(@Param("list") List<Orderm> list);

在架构中设置数据库连接属性

进入架构设计页面,开发架构和运行架构中都需要设置。设置方法是:点击业务数据,设置数据库连接属性为 allowMultiQueries=true,如下图所示,多个数据库连接属性之间用逗号,分隔

特别说明

  • 在"高级-数据库设置"中配置的连接参数,不带入发布后的环境

业务代码中调用 mapper 方法

public Integer updateATableForeach(String list) throws Exception {
    List<Orderm> orders = JSON.parseArray(list, Orderm.class);
    orders.forEach(item -> item.setOrderNumber(DateUtil.thisMillisecond()));
    return this.orderdCustomMapper.updateATableForeach(orders);
}

运行效果如下图所示

图 0

results matching ""

    No results matching ""