Springboot实现批量添加,批量删除

vo:接收前端参数

@Data
public class PeoPleVo extends People {

    /**
     * 批量删除id
     */
    private Long[] ids;
    /**
     * 批量添加参数
     */
    private List list;
}

controller

/**
 * 批量添加
 *
 * @param peoPleVo
 * @param request
 * @param response
 * @return
 */
@PostMapping("/addPeople")
public AjaxResult addPeople(@RequestBody PeoPleVo peoPleVo, HttpServletRequest request, HttpServletResponse response) {
    //验证权限
    if (verifyPermissions(request)) {
        return AjaxResult.error(HttpStatus.UNAUTHORIZED, "非法访问,请先登录");
    }
    String token = request.getHeader("token");
    String userName = getUserName(token);
    peoPleVo.getList().forEach(r -> {
        r.setCreateBy(userName);
        r.setUpdateBy(userName);
    });
    return AjaxResult.success(peopleService.addPeople(peoPleVo.getList()));

/**
 * 批量删除
 *
 * @param peoPleVo
 * @param request
 * @param response
 * @return
 */
@PostMapping("/delectPeople")
public AjaxResult delPeople(@RequestBody PeoPleVo peoPleVo, HttpServletRequest request, HttpServletResponse response) {
    //验证权限
    if (verifyPermissions(request)) {
        return AjaxResult.error(HttpStatus.UNAUTHORIZED, "非法访问,请先登录");
    }
    return toAjax(peopleService.delectPeople(peoPleVo.getIds()));
}
}

service

package com.thk.service;

import java.util.List;

public interface IPeopleService extends IService {


 /**
 * 批量添加
 * @param list
 * @return
 */
AjaxResult addPeople(List list);
/**
 * 批量删除
 * @param ids
 * @return
 */
int delectPeople(Long[] ids);
}

service实现类:

@Service
@Transactional//事务注解
public class PeopleImpl extends ServiceImpl implements IPeopleService {

/**
 * 增加用户或者修改用户
 *
 * @param people
 * @return
 */
@Override
@Transactional
public AjaxResult people(People people) {
    List list = baseMapper.selectpeple(people.getPhonenumber(), people.getUserName());
    for (People p : list) {
        if (p.getUserName().equals(people.getUserName())) {
            return AjaxResult.error("用户名已被注册,请登录或者重新输入");
        }
        if (p.getPhonenumber().equals(people.getPhonenumber())) {
            return AjaxResult.error("手机号已被注册,请登录或者重新输入");
        }
    }
    Date date = new Date();
    if (!phoneUtiles.VerifyPhone(people.getPhonenumber())) {
        return AjaxResult.error("请输入正确的手机号码格式");
    }
    if (!phoneUtiles.VerifyEmail(people.getEmail())) {
        return AjaxResult.error("请输入正确的邮箱格式");
    }
    //判断id是否为空,如果为空就是添加
    if (people.getId() == null) {
        SnowFlake snowFlake = new SnowFlake(2, 3);
        //生成id
        long l = snowFlake.nextId();
        //设置id
        people.setId(l);
        //密码加密
        people.setPwd(Md5Utils.hash(people.getPwd()));
        //创建时间
        people.setCreateTime(date);
        //设置状态
        people.setStatus(Constant.STARTUSING);
        //添加数据
        return AjaxResult.success(baseMapper.insert(people));
    } else {
        //如果id不为空,就是修改
        //到数据库查询是否存在这条数据,如果不存在抛出异常
        People people1 = baseMapper.selectById(people.getId());
        if (StringUtils.isNotNull(people1)) {
            return AjaxResult.error("数据不存在");
        }
        people.setUpdateTime(date);
        //修改数据
        return AjaxResult.success(baseMapper.updateById(people));
    }
}
/**
 * 批量添加
 * @param list
 * @return
 */
@Override
@Transactional//事务注解
public AjaxResult addPeople(List list) {
    for (People p : list) {
        //调用添加方法
        AjaxResult people = people(p);
        Object date = people.get("data");
        System.out.println(date);
        if (date == null) {
            //事务回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return people;
        }
    }
    return AjaxResult.success();
}
/**
 * 批量删除
 * @param ids
 * @return
 */
@Override
@Transactional
public int delectPeople(Long[] ids) {
    List longs = Arrays.asList(ids);
    return baseMapper.deleteBatchIds(longs);
}

}

测试添加:

Springboot实现批量添加,批量删除_第1张图片

 测试结果

Springboot实现批量添加,批量删除_第2张图片

数据库

Springboot实现批量添加,批量删除_第3张图片

测试删除

Springboot实现批量添加,批量删除_第4张图片

测试结果

Springboot实现批量添加,批量删除_第5张图片

数据库

删除前

Springboot实现批量添加,批量删除_第6张图片

 删除后Springboot实现批量添加,批量删除_第7张图片

你可能感兴趣的:(Java基础,java)