liusheng
2024-04-22 63ebc0007e9958bd6680c6841a7460b053275790
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.ruoyi.web.controller.system;
 
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysStudent;
import com.ruoyi.system.mapper.SysStudentMapper;
import com.ruoyi.system.service.ISysStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 学生信息Controller
 *
 * @author ruoyi
 * @date 2021-10-28
 */
@RestController
@RequestMapping("/system/student")
public class SysStudentController extends BaseController {
    @Autowired
    private ISysStudentService sysStudentService;
 
    @Autowired
    SysStudentMapper sysStudentMapper;
 
    /**
     * 查询学生信息列表
     */
    // @PreAuthorize("@ss.hasPermi('system:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysStudent sysStudent) {
        startPage();
        List<SysStudent> list = sysStudentService.queryList(sysStudent);
        //List<SysStudent> list = sysStudentMapper.selectSysStudentList(sysStudent);
        //List<SysStudent> list = sysStudentMapper.getStudentByName("测试");
 
 
        //利用stream流进行foreach遍历
        list.stream().forEach(student -> {
            //处理逻辑 打印出所有学生的名单和年龄
            System.out.println(student.getStudentName() + student.getStudentAge());
        });
 
        //利用filter函数进行筛选 获取符合条件的
        List<SysStudent> lista = list.stream().filter(student -> StringUtils.equals(student.getStudentSex(), "1")).collect(Collectors.toList());
 
        lista.stream().forEach(student -> {
            //处理逻辑 打印出所有学生的名单和年龄
            System.out.println(student.getStudentName() + student.getStudentAge());
        });
 
        //对List集合进行去重
        //将username相同的 进行去重
        List<SysStudent> unique = list.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SysStudent::getStudentName))), ArrayList::new));
        unique.stream().forEach(student -> {
            System.out.println("-------------------" + student.getStudentName());
        });
 
        //取出list集合对象中的某一个属性(取出list中的每一个对象的名字组成一个新的集合)
        List<String> usernames = list.stream().map(s -> s.getStudentName()).collect(Collectors.toList());//不取重的
        usernames.stream().forEach(username -> {
            System.out.println("不取重的-------------------" + username);
        });
 
        List<String> names = list.stream().map(SysStudent::getStudentName).distinct().collect(Collectors.toList());//这个是将名字取重之后的
        names.stream().forEach(username -> {
            System.out.println("取重之后-------------------" + username);
        });
 
 
        //查询姓名不为空的记录
        List<String> IsEmptyUsernameList = list.stream().map(s -> s.getStudentName()).filter(s -> !s.isEmpty()).collect(Collectors.toList());
        System.out.println(IsEmptyUsernameList);
        //根据其中的某一属性值进行计算
        //(获取年龄的最大值、最小值、平均值、综合、个数)
        IntSummaryStatistics resultNum =  list.stream().mapToInt((s) -> s.getStudentAge()).summaryStatistics();
        //个数
        System.out.println(resultNum.getCount());
        //总大小
        System.out.println(resultNum.getSum());
        //最大值
        System.out.println(resultNum.getMax());
        //最小值
        System.out.println(resultNum.getMin());
        //平均值
        System.out.println(resultNum.getAverage());
 
        return getDataTable(list);
    }
 
    /**
     * 导出学生信息列表
     */
    // @PreAuthorize("@ss.hasPermi('system:student:export')")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysStudent sysStudent) {
        List<SysStudent> list = sysStudentService.queryList(sysStudent);
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class);
        return util.exportExcel(list, "学生信息数据");
    }
 
    /**
     * 获取学生信息详细信息
     */
    // @PreAuthorize("@ss.hasPermi('system:student:query')")
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") Long studentId) {
        return AjaxResult.success(sysStudentService.getById(studentId));
    }
 
    /**
     * 新增学生信息
     */
    // @PreAuthorize("@ss.hasPermi('system:student:add')")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody SysStudent sysStudent) {
        return toAjax(sysStudentService.save(sysStudent));
    }
 
    /**
     * 修改学生信息
     */
    // @PreAuthorize("@ss.hasPermi('system:student:edit')")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @RepeatSubmit
    public AjaxResult edit(@RequestBody SysStudent sysStudent) {
        return toAjax(sysStudentService.updateById(sysStudent));
    }
 
    /**
     * 删除学生信息
     */
    // @PreAuthorize("@ss.hasPermi('system:student:remove')")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{studentIds}")
    public AjaxResult remove(@PathVariable Long[] studentIds) {
        return toAjax(sysStudentService.removeByIds(Arrays.asList(studentIds)));
    }
}