使用 MySQLLambdaQueryWrapper 实现先排序后去重

在数据处理中,排序和去重是常见的操作。在使用 MyBatis-Plus 的时候,我们可以利用 LambdaQueryWrapper 来实现这一功能。本文将通过一个实例来说明如何使用 MySQLLambdaQueryWrapper 来实现先排序后去重的流程。

流程概述

下面的表格概述了实现这一目标的步骤:

步骤 描述
1 创建数据库表
2 插入数据
3 使用 LambdaQueryWrapper 进行排序和去重操作
4 运行并验证结果

步骤详解

1. 创建数据库表

首先,我们需要在 MySQL 中创建一个表。例如,创建一个学生信息表 student,其中包含 idnamescore

CREATE TABLE student (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    score INT NOT NULL
);

这条 SQL 语句创建了一个名为 student 的表,包含三个字段:id(自增主键)、name(学生姓名)和 score(学生分数)。

2. 插入数据

接下来,向 student 表中插入一些数据。

INSERT INTO student (name, score) VALUES ('Alice', 90);
INSERT INTO student (name, score) VALUES ('Bob', 85);
INSERT INTO student (name, score) VALUES ('Alice', 95);
INSERT INTO student (name, score) VALUES ('Charlie', 90);

以上 SQL 语句将四名学生的信息插入到 student 表中,其中 'Alice' 的分数有两个不同的值。

3. 使用 LambdaQueryWrapper 进行排序和去重操作

我们可以使用 MyBatis-Plus 的 LambdaQueryWrapper 操作来实现先排序后去重。以下是主要代码实现。

代码结构
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.entity.Student;
import com.example.demo.mapper.StudentMapper;

import java.util.List;

public class StudentService {
    private final StudentMapper studentMapper;

    public StudentService(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    public List<Student> getSortedUniqueStudents() {
        // 创建一个 LambdaQueryWrapper 对象
        LambdaQueryWrapper<Student> queryWrapper = Wrappers.lambdaQuery();

        // 添加排序条件,按分数升序排列
        queryWrapper.orderByAsc(Student::getScore);

        // 使用 distinct 来去除重复的记录
        // 注意:MyBatis-Plus 本身并不支持直接在 QueryWrapper 中使用 distinct,
        // 应该用 SQL 的 distinct 操作
        // 这里的方案是先获取数据集合后再去重
        List<Student> students = studentMapper.selectList(queryWrapper);

        // 使用 Java 的 distinct 来去除重复
        return students.stream()
                       .distinct()
                       .toList();
    }
}

StudentService 类中,我们使用 LambdaQueryWrapper 创建查询条件。首先,我们通过 orderByAsc 方法对分数进行升序排序。然后,我们使用 stream()distinct() 方法来去掉重复的记录。

4. 运行并验证结果

最后,我们需要运行这个方法并验证结果。在我们的主类或测试类中,调用 getSortedUniqueStudents() 方法。

public class Main {
    public static void main(String[] args) {
        // 假设我们已经有一个 Spring Boot 项目并且 Autowired 了 StudentService
        StudentService studentService = new StudentService(new StudentMapper());

        // 获取排序和去重后的学生列表
        List<Student> uniqueSortedStudents = studentService.getSortedUniqueStudents();

        // 输出结果
        for (Student student : uniqueSortedStudents) {
            System.out.println("Name: " + student.getName() + ", Score: " + student.getScore());
        }
    }
}

以上代码演示了如何调用 getSortedUniqueStudents() 方法,并打印结果。

类图

为了更好地理解代码结构,以下是该实现的类图。

classDiagram
    class Student {
        +int id
        +String name
        +int score
    }
    
    class StudentMapper {
        +List<Student> selectList(LambdaQueryWrapper<Student> queryWrapper)
    }
    
    class StudentService {
        +List<Student> getSortedUniqueStudents()
    }
    
    StudentService --> StudentMapper
    Student <-- StudentMapper

类图展示了 Student, StudentMapperStudentService 三个类之间的关系。

总结

在本教程中,我们学习了如何使用 MySQLLambdaQueryWrapper 来实现先排序后去重。首先,我们创建了数据库表,并插入了数据。然后,使用 LambdaQueryWrapper 进行排序和去重的操作,最后通过运行代码来验证结果。这样的流程和代码实现对于刚入行的小白来说,是一种很好的学习方法。希望你能在实际项目中灵活应用这些技巧,并继续深入学习 MyBatis-Plus 的其他功能。