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,这里不需要重复定义
   
}
复制代码
相关推荐
candyTong4 小时前
Claude Code 的 Edit 工具是怎么工作的
javascript·后端·架构
GetcharZp5 小时前
GitHub 2.4 万 Star!D2 正在重新定义程序员画图方式
后端
zhangxingchao7 小时前
多 Agent 架构到底怎么选?从 Claude Agent Teams、Cognition/Devin 到工程落地原则
前端·人工智能·后端
IT_陈寒7 小时前
SpringBoot那个自动配置的坑,害我排查到凌晨三点
前端·人工智能·后端
ServBay7 小时前
OpenCode 和它的7款必备插件
后端·github·ai编程
ping某7 小时前
逐字节拆解 tcpdump
后端
阿凡9807307 小时前
花 100 dollar,用 Claude 打通 EasyEDA&Fusion 双向同步
后端·程序员
irving同学462387 小时前
从零搭建生产级 RAG:Embedding、Chunking、Hybrid Search 与 Reranker
前端·后端
她的男孩7 小时前
从零搭一个企业后台,为什么我把能力拆成 Starter 和 Plugin
java·后端·架构
胡志辉7 小时前
本地 AI 编码助手从 0 配起来:先选模型,再接 Ollama、VS Code、Claude Code 和 Codex
前端·后端