肖海南学生管理初始密码接口编写

master
总裁 3 months ago
parent b1ccfb1c3e
commit fcd26f6255
  1. 12
      src/main/java/com/teaching/backend/controller/umsAdmin/UmsStudentManageController.java
  2. 45
      src/main/java/com/teaching/backend/service/impl/umsAdmin/UmsStudentManageServiceImpl.java
  3. 2
      src/main/java/com/teaching/backend/service/umsAdmin/UmsStudentManageService.java

@ -83,4 +83,16 @@ public class UmsStudentManageController {
public CommonResult<String> batchDelete(@RequestParam List<Long> ids){
return umsStudentManageService.batchRemove(ids);
}
/**
* 根据ids初始密码
* @param ids
* @return
*/
@ApiOperation("学生信息批量初始密码")
@PutMapping("/initialPassword")
public CommonResult<String> initialPassword(@RequestParam List<Long> ids){
return umsStudentManageService.initialPassword(ids);
}
}

@ -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<UmsStudentManageMap
private UmsUserMapper umsUserMapper;
@Autowired
private CoursesServiceImpl coursesService;
@Autowired
private PasswordEncoder passwordEncoder;
@Value("${spring.datasource.url}")
private String myUrl;
@ -178,4 +181,46 @@ public class UmsStudentManageServiceImpl extends ServiceImpl<UmsStudentManageMap
}
}
}
@Override
public CommonResult<String> initialPassword(List<Long> 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("修改失败!");
}
}
}

@ -23,4 +23,6 @@ public interface UmsStudentManageService extends IService<UmsStudentManage> {
void startOrStop(String status, Long id);
CommonResult<String> batchRemove(List<Long> ids);
CommonResult<String> initialPassword(List<Long> ids);
}

Loading…
Cancel
Save