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,这里不需要重复定义
   
}
复制代码
相关推荐
苏三说技术26 分钟前
LangChain4j 和 LangGraph4j,哪个更好?
后端
ServBay2 小时前
7 个AI开发中真正用得上的 MCP Server,配合Claude Code食用效果更佳
后端·claude·mcp
妙码生花2 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十五):优化细节、网络请求封装
前端·后端·ai编程
用户6757049885022 小时前
Go 语言里判断字符串为空,90% 的人都写错了!
后端·go
用户6757049885023 小时前
Go 进阶必修:90% 的人都没用对的“表驱动法”
后端·go
小兔崽子去哪了3 小时前
Java 生成二维码解决方案
java·后端
苍何3 小时前
懂事的 Agent 已经开始自己看屏幕干活了,效率起飞!
后端
掘金码甲哥3 小时前
1分钟买不了吃亏系列: nginx动态域名解析
后端
神奇小汤圆3 小时前
2026大厂Java岗面试记录:八股+场景+项目+AI,一文讲透快速上岸路径(含答案)
后端
神奇小汤圆4 小时前
我说MySQL每张表最好不超过2000万条数据,面试官让我回去等通知?
后端