首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用REST API删除多条记录

如何使用REST API删除多条记录
EN

Stack Overflow用户
提问于 2021-03-16 11:26:05
回答 2查看 906关注 0票数 0

我是Springboot的新手,我已经开发了按ID删除记录的资源,现在我喜欢删除选定的多个记录。示例:我喜欢在一次请求中删除10条记录中的3条记录

控制器类:

代码语言:javascript
运行
复制
 @ApiHeader(
            apiOperation = "delete a Content Manage by id",
            apiOperationNotes = "delete a Content Manage by id"
    )
    @PostMapping(value = UriConstants.CONTENT_MANAGE_DELETE)
    @ResponseStatus(HttpStatus.OK)
    public void deleteContentManage(@PathVariable("content_manage_id") int contentmanageId) {
        contentManageService.deleteContentManage(contentmanageId);
    }

服务类别:

代码语言:javascript
运行
复制
  @Transactional(rollbackFor = Exception.class)
    public void deleteContentManage(int contentmanageId) {
        Optional<UserContentManage> optional = userContentManageRepository.findById(contentmanageId);
        if(!optional.isPresent()){
            log.error("Exception occurs while not found content manage ({}) in deletion. ", contentmanageId);
            throw new GenericBadException(StaffNotificationExceptionEnum.CONTENT_MANAGE_NOT_FOUND_EXCEPTION);
        }
        userContentManageRepository.deleteById(contentmanageId);
    }

JPA类:

代码语言:javascript
运行
复制
public interface UserContentManageRepository extends JpaRepository<UserContentManage, Integer> {
}

请建议如何删除选中的多条记录。

EN

Stack Overflow用户

回答已采纳

发布于 2021-03-16 20:51:09

您可以在Repository中添加方法,如下所示

代码语言:javascript
运行
复制
@Modifying
@Transactional
@Query("delete from UserContentManagep where u.id in(:integers)")
void deleteByIdIn(List<Integer> integers);

如果您已经在项目中实现了软删除,可以进行如下软删除操作:

代码语言:javascript
运行
复制
@Modifying
@Transactional
@Query("update  UserContentManagep u set u.active = false where u.id in(:integers)")
void softDeleteAllIds(List<Integer> integers);

在service class中,您可以尝试调用

代码语言:javascript
运行
复制
public void deleteAllBYIds(List<Integer> integers) {
    personRepository.deleteByIdIn(integers);
}

完整的工作示例:

代码语言:javascript
运行
复制
@RestController
@RequestMapping("/person")
public class PersonController {

    private final PersonService personService;

    @Autowired
    public PersonController(PersonService personService) {
        this.personService = personService;
    }

    @GetMapping
    public Iterable<Person> list() {
        return personService.list();
    }

    @PostMapping
    public Person create(@RequestBody Person car) {
        return personService.save(car);
    }


    @DeleteMapping
    public String delete(@RequestParam("ids") List<Integer> ids) {
        System.out.println("deleting");
        personService.deleteAllBYIds(ids);
        return String.join(",", ids.stream().map(value ->  Integer.toString(value)).collect(Collectors.toList()));
    }


}

@Getter
@Setter
@ToString
@Entity
@Where(clause = "active = true") // selecting only items which are active
class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String name;
    private boolean active = true;
}

@Service
class PersonService {
    private final PersonRepository personRepository;

    @Autowired
    PersonService(PersonRepository personRepository) {
        this.personRepository = personRepository;
    }

    @Transactional
    public Person save(Person person) {
        return personRepository.save(person);
    }

    @Transactional(readOnly = true)
    public Iterable<Person> list() {
        return personRepository.findAll();
    }

    @Transactional(readOnly = true)
    public PersonDTO findPersonByName(String name) {
        return personRepository.findPersonsByName(name);
    }

    public void deleteAllBYIds(List<Integer> integers) {
//        personRepository.deleteByIdIn(new ArrayList<>(integers));
        personRepository.softDeleteAllIds(integers);

        System.out.println("deleted adnlakdjakldlas");
    }
}

interface PersonDTO {
    String getName();

    Collection<String> getPersonEvents();
}

@Repository
interface PersonRepository extends CrudRepository<Person, Integer> {
    PersonDTO findPersonsByName(String name);

    @Modifying
    @Transactional
    @Query("delete from Person p where p.id in(:integers)")
    void deleteByIdIn(List<Integer> integers);

    @Modifying
    @Transactional
    @Query("update  Person p set p.active = false where p.id in(:integers)")
    void softDeleteAllIds(List<Integer> integers);

}
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66648705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档