MyBatis传入数组批量更新

Mapper类

	int batchDel(@Param("ids")java.lang.String[] ids);

Maper.XML

这里的item要和Mapper类里的**@Param(“ids”)**一致,如果没有要写入array

	<update id="batchDel">
        UPDATE `user` SET `is_valid` = 0 WHERE `user_id` in
        <foreach collection="ids" item="ids" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </update>

Service类

public int batchDel(java.lang.String[] ids){
        return userMapper.batchDel(ids);
    }

Controller

直接将接收的值返回即可

/**
     * 批量删除用户
     * @author Devil
     * @param ids 用户ID数组
     * @return int 返回执行成功的行数
     * String[] ids = new String[]{"3e776492e7ed11eb8171d8cb8ad43d4o","3e776492e7ed11eb8171d8cb8ad43d4s"};
     */
    @PostMapping("/batchDel")
    public APIResult<Integer> batchDel(java.lang.String[] ids){
        return APIResult.ok(userService.batchDel(ids));
    }

application.yml

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql:///shop?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE&useSSL=false&useAffectedRows=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
mybatis:
  mapper-locations: classpath:mapper/*.xml
#  sql语句在控制台打印
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
  port: 8080
  servlet:
    context-path: /api

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值