ssm

25 阅读1分钟

SSM

使用spring-boot-starter-parent 3.2.0 和 mybatis-plus-spring-boot3-starter 3.5.7实现学生管理系统的增删改查功能。

查询:需要支持分页和多条件查询

mp分页配置文件

// MybatisPlusConfig
package com.example.demo.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

dto

在dto目录下定义StudentQueryDTO JAVA类,用于前端传递给后端的查询参数

@Data
@TableName("student")
public class StudentQueryDTO {
    @TableId(type = IdType.AUTO)
    private int id;
    @TableField("name")
    private String name;
    private int age;
    private String className;
    private Integer minScore;   // 最低分
    private Integer maxScore;   // 最高分    

    // 分页
    private Integer pageNum = 1;
    private Integer pageSize = 10;
}

entity

在entity目录下定义StudentQueryEntity JAVA类,数据库字段。也是后端返回给前端的实体类

@Data
@TableName("student")
public class Student {
    @TableId(type = IdType.AUTO)
    private int id;
    private String name;
    private int age;
    private String idNo;
    private String className;
    private int score;
}

controller 表现层

返回给前端的一些接口定义都在这里

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService StudentService;
    @PostMapping("/query/page")
    public Page<Student> getStudentByPage(@RequestBody StudentQueryDTO dto) {
        return StudentService.queryStudentByPage(dto);
    }
}

service 业务层

//StudentService.java 业务层接口
public interface StudentService {
    Page<Student> queryStudentByPage(StudentQueryDTO dto);
}

//StudentServiceImpl.java 业务层实现类
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public Page<Student> queryStudentByPage(StudentQueryDTO dto) {
        // 1. 构建分页对象
        Page<Student> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        // 2. 构建多条件查询
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(dto.getName()), Student::getName, dto.getName());
        wrapper.eq(StringUtils.isNotBlank(dto.getClassName()), Student::getClassName, dto.getClassName());
        wrapper.ge(dto.getMinScore() != null, Student::getScore, dto.getMinScore());
        wrapper.le(dto.getMaxScore() != null, Student::getScore, dto.getMaxScore());
        // 3. 执行查询
        return studentMapper.selectPage(page, wrapper);
    }
}

mapper 数据访问层

@Mapper
public interface StudentMapper extends BaseMapper<Student> {
 //selectPage方法已经继承自BaseMapper,这里不需要重复定义
   
}