From 1d3538c720cead214edc1ee049723f1b8c7eb69f Mon Sep 17 00:00:00 2001 From: pveayojnc <3273195329@qq.com> Date: Tue, 29 Apr 2025 18:06:33 +0800 Subject: [PATCH] Update StudentServiceImpl.java --- .../zsz/service/impl/StudentServiceImpl.java | 68 ++++++++++++++++--- 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/zsz/service/impl/StudentServiceImpl.java b/src/main/java/com/zsz/service/impl/StudentServiceImpl.java index ff8dce5..91ea6df 100644 --- a/src/main/java/com/zsz/service/impl/StudentServiceImpl.java +++ b/src/main/java/com/zsz/service/impl/StudentServiceImpl.java @@ -1,50 +1,100 @@ package com.zsz.service.impl; +// 导入 MyBatis-Plus 用于构建查询条件的 QueryWrapper 类 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +// 导入 MyBatis-Plus 的基础 Mapper 接口 import com.baomidou.mybatisplus.core.mapper.BaseMapper; +// 导入 MyBatis-Plus 用于实现分页查询的 Page 类 import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +// 导入 MyBatis-Plus 提供的通用 Service 实现类 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +// 导入学生数据访问层的 Mapper 接口 import com.zsz.mapper.StudentMapper; +// 导入管理员实体类(此处可能是多余导入,代码中未使用) import com.zsz.pojo.Admin; +// 导入用户登录表单实体类 import com.zsz.pojo.LoginForm; +// 导入学生实体类 import com.zsz.pojo.Student; +// 导入学生服务层接口 import com.zsz.service.StudentService; +// 导入 MD5 加密工具类,用于对密码进行加密处理 import com.zsz.util.MD5; +// 导入 Spring 框架的 Service 注解,用于将该类标记为服务层组件 import org.springframework.stereotype.Service; +// 导入 Spring 框架的事务管理注解,用于开启事务支持 import org.springframework.transaction.annotation.Transactional; +// 导入 Spring 框架的工具类,用于字符串操作 import org.springframework.util.StringUtils; +/** + * 学生服务实现类,继承自 MyBatis-Plus 的 ServiceImpl 类,实现了 StudentService 接口。 + * 该类负责处理与学生相关的业务逻辑,如学生登录验证、根据 ID 获取学生信息以及分页查询学生数据。 + */ +// 将该类标记为服务层组件,指定名称为 "studentServiceImpl" @Service("studentServiceImpl") +// 开启事务支持,确保业务操作的原子性 @Transactional public class StudentServiceImpl extends ServiceImpl implements StudentService { + + /** + * 学生登录验证方法 + * @param loginForm 用户登录表单对象,包含用户名和密码等信息 + * @return 如果验证通过,返回对应的学生对象;否则返回 null + */ @Override public Student login(LoginForm loginForm) { - //创建QueryWapper对象 + // 创建 QueryWrapper 对象,用于构建数据库查询条件 QueryWrapper studentQueryWrapper = new QueryWrapper<>(); - //拼接查询条件 - studentQueryWrapper.eq("name",loginForm.getUsername()); + // 拼接查询条件:学生姓名等于登录表单中的用户名 + studentQueryWrapper.eq("name", loginForm.getUsername()); + // 拼接查询条件:学生密码(经过 MD5 加密)等于登录表单中的密码加密后的值 studentQueryWrapper.eq("password", MD5.encrypt(loginForm.getPassword())); + // 调用 BaseMapper 的 selectOne 方法,根据查询条件查询数据库,返回符合条件的唯一学生对象 Student student = baseMapper.selectOne(studentQueryWrapper); return student; } + /** + * 根据学生 ID 获取学生信息 + * @param userId 学生的 ID + * @return 如果存在对应的学生信息,返回该学生对象;否则返回 null + */ @Override public Student getStudentById(Long userId) { + // 创建 QueryWrapper 对象,用于构建数据库查询条件 QueryWrapper studentQueryWrapper = new QueryWrapper<>(); - studentQueryWrapper.eq("id",userId); + // 拼接查询条件:学生 ID 等于传入的用户 ID + studentQueryWrapper.eq("id", userId); + // 调用 BaseMapper 的 selectOne 方法,根据查询条件查询数据库,返回符合条件的唯一学生对象 return baseMapper.selectOne(studentQueryWrapper); } + /** + * 分页查询学生数据 + * @param studentPage 分页对象,包含页码、每页记录数等信息 + * @param student 学生实体对象,用于构建查询条件,如班级名称、学生姓名等 + * @return 包含分页查询结果的 Page 对象 + */ @Override public Page getStudentData(Page studentPage, Student student) { + // 创建 QueryWrapper 对象,用于构建数据库查询条件 QueryWrapper studentQueryWrapper = new QueryWrapper<>(); - if (!StringUtils.isEmpty(student.getClazzName())){ - studentQueryWrapper.like("clazz_name",student.getClazzName()); + + // 判断学生对象中的班级名称是否为空,如果不为空,则添加模糊查询条件 + if (!StringUtils.isEmpty(student.getClazzName())) { + // 添加班级名称的模糊查询条件,对应数据库表中的 clazz_name 字段 + studentQueryWrapper.like("clazz_name", student.getClazzName()); } - if (!StringUtils.isEmpty(student.getName())){ - studentQueryWrapper.like("name",student.getName()); + + // 判断学生对象中的学生姓名是否为空,如果不为空,则添加模糊查询条件 + if (!StringUtils.isEmpty(student.getName())) { + // 添加学生姓名的模糊查询条件,对应数据库表中的 name 字段 + studentQueryWrapper.like("name", student.getName()); } + + // 调用 BaseMapper 的 selectPage 方法,根据分页信息和查询条件进行分页查询 Page studentPage1 = baseMapper.selectPage(studentPage, studentQueryWrapper); return studentPage1; } -} +} \ No newline at end of file