feat(class): 删除班级时验证是否存在学生

- 新增StudentDOMapper接口方法selectStudentCountByClassId,用于查询班级下学生数量
- 在ClassServiceImpl中注入StudentDOMapper
- 删除班级时先判断班级下是否存在学生,若存在则抛出异常防止删除
- 更新StudentDOMapper.xml,添加对应的SQL查询语句selectStudentCountByClassId
This commit is contained in:
lbw
2025-12-15 16:37:42 +08:00
parent f8169b453e
commit e5fbb445cf
3 changed files with 16 additions and 0 deletions

View File

@@ -19,4 +19,6 @@ public interface StudentDOMapper {
// 逻辑删除
void deleteById(Integer id);
int selectStudentCountByClassId(@Param("classId") Integer classId);
}

View File

@@ -6,6 +6,7 @@ import com.yinlihupo.enlish.service.domain.dataobject.GradeDO;
import com.yinlihupo.enlish.service.domain.mapper.ClassDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.GradeClassDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.GradeDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.StudentDOMapper;
import com.yinlihupo.enlish.service.service.ClassService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
@@ -24,6 +25,8 @@ public class ClassServiceImpl implements ClassService {
private GradeClassDOMapper gradeClassDOMapper;
@Resource
private GradeDOMapper gradeDOMapper;
@Resource
private StudentDOMapper studentDOMapper;
@Override
public ClassDO findClassById(Integer id) {
@@ -82,6 +85,10 @@ public class ClassServiceImpl implements ClassService {
@Override
public void deleteClass(Integer classId) {
int selectStudentCountByClassId = studentDOMapper.selectStudentCountByClassId(classId);
if (selectStudentCountByClassId > 0) {
throw new RuntimeException("该班级下有学生,请先删除该班级下的学生");
}
classDOMapper.delete(classId);
}
}

View File

@@ -61,4 +61,11 @@
set is_deleted = 1
where id = #{id}
</update>
<select id="selectStudentCountByClassId" resultType="java.lang.Integer">
select count(1)
from student
where class_id = #{classId}
and is_deleted = 0
</select>
</mapper>