Update StudentServiceImpl.java

main
pveayojnc 4 months ago
parent 2436048a06
commit 1d3538c720

@ -1,49 +1,99 @@
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<StudentMapper, Student> implements StudentService {
/**
*
* @param loginForm
* @return null
*/
@Override
public Student login(LoginForm loginForm) {
//创建QueryWapper对象
// 创建 QueryWrapper 对象,用于构建数据库查询条件
QueryWrapper<Student> 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<Student> 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<Student> getStudentData(Page<Student> studentPage, Student student) {
// 创建 QueryWrapper 对象,用于构建数据库查询条件
QueryWrapper<Student> 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<Student> studentPage1 = baseMapper.selectPage(studentPage, studentQueryWrapper);
return studentPage1;
}

Loading…
Cancel
Save