From 52398db536eb54b2205201d26acfa3fc6551d17a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=80=BB=E8=A3=81?= <14186472+wenyu441069198@user.noreply.gitee.com> Date: Sat, 10 Aug 2024 17:23:39 +0800 Subject: [PATCH] =?UTF-8?q?=E8=82=96=E6=B5=B7=E5=8D=97=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../umsAdmin/UmsStudentManageController.java | 16 +++- .../umsAdmin/UmsStudentManageServiceImpl.java | 89 ++++++++++++++++++- .../impl/umsAdmin/UmsUserServiceImpl.java | 2 +- .../umsAdmin/UmsStudentManageService.java | 5 ++ 4 files changed, 109 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java b/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java index 6dbf21c..39063e2 100644 --- a/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java +++ b/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java @@ -1,5 +1,6 @@ package com.teaching.backend.controller.umsAdmin; +import com.teaching.backend.common.BaseResponse; import com.teaching.backend.common.CommonResult; import com.teaching.backend.model.dto.umsAdmin.UmsStudentPageQueryDTO; import com.teaching.backend.model.entity.umsAdmin.UmsStudentManage; @@ -9,6 +10,9 @@ import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; import java.util.Map; @RestController @@ -65,8 +69,18 @@ public class UmsStudentManageController { @PutMapping @ApiOperation("编辑学生信息") public CommonResult update(@RequestBody UmsStudentManage umsStudentManage){ - log.info("编辑学生信息: {}",umsStudentManage); umsStudentManageService.updateStudentAndUser(umsStudentManage); return CommonResult.success("修改成功"); } + + /** + * 根据ids批量删除 + * @param ids + * @return + */ + @ApiOperation("学生信息批量删除") + @DeleteMapping("/batchDelete") + public CommonResult batchDelete(@RequestParam List ids){ + return umsStudentManageService.batchRemove(ids); + } } diff --git a/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java index 5dea081..eb33f43 100644 --- a/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java @@ -1,6 +1,7 @@ package com.teaching.backend.service.impl.umsAdmin; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.teaching.backend.common.CommonResult; import com.teaching.backend.exception.BusinessException; import com.teaching.backend.mapper.umsAdmin.*; import com.teaching.backend.model.dto.umsAdmin.UmsStudentPageQueryDTO; @@ -10,15 +11,21 @@ import com.teaching.backend.service.umsAdmin.UmsStudentManageService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.stream.Collectors; - +@Component @Service @Slf4j public class UmsStudentManageServiceImpl extends ServiceImpl implements UmsStudentManageService { @@ -32,6 +39,13 @@ public class UmsStudentManageServiceImpl extends ServiceImpl pageQuery(UmsStudentPageQueryDTO umsStudentPageQueryDTO) { @@ -91,4 +105,77 @@ public class UmsStudentManageServiceImpl extends ServiceImpl batchRemove(List ids) { + + String url = myUrl; // 数据库URL + String user = myUsername; // 数据库用户名 + String password = myPassword; // 数据库密码 + + Connection conn = null; + PreparedStatement pstmt1 = null; + PreparedStatement pstmt2 = null; + + try { + // 建立数据库连接 + conn = DriverManager.getConnection(url, user, password); + + // 关闭自动提交 + conn.setAutoCommit(false); + + StringBuilder inClause = new StringBuilder("("); + for (int i = 0; i < ids.size(); i++) { + inClause.append("?"); + if (i < ids.size() - 1) { + inClause.append(", "); + } + } + inClause.append(")"); + + // 准备SQL语句 + String sql1 = "DELETE FROM ums_user WHERE id IN " + inClause.toString(); + String sql2 = "DELETE FROM ums_student WHERE user_id IN " + inClause.toString(); + + // 创建PreparedStatement对象 + pstmt1 = conn.prepareStatement(sql1); + pstmt2 = conn.prepareStatement(sql2); + + // 为IN子句中的每个ID设置参数 + for (int i = 0; i < ids.size(); i++) { + pstmt1.setInt(i + 1, Math.toIntExact(ids.get(i))); + pstmt2.setInt(i + 1, Math.toIntExact(ids.get(i))); + } + + // 执行DELETE语句 + pstmt1.executeUpdate(); + pstmt2.executeUpdate(); + + // 提交事务 + conn.commit(); + + return CommonResult.success("数据删除成功!"); + + } catch (SQLException e) { + if (conn != null) { + try { + // 发生异常时回滚事务 + conn.rollback(); + } catch (SQLException ex) { + ex.printStackTrace(); + } + } + e.printStackTrace(); + return CommonResult.failed("数据删除失败!"); + } finally { + // 关闭资源(在实际应用中,应该使用try-with-resources语句或确保在finally块中关闭所有资源) + try { + if (pstmt1 != null) pstmt1.close(); + if (pstmt2 != null) pstmt2.close(); + if (conn != null) conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } } diff --git a/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsUserServiceImpl.java b/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsUserServiceImpl.java index e691525..797c403 100644 --- a/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsUserServiceImpl.java +++ b/src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsUserServiceImpl.java @@ -105,7 +105,7 @@ public class UmsUserServiceImpl extends ServiceImpl impl if(!passwordEncoder.matches(password,userDetails.getPassword())){ throw new BusinessException(400,"密码错误"); } - if(!userDetails.isEnabled()){ + if(userDetails.isEnabled()){ throw new BusinessException(400,"帐号已被禁用"); } UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); diff --git a/src/main/java/com/teaching/backend/service/umsAdmin/UmsStudentManageService.java b/src/main/java/com/teaching/backend/service/umsAdmin/UmsStudentManageService.java index cc741bf..3f7a54d 100644 --- a/src/main/java/com/teaching/backend/service/umsAdmin/UmsStudentManageService.java +++ b/src/main/java/com/teaching/backend/service/umsAdmin/UmsStudentManageService.java @@ -3,9 +3,12 @@ package com.teaching.backend.service.umsAdmin; import cn.hutool.db.PageResult; import com.baomidou.mybatisplus.extension.service.IService; +import com.teaching.backend.common.BaseResponse; +import com.teaching.backend.common.CommonResult; import com.teaching.backend.model.dto.umsAdmin.UmsStudentPageQueryDTO; import com.teaching.backend.model.entity.umsAdmin.UmsStudentManage; +import java.util.List; import java.util.Map; public interface UmsStudentManageService extends IService { @@ -18,4 +21,6 @@ public interface UmsStudentManageService extends IService { void updateStudentAndUser(UmsStudentManage umsStudentManage); void startOrStop(String status, Long id); + + CommonResult batchRemove(List ids); }