Mysql 批量更新 @mapper使用注解的方式写动态sql

工作中,有个需求,根据不同Id,批量修改数据库中对象的状态。经过测试,用@Mapper生成动态的sql语句。代码如下

BroInstanceBatchMapper

@Mapper
public interface BroInstanceBatchMapper {

    /**
     * 数据表 bro_instance
     * 字段 status = 0:新建进行中  1:成功 2:超时 3:异常错误
     *
     * 原来的SQL语句
     * "UPDATE bro_instance" +
     *             "    SET `status` = CASE " +
     *             "        WHEN id IN (#{successIdList}) THEN 1" +
     *             "        WHEN id IN (#{overTimeIdList}) THEN 2" +
     *             "        WHEN id IN (#{errorIdList}) THEN 3" +
     *             "    END" +
     *             "WHERE id IN (#{allIdList})"
     * @param successIdList
     * @param overTimeIdList
     * @param errorIdList
     * @param allIdList
     */

// ****关键代码*****
    @Update("")
    void batchUpdateInstanceStatus(@Param("successIdList") List successIdList,
                                   @Param("overTimeIdList") List overTimeIdList,
                                   @Param("errorIdList") List errorIdList,
                                   @Param("allIdList") List allIdList);

}

测试调用


  @Autowired
  BroInstanceBatchMapper broInstanceBatchMapper;

public boolean bikeInfoServiceClient() throws TException {
      List errorList = Lists.newArrayList(1L,2L,3L);
      List successList = Lists.newArrayList(4L,5L,6L);
      List overTimeList = Lists.newArrayList(7L,8L,9L);
      List instanceIdList = Lists.newArrayList(1L,2L,3L,4L,5L,6L,7L,8L,9L);
      // 批量修改数据库
      broInstanceBatchMapper.batchUpdateInstanceStatus(successList,overTimeList,errorList,instanceIdList);
      return true;
  }

你可能感兴趣的:(Mysql 批量更新 @mapper使用注解的方式写动态sql)