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 39063e2..71c47b8 100644 --- a/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java +++ b/src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java @@ -83,4 +83,16 @@ public class UmsStudentManageController { public CommonResult batchDelete(@RequestParam List ids){ return umsStudentManageService.batchRemove(ids); } + + /** + * 根据ids初始密码 + * @param ids + * @return + */ + @ApiOperation("学生信息批量初始密码") + @PutMapping("/initialPassword") + public CommonResult initialPassword(@RequestParam List ids){ + return umsStudentManageService.initialPassword(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 eb33f43..a81f5f0 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 @@ -12,6 +12,7 @@ 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.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; @@ -38,6 +39,8 @@ public class UmsStudentManageServiceImpl extends ServiceImpl initialPassword(List ids) { + String encodePassword = passwordEncoder.encode("123456"); + + String url = myUrl; // 数据库URL + String user = myUsername; // 数据库用户名 + String password = myPassword; // 数据库密码 + + Connection conn = null; + + String sql = "UPDATE ums_user SET password = ? WHERE id = ?"; + try{ + conn = DriverManager.getConnection(url, user, password); + + PreparedStatement pstmt = conn.prepareStatement(sql); + + conn.setAutoCommit(false); // 关闭自动提交,以便进行批处理 + + for (Long userId : ids) { + pstmt.setString(1, encodePassword); + pstmt.setLong(2, userId); + pstmt.addBatch(); // 将SQL语句添加到批处理中 + } + + pstmt.executeBatch(); // 执行批处理中的所有更新 + conn.commit(); // 提交事务 + + return CommonResult.success("修改成功!"); + } catch (SQLException e) { + e.printStackTrace(); + try { + if (conn != null) { + conn.rollback(); // 发生异常时回滚事务 + } + } catch (SQLException ex) { + ex.printStackTrace(); + } + return CommonResult.failed("修改失败!"); + } + + } } 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 3f7a54d..55e346a 100644 --- a/src/main/java/com/teaching/backend/service/umsAdmin/UmsStudentManageService.java +++ b/src/main/java/com/teaching/backend/service/umsAdmin/UmsStudentManageService.java @@ -23,4 +23,6 @@ public interface UmsStudentManageService extends IService { void startOrStop(String status, Long id); CommonResult batchRemove(List ids); + + CommonResult initialPassword(List ids); }