From 38a268b6e5ce123bc47128fb152f10d54f9d8fc0 Mon Sep 17 00:00:00 2001 From: wyl <wangyulong990316@outlook.com> Date: Wed, 20 Nov 2024 12:37:35 +0000 Subject: [PATCH] batch delete function --- .../controller/admin/AdminController.java | 18 ++++++++++++++---- .../client_project/mapper/AdminMapper.java | 19 +++++++++++++++++++ .../client_project/service/AdminService.java | 10 ++++++++++ .../service/imp/AdminServiceImp.java | 15 +++++++++++++++ 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/cardiff/client_project/controller/admin/AdminController.java b/src/main/java/com/cardiff/client_project/controller/admin/AdminController.java index 4acbf34..778c508 100644 --- a/src/main/java/com/cardiff/client_project/controller/admin/AdminController.java +++ b/src/main/java/com/cardiff/client_project/controller/admin/AdminController.java @@ -4,11 +4,9 @@ import com.cardiff.client_project.pojo.vo.SignUserVo; import com.cardiff.client_project.service.AdminService; import com.cardiff.client_project.utils.Result; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; +import java.util.List; @RestController @@ -28,4 +26,16 @@ public class AdminController { Result result=adminService.insertUserInform(signVo); return result; } + + /** + *delete information by id + * @param ids + * @return + */ + @DeleteMapping("/deleteById") + public Result deleteById(@RequestBody List<Integer> ids){ + System.out.println(ids); + Result result=adminService.deleteById(ids); + return result; + } } diff --git a/src/main/java/com/cardiff/client_project/mapper/AdminMapper.java b/src/main/java/com/cardiff/client_project/mapper/AdminMapper.java index aa4d5ab..258f6ee 100644 --- a/src/main/java/com/cardiff/client_project/mapper/AdminMapper.java +++ b/src/main/java/com/cardiff/client_project/mapper/AdminMapper.java @@ -8,7 +8,9 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.simple.SimpleJdbcInsert; import org.springframework.stereotype.Repository; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; @@ -54,4 +56,21 @@ public class AdminMapper { } } + /** + * Batch deletion based on id + * @param ids + * @return + */ + public int[] deleteById(List<Integer> ids) { + + String sql = "DELETE FROM common_admin WHERE id=?"; + List<Object[]> idList = new ArrayList<>(); + // Build parameter list + for (Integer id : ids) { + idList.add(new Object[]{id}); + } + // Batch delete + int[] item = jdbcTemplate.batchUpdate(sql, idList); + return item; + } } diff --git a/src/main/java/com/cardiff/client_project/service/AdminService.java b/src/main/java/com/cardiff/client_project/service/AdminService.java index 9fab3f7..c83f7a9 100644 --- a/src/main/java/com/cardiff/client_project/service/AdminService.java +++ b/src/main/java/com/cardiff/client_project/service/AdminService.java @@ -3,6 +3,8 @@ package com.cardiff.client_project.service; import com.cardiff.client_project.pojo.vo.SignUserVo; import com.cardiff.client_project.utils.Result; +import java.util.List; + public interface AdminService { /** * User registration @@ -10,4 +12,12 @@ public interface AdminService { * @return */ Result insertUserInform(SignUserVo signVo); + + + /** + * delete information by id + * @param ids + * @return + */ + Result deleteById(List<Integer> ids); } diff --git a/src/main/java/com/cardiff/client_project/service/imp/AdminServiceImp.java b/src/main/java/com/cardiff/client_project/service/imp/AdminServiceImp.java index ac7dc99..c91644d 100644 --- a/src/main/java/com/cardiff/client_project/service/imp/AdminServiceImp.java +++ b/src/main/java/com/cardiff/client_project/service/imp/AdminServiceImp.java @@ -2,16 +2,20 @@ package com.cardiff.client_project.service.imp; import com.cardiff.client_project.constant.Authority; +import com.cardiff.client_project.constant.ResponseCode; import com.cardiff.client_project.mapper.AdminMapper; import com.cardiff.client_project.pojo.entity.CommonAdmin; import com.cardiff.client_project.pojo.vo.SignUserVo; import com.cardiff.client_project.service.AdminService; import com.cardiff.client_project.utils.Result; +import org.apache.coyote.Response; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; +import java.util.List; + @Service public class AdminServiceImp implements AdminService { @@ -40,4 +44,15 @@ public class AdminServiceImp implements AdminService { Result result = adminMapper.insertUserInform(commonAdmin); return result; } + + /** + * delete information by id + * @param ids + * @return + */ + @Override + public Result deleteById(List<Integer> ids) { + adminMapper.deleteById(ids); + return Result.success(ResponseCode.SUCCESS); + } } -- GitLab