ssm

SSM

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

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

mp分页配置文件

java 复制代码
// 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类,用于前端传递给后端的查询参数

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类,数据库字段。也是后端返回给前端的实体类

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 表现层

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

java 复制代码
@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 业务层

java 复制代码
//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 数据访问层

java 复制代码
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
 //selectPage方法已经继承自BaseMapper,这里不需要重复定义
   
}
复制代码
相关推荐
仿生joe会梦见漫天的大雪吗5 分钟前
CTF学习笔记03:密码口令 —— 从弱口令到字典爆破
后端
自进化Agent智能体7 分钟前
从零到一玩转Hermes Agent:VPS部署 × 模型配置 × 记忆架构 × 多Agent协作
后端
用户4682557459138 分钟前
Testcontainers 在 Windows Docker Desktop 上跑不通:协议层不兼容 + 4 种可行环境
java·后端
Tenaryo12 分钟前
「底层系统基石 · 缓存篇」V —— 写策略、Store Buffer 与内存屏障
后端·面试
小刘|1 小时前
Spring WebFlux + AI 流式输出深度解析:Spring AI 与 LangChain4j 效果差异溯源
java·后端·spring
夕除1 小时前
Spring Security 配置类(SecurityConfig)
java·后端·spring
lfwh1 小时前
探针程序技术解析:基于 Spring Boot 非 Web 模式的云服务监控告警系统
前端·spring boot·后端
武子康1 小时前
Java-22 深入浅出 MyBatis - 手写ORM框架3 手写SqlSession、Executor 工作原理
java·后端
ikoala1 小时前
Codex 不得不装的 12 个插件,都在这了
前端·javascript·后端