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,这里不需要重复定义
   
}
复制代码
相关推荐
我的xiaodoujiao12 分钟前
Django 基础知识详细图文教程 4-Django 视图定义与使用
开发语言·数据库·后端·测试工具·django·sqlite
Apifox25 分钟前
Apifox 8 月更新|调试、权限与协作体验持续优化
前端·后端·测试
kyson_27 分钟前
后端登录凭证设计:Session-Cookie、Redis-Token 与 JWT
后端
对象存储与RustFS28 分钟前
RustFS 后台扫描器与自愈调参:五档速度、位腐检测周期与并发上限
后端·rust·开源
天天喝旺仔38 分钟前
Docker 镜像瘦身实战:多阶段构建把体积缩小 90%
运维·后端·ci/cd·docker·云原生·容器·性能优化
Lost of 程序猿43 分钟前
ASP.NET Core Saga 分布式事务深度实战:备件采购跨服务长流程,如何保证“要么全成,要么全回“
分布式·后端·asp.net
卷无止境1 小时前
Coding Agent 里的上下文 Compact,到底在压缩什么
后端·python
爱勇宝1 小时前
公司没给活干,却因为员工看手机把人开了:法院判赔11万元
前端·后端·程序员
2601_962071001 小时前
【Java EE】SpringBoot的创建与简单使用
spring boot·后端·java-ee
Csvn1 小时前
🐍 Day 7:文件 I/O 实战
后端