springboot实现对数据库批量增删(List里的内容)

适用场景:需要将一个List的内容添加到数据库或从数据库删除
0、.xml文件
在这里插入图片描述
代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="top.ptcc9.mapper.UserMapper">
    <insert id="insertFunctions">
        <if test="list != null and list.size() > 0">
            insert into user_ref_functions
            values
            <foreach collection="list" item="userRefFunctions" index="index" separator=",">
                (#{userRefFunctions.id},#{userRefFunctions.userId},#{userRefFunctions.functionsId},#{userRefFunctions.createTime})
            </foreach>
        </if>
    </insert>

    <delete id="deleteFunctions">
        delete from user_ref_functions
        where functions_id in
        (
        <foreach collection="cancelAuth" item="item" index="index" separator=",">
            #{item}
        </foreach>
        ) and user_id = #{userId}
    </delete>
</mapper>

测试结果:
在这里插入图片描述
以下内容供测试使用
1、Controller 这个方法是给数据库授权或者取消授权

@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Resource
    private UserService userService;
@RequestMapping(value = "/doAuth",method = RequestMethod.POST)
    public CommonResult<String> doAuth(@RequestBody DoAuthDto doAuthDto){
        String message = userService.doAuth(doAuthDto);
        //返回值类型大家可以根据自己需求设置
        return CommonResult.build(SUCCESS_DO_AUTH,message);
    }
   }  

2、Dto 接收参数类型

```java
package top.ptcc9.entity.Dto;
import java.util.List;
public class DoAuthDto {
    private String managerId;
    private String userId;
    private List<String> newAuth;
    private List<String> cancelAuth;

    public String getManagerId() {
        return managerId;
    }

    public void setManagerId(String managerId) {
        this.managerId = managerId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public List<String> getNewAuth() {
        return newAuth;
    }

    public void setNewAuth(List<String> newAuth) {
        this.newAuth = newAuth;
    }

    public List<String> getCancelAuth() {
        return cancelAuth;
    }

    public void setCancelAuth(List<String> cancelAuth) {
        this.cancelAuth = cancelAuth;
    }
}

3、service层及service实现层

package top.ptcc9.service;
import top.ptcc9.entity.Dto.DoAuthDto;
public interface UserService {
    String doAuth(DoAuthDto doAuthDto);
}
package top.ptcc9.service.impl;
import org.springframework.stereotype.Service;
import top.ptcc9.entity.Do.UserRefFunctions;
import top.ptcc9.entity.Dto.DoAuthDto;
import top.ptcc9.mapper.UserMapper;
import top.ptcc9.service.UserService;
import top.ptcc9.utils.CommonUtil;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommonUtil commonUtil;

    @Override
    public String doAuth(DoAuthDto doAuthDto) {
        int newAuthCount;
        int cancelAuthCount;
        List<String> newAuth = doAuthDto.getNewAuth();
        List<String> cancelAuth = doAuthDto.getCancelAuth();
        if (commonUtil.isEmpty(newAuth)){
            newAuthCount = 0;
        }else {
            LinkedList<UserRefFunctions> list = new LinkedList<>();
            for (String item : newAuth) {
                UserRefFunctions userRefFunctions = new UserRefFunctions();
                userRefFunctions.setId(commonUtil.getSimpleUUID());
                userRefFunctions.setUserId(doAuthDto.getUserId());
                userRefFunctions.setFunctionsId(item);
                userRefFunctions.setCreateTime(commonUtil.getSimpleDateTime());
                list.add(userRefFunctions);
            }
            newAuthCount = userMapper.insertFunctions(list);
        }
        if (commonUtil.isEmpty(cancelAuth)){
            cancelAuthCount = 0;
        }else {
            cancelAuthCount = userMapper.deleteFunctions(cancelAuth,doAuthDto.getUserId());
        }

        if (newAuthCount + cancelAuthCount != newAuth.size() + cancelAuth.size()) {
            // TODO: 2022/5/8 回滚
        }

        return "授权成功,新增授权"+ newAuthCount +"条,取消授权"+ cancelAuthCount +"条";
    }
}

4、mapper层

package top.ptcc9.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import top.ptcc9.entity.Do.UserRefFunctions;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    int insertFunctions (@Param("list")List<UserRefFunctions> list);

    int deleteFunctions (@Param("cancelAuth")List<String> cancelAuth,@Param("userId")String userId);

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值