栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

适用场景:需要将一个List的内容添加到数据库或从数据库删除
0、.xml文件

代码





    
        
            insert into user_ref_functions
            values
            
                (#{userRefFunctions.id},#{userRefFunctions.userId},#{userRefFunctions.functionsId},#{userRefFunctions.createTime})
            
        
    

    
        delete from user_ref_functions
        where functions_id in
        (
        
            #{item}
        
        ) and user_id = #{userId}
    

测试结果:

以下内容供测试使用
1、Controller 这个方法是给数据库授权或者取消授权

@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Resource
    private UserService userService;
@RequestMapping(value = "/doAuth",method = RequestMethod.POST)
    public CommonResult 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 newAuth;
    private List 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 getNewAuth() {
        return newAuth;
    }

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

    public List getCancelAuth() {
        return cancelAuth;
    }

    public void setCancelAuth(List 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 newAuth = doAuthDto.getNewAuth();
        List cancelAuth = doAuthDto.getCancelAuth();
        if (commonUtil.isEmpty(newAuth)){
            newAuthCount = 0;
        }else {
            LinkedList 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 list);

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

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/871484.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号