@RequestMapping("/myDataList") public String myDataList(Model model, HttpSession session) { //判断是否登陆 if (session.getAttribute("loginUserinfo") == null) { model.addAttribute("errorMsg", "登陆以后才可以操作"); return "/"; } Student student = (Student) session.getAttribute("loginUserinfo"); if (ObjectUtils.isNotEmpty(student.getDormitoryId())) { LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>(); studentLambdaQueryWrapper.eq(Student::getDormitoryId, student.getDormitoryId()); List<Student> studentList = studentService.list(studentLambdaQueryWrapper); Dormitory dormitory = dormitoryService.getById(student.getDormitoryId()); List<String> stringList = studentList.stream().map(Student::getName).collect(Collectors.toList()); String[] ans2 = stringList.toArray(new String[stringList.size()]); dormitory.setDormUser(String.join(",",ans2)); session.setAttribute("dormUserinfoList", studentList); model.addAttribute("dorminfo", dormitory); } return "myDataList"; }请详细解释每一行代码
时间: 2023-06-26 14:05:23 浏览: 87
好的,我来一行一行地解释这段代码的含义:
```
@RequestMapping("/myDataList")
public String myDataList(Model model, HttpSession session) {
```
这里定义了一个请求处理的方法,请求路径为"/myDataList",方法名为"myDataList",接受两个参数:Model和HttpSession。
```
if (session.getAttribute("loginUserinfo") == null) {
model.addAttribute("errorMsg", "登陆以后才可以操作");
return "/";
}
```
这里判断用户是否登录,如果没有登录则将错误信息放入Model中并返回首页"/"。
```
Student student = (Student) session.getAttribute("loginUserinfo");
```
从Session中获取已登录的学生信息。
```
if (ObjectUtils.isNotEmpty(student.getDormitoryId())) {
LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
studentLambdaQueryWrapper.eq(Student::getDormitoryId, student.getDormitoryId());
List<Student> studentList = studentService.list(studentLambdaQueryWrapper);
```
如果学生信息中宿舍ID不为空,则根据宿舍ID查询所有同宿舍的学生信息。
```
Dormitory dormitory = dormitoryService.getById(student.getDormitoryId());
List<String> stringList = studentList.stream().map(Student::getName).collect(Collectors.toList());
String[] ans2 = stringList.toArray(new String[stringList.size()]);
dormitory.setDormUser(String.join(",",ans2));
```
通过宿舍ID获取宿舍信息,将同宿舍的学生姓名放入数组中,并将数组转换成字符串,最后将字符串设置为宿舍信息中的"dormUser"属性。
```
session.setAttribute("dormUserinfoList", studentList);
model.addAttribute("dorminfo", dormitory);
```
将同宿舍的学生信息列表放入Session中,并将宿舍信息放入Model中。
```
return "myDataList";
```
返回"myDataList"页面。
阅读全文