基于javaweb+mysql的springboot宿舍管理系统(java+springboot+thymeleaf+html+layui+mysql)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SpringBoot宿舍管理系统(java+springboot+thymeleaf+html+layui+mysql)
项目介绍
宿舍管理系统,分为系统管理员与学生两种角色; 系统管理员主要功能包括: 系统管理:用户列表、角色与权限; 学生管理:学生列表、教学班级; 宿舍管理:寝室列表、寝室类型; 入住管理:住宿申请、宿舍分配详情; 学生主要功能包括:
申请入住、个人信息管理;
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。 2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA; 3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可 4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.是否Maven项目: 是;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目
6.数据库:MySql 5.7版本;
技术栈
(1)Spring Boot v2.3.1 (2)Spring Data JPA 的 hibernate实现 (3)shiro 用于授权与认证 (4)Thymeleaf+html 服务器端模板引擎 (5)layui 布局前端界面
(6)jQuery 简化Dom操作与Ajax请求
使用说明
- 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件; 2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,配置tomcat,然后运行; 3. 将项目中application.yml配置文件中的数据库配置改为自己的配置 4. 运行项目,输入localhost:8080/ 登录 5. 管理员账户:admin 密码:123
学生账户:0123 密码:123
if (result.isSuccess()) {
val roleId = result.getValue().getRole().getId();
switch (roleId.intValue()) {
case 1: // 学生角色的id是1
return "redirect:/index";
case 2: // 管理员角色的id是2
return "redirect:/admin/index";
default:
attributes.addFlashAttribute("username", username);
attributes.addFlashAttribute("error", "未知角色");
return "redirect:/login";
}
} else {
attributes.addFlashAttribute("username", username);
attributes.addFlashAttribute("error", result.getMsg());
return "redirect:/login";
}
}
@ApiOperation("安全退出")
@RequestMapping(value = "/logout", method = {RequestMethod.GET})
public String logout(RedirectAttributes attributes) {
val result = userService.signOut();
if (result.isSuccess()) {
val username = result.getValue().getUsername();
attributes.addFlashAttribute("info", (username == null ? "未知用户" : username) + "已安全退出");
}
return "redirect:/login";
}
@GetMapping(value = {"/admin/user-list"})
public String toAdminUserList(Model model) {
model.addAttribute("roles", roleService.listAllRoles());
return "admin/user-list"; // Thymeleaf模板的名字,表示 templates/admin/user-list.html
}
@ApiOperation("ajax:分页查询用户信息")
@RequestMapping(value = "/admin/users", method = {RequestMethod.POST})
@ResponseBody
public Page<User> fetchPage(@RequestBody PageRequest pageRequest) {
return userService.fetchPage(pageRequest);
}
}
@Override
public void refreshNoHandleCount() {
if (session.getAttribute(NO_HANDLE_COUNT) != null) {
session.removeAttribute(NO_HANDLE_COUNT);
}
session.setAttribute(NO_HANDLE_COUNT, roomRequestService.getNoHandleCount());
}
@Override
public Integer getNoHandleCount() {
return (Integer) this.get(NO_HANDLE_COUNT);
}
}
package com.demo.controller;
@Controller
public class IndexController {
@Resource
private RoomService roomService;
@Resource
private CategoryService categoryService;
@GetMapping(value = {"/login"})
public String toLogin() {
return "login"; // Thymeleaf模板的名字,表示 templates/login.html
}
@GetMapping(value = {"/", "/index"})
public String toIndex(Model model) {
val room = roomService.getCurrentStudentRoom();
}
}
package com.demo.controller;
/**
* 将JSON返回值统一包装成LayuiResult对象
* 注:对使用了@ResponseBody的Restful接口有效
*/
@RestControllerAdvice(basePackages = {"com.demo.controller"})
public class Advice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, @NonNull Class<? extends HttpMessageConverter<?>> aClass) {
// 如果接口返回的类型已经是LayuiResult,则无需转换
val returnTypeName = returnType.getGenericParameterType().getTypeName();
val layuiResultTypeName = LayuiResult.class.getTypeName();
return !returnTypeName.startsWith(layuiResultTypeName); // 前者包含泛型信息,后者不包含,所以用startsWith而不是equals
}
// String =>beforeBodyWrite(包装成LayuiResult,手动转json) => String;
// 其它 =>beforeBodyWrite(包装成LayuiResult) => LayuiResult => springMVC自动转json => String;
@Override
public Object beforeBodyWrite(Object data, @NonNull MethodParameter returnType, @NonNull MediaType mediaType, @NonNull Class<? extends HttpMessageConverter<?>> aClass, @NonNull ServerHttpRequest serverHttpRequest, @NonNull ServerHttpResponse serverHttpResponse) {
// 如果controller层中返回的类型是String,那么springMVC在选择处理MessageConverter时会选择StringMessageConverter。
// 问题在于StringMessageConverter只接受String类型,不能处理包装后的LayuiResult类型,所以要转成json字符串作为返回值
if (data instanceof String) {
// String类型
@Api("寝室相关api")
@Controller
public class RoomController {
@Resource
private RoomService roomService;
@Resource
private CategoryService categoryService;
@GetMapping(value = {"/admin/room-list"})
public String toAdminRoomList(Model model) {
model.addAttribute("categories", categoryService.listAllCategories());
return "admin/room-list"; // Thymeleaf模板的名字,表示 templates/admin/room-list.html
}
@ApiOperation("ajax:分页查询寝室信息")
@RequestMapping(value = "/admin/rooms", method = {RequestMethod.POST})
@ResponseBody
public Page<Room> fetchPage(@RequestBody PageRequest pageRequest) {
return roomService.fetchPage(pageRequest);
}
@ApiOperation("ajax:根据id查询寝室")
@GetMapping("/admin/room/{id}")
@ResponseBody
public Room getRoomById(@PathVariable Long id) {
return roomService.getRoomById(id);
}
@ApiOperation("跳转到寝室详情界面")
@GetMapping("/admin/room/detail")
public String toDisplayRoomDetailById(@RequestParam(defaultValue = "") Long id, Model model) {
val room = roomService.getRoomWithStudentsById(id);
if (room != null) {
@Api("教学班级相关api")
@Controller
public class TeachingClassController {
@Resource
private TeachingClassService teachingClassService;
@GetMapping(value = {"/admin/teaching-class-list"})
public String toAdminTeachingClassList() {
return "admin/teaching-class-list"; // Thymeleaf模板的名字,表示 templates/admin/teaching-class-list.html
}
@ApiOperation("ajax:分页查询班级信息")
@RequestMapping(value = "/admin/teaching-classes", method = {RequestMethod.POST})
@ResponseBody
public Page<TeachingClass> fetchPage(@RequestBody PageRequest pageRequest) {
return teachingClassService.fetchPage(pageRequest);
}
@ApiOperation("ajax:根据id查询班级")
@GetMapping("/admin/teaching-class/{id}")
@ResponseBody
public TeachingClass getTeachingClassById(@PathVariable Long id) {
return teachingClassService.getTeachingClassById(id);
}
@ApiOperation("跳转到班级编辑界面")
@GetMapping("/admin/teaching-class/edit")
public String toEditTeachingClassById(@RequestParam(defaultValue = "-1") long id, Model model) {
val teachingClass = teachingClassService.getTeachingClassById(id);
if (teachingClass != null) {
model.addAttribute("operation", "编辑班级");
model.addAttribute("teachingClass", teachingClass);
} else {
return "redirect:/admin/user-list";
}
}
@ApiOperation("跳转到用户添加界面")
@GetMapping("/admin/user/create")
public String toCreateUser(Model model) {
model.addAttribute("operation", "添加用户");
model.addAttribute("user", new User());
model.addAttribute("roles", roleService.listAllRoles());
return "admin/user-input";
}
@ApiOperation("保存用户")
@GetMapping("/admin/user/save")
public String saveUser(User user, RedirectAttributes attributes) {
val result = userService.saveUser(user);
if (result.isSuccess()) {
attributes.addFlashAttribute("info", "操作成功");
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",保存用户失败");
}
return "redirect:/admin/user-list";
}
@ApiOperation("ajax:根据若干id删除用户")
@RequestMapping(value = "/admin/user/delete", method = {RequestMethod.POST})
@ResponseBody
public LayuiResult<String> deleteUserByIds(String ids) {
val idList = parseLongList(ids);
if (userService.deleteUserByIds(idList).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
return new LayuiResult<>(FAILED, null, null);
}
}
@ApiOperation("修改个人信息")
@GetMapping("/user/update-info")
public String updateUserInfo(User user, RedirectAttributes attributes) {
val result = userService.updateUser(user);
if (result.isSuccess()) {
attributes.addFlashAttribute("info", "操作成功");
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",更新信息失败");
}
return "redirect:/login";
}
@ApiOperation("跳转到用户添加界面")
@GetMapping("/admin/user/create")
public String toCreateUser(Model model) {
model.addAttribute("operation", "添加用户");
model.addAttribute("user", new User());
model.addAttribute("roles", roleService.listAllRoles());
return "admin/user-input";
}
@ApiOperation("保存用户")
@GetMapping("/admin/user/save")
public String saveUser(User user, RedirectAttributes attributes) {
val result = userService.saveUser(user);
if (result.isSuccess()) {
attributes.addFlashAttribute("info", "操作成功");
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",保存用户失败");
}
return "redirect:/admin/user-list";
}
@ApiOperation("ajax:根据若干id删除用户")
@RequestMapping(value = "/admin/user/delete", method = {RequestMethod.POST})
@ResponseBody
public LayuiResult<String> deleteUserByIds(String ids) {
val idList = parseLongList(ids);
if (userService.deleteUserByIds(idList).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
return new LayuiResult<>(FAILED, null, null);
}
}
@ApiOperation("修改个人信息")
@GetMapping("/user/update-info")
public String updateUserInfo(User user, RedirectAttributes attributes) {
val result = userService.updateUser(user);
if (result.isSuccess()) {
attributes.addFlashAttribute("info", "操作成功");
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",更新信息失败");
}
return "redirect:/login";
}
}
package com.demo.controller;
/**
* 全局异常处理
*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/**
* 处理自定义的异常 ApiException
*/
@ExceptionHandler(ApiException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public LayuiResult<String> ApiExceptionHandler(ApiException e) {
log.error("api异常");
List<String> msgList = new ArrayList<>();
msgList.add(e.getMsg());
return new LayuiResult<>(LayuiResult.ResultCode.FAILED, null, msgList);
}
/**
* 处理方法参数错误的异常 MethodArgumentNotValidException
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public LayuiResult<String> MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
log.error("方法参数错误异常");
List<String> msgList = new ArrayList<>();
if (!e.getBindingResult().getAllErrors().isEmpty()) {
for (ObjectError error : e.getBindingResult().getAllErrors()) {
msgList.add(error.getDefaultMessage());
}
}
// 然后提取错误提示信息进行返回
return new LayuiResult<>(LayuiResult.ResultCode.VALIDATE_FAILED, null, msgList);
}
}
package com.demo.controller;
@ApiOperation("保存寝室类型")
@GetMapping("/admin/category/save")
public String saveCategory(Category category, RedirectAttributes attributes) {
val result = categoryService.saveCategory(category);
if (result.isSuccess()) {
attributes.addFlashAttribute("info", "操作成功");
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",保存寝室类型失败");
}
return "redirect:/admin/category-list";
}
@ApiOperation("ajax:根据若干id删除寝室类型")
@RequestMapping(value = "/admin/category/delete", method = {RequestMethod.POST})
@ResponseBody
public LayuiResult<String> deleteCategoryByIds(String ids) {
val idList = parseLongList(ids);
if (categoryService.deleteCategoryByIds(idList).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
return new LayuiResult<>(FAILED, null, null);
}
}
}
package com.demo.controller;
/**
* 将JSON返回值统一包装成LayuiResult对象
* 注:对使用了@ResponseBody的Restful接口有效
*/
@RestControllerAdvice(basePackages = {"com.demo.controller"})
public class Advice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, @NonNull Class<? extends HttpMessageConverter<?>> aClass) {
@Api("图片文件上传api")
@Controller
@Slf4j
public class FileUploadController {
private final static String NAME = "file";
@Resource
private PictureConfig pictureConfig;
/**
* 本地图片文件上传接口 "/upload"
*
* @param request 图片文件上传请求,要求参数名是 file, (例如:用原生form提交,input标签需要添加 name="file" )
* @return JSON格式的对象, code == 0 表示上传成功 , code == 1 表示上传失败
*/
@ApiOperation("ajax:本地图片文件上传")
@PostMapping("/upload")
@ResponseBody
public LayuiResult<Object> upload(HttpServletRequest request) {
MultipartHttpServletRequest mRequest;
if (request instanceof MultipartHttpServletRequest) {
mRequest = (MultipartHttpServletRequest) request;
} else {
return result(1, "failure:请求异常", null);
}
val multipartFile = mRequest.getFile(NAME);
if (null == multipartFile) {
return result(1, "failure:参数异常,请检查参数名是否为" + NAME, null);
}
val originalFilename = multipartFile.getOriginalFilename();
if (StringUtils.isEmpty(originalFilename)) {
return result(1, "failure:文件名为空", null);
}
val path = pictureConfig.getPath();
val dest = new File(path + originalFilename);
if (!dest.getParentFile().exists()) {
if (!dest.getParentFile().mkdirs()) {
return result(1, "failure:服务器存储路径创建失败", null);
}
return "admin/room-request-input";// Thymeleaf模板的名字,表示 templates/admin/room-request-input.html
} else {
return "redirect:/admin/room-request-list";
}
}
@ApiOperation("跳转到住宿申请添加界面")
@GetMapping("/admin/room-request/create")
public String toCreateRoomRequest(Model model) {
model.addAttribute("operation", "添加住宿申请");
model.addAttribute("roomRequest", new RoomRequest());
model.addAttribute("statuses", roomRequestService.listAllStatuses());
model.addAttribute("students", studentService.listAllStudents());
model.addAttribute("rooms", roomService.listAllRooms());
return "admin/room-request-input";
}
@ApiOperation("保存住宿申请")
@GetMapping("/admin/room-request/save")
public String saveRoomRequest(RoomRequest roomRequest, RedirectAttributes attributes) {
val result = roomRequestService.saveRoomRequest(roomRequest);
if (result.isSuccess()) {
attributes.addFlashAttribute("info", "操作成功");
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",保存住宿申请失败");
}
return "redirect:/admin/room-request-list";
}
@ApiOperation("ajax:根据若干id删除住宿申请")
@RequestMapping(value = "/admin/room-request/delete", method = {RequestMethod.POST})
@ResponseBody
public LayuiResult<String> deleteRoomRequestByIds(String ids) {
val idList = parseLongList(ids);
if (roomRequestService.deleteRoomRequestByIds(idList).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
return new LayuiResult<>(FAILED, null, null);
}
}
@ApiOperation("新增住宿申请")
@GetMapping("/room-request/new")
@ResponseBody
public LayuiResult<String> addNewRoomRequest(String studentId, Long roomId) {
if (roomRequestService.newRoomRequest(studentId, roomId).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
if (roomRequest != null) {
model.addAttribute("operation", "编辑住宿申请");
model.addAttribute("roomRequest", roomRequest);
model.addAttribute("statuses", roomRequestService.listAllStatuses());
model.addAttribute("students", studentService.listAllStudents());
model.addAttribute("rooms", roomService.listAllRooms());
return "admin/room-request-input";// Thymeleaf模板的名字,表示 templates/admin/room-request-input.html
} else {
return "redirect:/admin/room-request-list";
}
}
@ApiOperation("跳转到住宿申请添加界面")
@GetMapping("/admin/room-request/create")
public String toCreateRoomRequest(Model model) {
model.addAttribute("operation", "添加住宿申请");
model.addAttribute("roomRequest", new RoomRequest());
model.addAttribute("statuses", roomRequestService.listAllStatuses());
model.addAttribute("students", studentService.listAllStudents());
model.addAttribute("rooms", roomService.listAllRooms());
return "admin/room-request-input";
}
@ApiOperation("保存住宿申请")
@GetMapping("/admin/room-request/save")
public String saveRoomRequest(RoomRequest roomRequest, RedirectAttributes attributes) {
val result = roomRequestService.saveRoomRequest(roomRequest);
if (result.isSuccess()) {
attributes.addFlashAttribute("info", "操作成功");
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",保存住宿申请失败");
}
return "redirect:/admin/room-request-list";
}
@ApiOperation("ajax:根据若干id删除住宿申请")
@RequestMapping(value = "/admin/room-request/delete", method = {RequestMethod.POST})
@ResponseBody
public LayuiResult<String> deleteRoomRequestByIds(String ids) {
val idList = parseLongList(ids);
if (roomRequestService.deleteRoomRequestByIds(idList).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
return new LayuiResult<>(FAILED, null, null);
}
}
@ApiOperation("新增住宿申请")
}
}
package com.demo.controller;
@Api("学生相关api")
@Controller
public class StudentController {
@Resource
private StudentService studentService;
@Resource
private TeachingClassService teachingClassService;
@GetMapping(value = {"/admin/student-list"})
public String toAdminStudentList(Model model) {
model.addAttribute("teachingClasses", teachingClassService.listAllTeachingClasses());
return "admin/student-list"; // Thymeleaf模板的名字,表示 templates/admin/student-list.html
}
@ApiOperation("ajax:分页查询学生信息")
@RequestMapping(value = "/admin/students", method = {RequestMethod.POST})
@ResponseBody
public Page<Student> fetchPage(@RequestBody PageRequest pageRequest) {
return studentService.fetchPage(pageRequest);
}
@ApiOperation("ajax:根据id查询学生")
@GetMapping("/admin/student")
@ResponseBody
@Override
public void refreshNoHandleCount() {
if (session.getAttribute(NO_HANDLE_COUNT) != null) {
session.removeAttribute(NO_HANDLE_COUNT);
}
session.setAttribute(NO_HANDLE_COUNT, roomRequestService.getNoHandleCount());
}
@Override
public Integer getNoHandleCount() {
return (Integer) this.get(NO_HANDLE_COUNT);
}
}
package com.demo.controller;
@Controller
public class IndexController {
@Resource
private RoomService roomService;
@Resource
private CategoryService categoryService;
@GetMapping(value = {"/login"})
public String toLogin() {
return "login"; // Thymeleaf模板的名字,表示 templates/login.html
}
@GetMapping(value = {"/", "/index"})
public String toIndex(Model model) {
val room = roomService.getCurrentStudentRoom();
model.addAttribute("currentRoom", room);
model.addAttribute("categories", categoryService.listAllCategories());
return "index"; // Thymeleaf模板的名字,表示 templates/index.html
}
@Api("住宿申请相关api")
@Controller
public class RoomRequestController {
@Resource
private RoomRequestService roomRequestService;
@Resource
private RoomService roomService;
@Resource
private StudentService studentService;
@GetMapping(value = {"/admin/room-request-list"})
public String toAdminRoomRequestList(Model model) {
model.addAttribute("statuses", roomRequestService.listAllStatuses());
return "admin/room-request-list"; // Thymeleaf模板的名字,表示 templates/admin/room-request-list.html
}
@ApiOperation("ajax:分页查询住宿申请信息")
@RequestMapping(value = "/admin/room-requests", method = {RequestMethod.POST})
@ResponseBody
public Page<RoomRequest> fetchPage(@RequestBody PageRequest pageRequest) {
return roomRequestService.fetchPage(pageRequest);
}
@ApiOperation("ajax:根据id查询住宿申请")
@GetMapping("/admin/room-request/{id}")
@ResponseBody
public RoomRequest getRoomRequestById(@PathVariable Long id) {
return roomRequestService.getRoomRequestById(id);
}
@ApiOperation("跳转到住宿申请编辑界面")
@GetMapping("/admin/room-request/edit")
public String toEditRoomRequestById(@RequestParam(defaultValue = "-1") long id, Model model) {
val roomRequest = roomRequestService.getRoomRequestById(id);
if (roomRequest != null) {
model.addAttribute("operation", "编辑住宿申请");
model.addAttribute("roomRequest", roomRequest);
model.addAttribute("statuses", roomRequestService.listAllStatuses());
model.addAttribute("students", studentService.listAllStudents());
model.addAttribute("rooms", roomService.listAllRooms());
return "admin/room-request-input";// Thymeleaf模板的名字,表示 templates/admin/room-request-input.html
} else {
return "redirect:/admin/room-request-list";
}
@ApiOperation("新增住宿申请")
@GetMapping("/room-request/new")
@ResponseBody
public LayuiResult<String> addNewRoomRequest(String studentId, Long roomId) {
if (roomRequestService.newRoomRequest(studentId, roomId).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
return new LayuiResult<>(FAILED, null, null);
}
}
}
package com.demo.controller;
@Api("学生相关api")
@Controller
public class StudentController {
@Resource
private StudentService studentService;
@Resource
private TeachingClassService teachingClassService;
@GetMapping(value = {"/admin/student-list"})
public String toAdminStudentList(Model model) {
model.addAttribute("teachingClasses", teachingClassService.listAllTeachingClasses());
return "admin/student-list"; // Thymeleaf模板的名字,表示 templates/admin/student-list.html
}
@ApiOperation("ajax:分页查询寝室信息")
@RequestMapping(value = "/admin/rooms", method = {RequestMethod.POST})
@ResponseBody
public Page<Room> fetchPage(@RequestBody PageRequest pageRequest) {
return roomService.fetchPage(pageRequest);
}
@ApiOperation("ajax:根据id查询寝室")
@GetMapping("/admin/room/{id}")
@ResponseBody
public Room getRoomById(@PathVariable Long id) {
return roomService.getRoomById(id);
}
@ApiOperation("跳转到寝室详情界面")
@GetMapping("/admin/room/detail")
public String toDisplayRoomDetailById(@RequestParam(defaultValue = "") Long id, Model model) {
val room = roomService.getRoomWithStudentsById(id);
if (room != null) {
model.addAttribute("room", room);
return "admin/room-detail";// Thymeleaf模板的名字,表示 templates/admin/room-detail.html
} else {
return "redirect:/admin/room-list";
}
}
@ApiOperation("跳转到寝室编辑界面")
@GetMapping("/admin/room/edit")
public String toEditRoomById(@RequestParam(defaultValue = "-1") long id, Model model) {
val room = roomService.getRoomById(id);
if (room != null) {
model.addAttribute("operation", "编辑寝室");
model.addAttribute("room", room);
model.addAttribute("categories", categoryService.listAllCategories());
return "admin/room-input";// Thymeleaf模板的名字,表示 templates/admin/room-input.html
} else {
return "redirect:/admin/room-list";
}
}
@ApiOperation("跳转到寝室添加界面")
@GetMapping("/admin/room/create")
public String toCreateRoom(Model model) {
model.addAttribute("operation", "添加寝室");
model.addAttribute("room", new Room());
model.addAttribute("categories", categoryService.listAllCategories());
return "admin/room-input";
}
@ApiOperation("保存寝室")
@GetMapping("/admin/room/save")
private UserService userService;
@Resource
private RoleService roleService;
@ApiOperation("登录认证")
@PostMapping("/login-auth")
public String login(String username, String password, RedirectAttributes attributes) {
val result = userService.signIn(username, password);
if (result.isSuccess()) {
val roleId = result.getValue().getRole().getId();
switch (roleId.intValue()) {
case 1: // 学生角色的id是1
return "redirect:/index";
case 2: // 管理员角色的id是2
return "redirect:/admin/index";
default:
attributes.addFlashAttribute("username", username);
attributes.addFlashAttribute("error", "未知角色");
return "redirect:/login";
}
} else {
attributes.addFlashAttribute("username", username);
attributes.addFlashAttribute("error", result.getMsg());
return "redirect:/login";
}
}
@ApiOperation("安全退出")
@RequestMapping(value = "/logout", method = {RequestMethod.GET})
public String logout(RedirectAttributes attributes) {
val result = userService.signOut();
if (result.isSuccess()) {
val username = result.getValue().getUsername();
attributes.addFlashAttribute("info", (username == null ? "未知用户" : username) + "已安全退出");
}
return "redirect:/login";
}
@GetMapping(value = {"/admin/user-list"})
public String toAdminUserList(Model model) {
model.addAttribute("roles", roleService.listAllRoles());
return "admin/user-list"; // Thymeleaf模板的名字,表示 templates/admin/user-list.html
}
@ApiOperation("ajax:分页查询用户信息")
@RequestMapping(value = "/admin/users", method = {RequestMethod.POST})
@ResponseBody
public Page<User> fetchPage(@RequestBody PageRequest pageRequest) {
return userService.fetchPage(pageRequest);
* @return JSON格式的对象, code == 0 表示上传成功 , code == 1 表示上传失败
*/
@ApiOperation("ajax:本地图片文件上传")
@PostMapping("/upload")
@ResponseBody
public LayuiResult<Object> upload(HttpServletRequest request) {
MultipartHttpServletRequest mRequest;
if (request instanceof MultipartHttpServletRequest) {
mRequest = (MultipartHttpServletRequest) request;
} else {
return result(1, "failure:请求异常", null);
}
val multipartFile = mRequest.getFile(NAME);
if (null == multipartFile) {
return result(1, "failure:参数异常,请检查参数名是否为" + NAME, null);
}
val originalFilename = multipartFile.getOriginalFilename();
if (StringUtils.isEmpty(originalFilename)) {
return result(1, "failure:文件名为空", null);
}
val path = pictureConfig.getPath();
val dest = new File(path + originalFilename);
if (!dest.getParentFile().exists()) {
if (!dest.getParentFile().mkdirs()) {
return result(1, "failure:服务器存储路径创建失败", null);
}
}
try {
multipartFile.transferTo(dest);
} catch (Exception e) {
log.error("文件上传失败: error = " + e.getMessage());
return result(1, "failure:文件保存失败", null);
}
return result(0, "success", "/sdms-images/" + originalFilename);
}
private LayuiResult<Object> result(Integer code, String msg, String pictureURL) {
val m = new HashMap<String, Object>();
m.put("pictureURL", pictureURL);
return new LayuiResult<>(code, msg, null, Collections.singletonList(m));
}
}
return "redirect:/index";
case 2: // 管理员角色的id是2
return "redirect:/admin/index";
default:
attributes.addFlashAttribute("username", username);
attributes.addFlashAttribute("error", "未知角色");
return "redirect:/login";
}
} else {
attributes.addFlashAttribute("username", username);
attributes.addFlashAttribute("error", result.getMsg());
return "redirect:/login";
}
}
@ApiOperation("安全退出")
@RequestMapping(value = "/logout", method = {RequestMethod.GET})
public String logout(RedirectAttributes attributes) {
val result = userService.signOut();
if (result.isSuccess()) {
val username = result.getValue().getUsername();
attributes.addFlashAttribute("info", (username == null ? "未知用户" : username) + "已安全退出");
}
return "redirect:/login";
}
@GetMapping(value = {"/admin/user-list"})
public String toAdminUserList(Model model) {
model.addAttribute("roles", roleService.listAllRoles());
return "admin/user-list"; // Thymeleaf模板的名字,表示 templates/admin/user-list.html
}
@ApiOperation("ajax:分页查询用户信息")
@RequestMapping(value = "/admin/users", method = {RequestMethod.POST})
@ResponseBody
public Page<User> fetchPage(@RequestBody PageRequest pageRequest) {
return userService.fetchPage(pageRequest);
}
@ApiOperation("ajax:根据id查询用户")
@GetMapping("/admin/user/{id}")
@ResponseBody
public User getUserById(@PathVariable Long id) {
return userService.getUserById(id);
}
@ApiOperation("跳转到用户编辑界面")
@GetMapping("/admin/user/edit")
public String toEditUserById(@RequestParam(defaultValue = "-1") long id, Model model) {
@Resource
private StudentService studentService;
@GetMapping(value = {"/admin/room-request-list"})
public String toAdminRoomRequestList(Model model) {
model.addAttribute("statuses", roomRequestService.listAllStatuses());
return "admin/room-request-list"; // Thymeleaf模板的名字,表示 templates/admin/room-request-list.html
}
@ApiOperation("ajax:分页查询住宿申请信息")
@RequestMapping(value = "/admin/room-requests", method = {RequestMethod.POST})
@ResponseBody
public Page<RoomRequest> fetchPage(@RequestBody PageRequest pageRequest) {
return roomRequestService.fetchPage(pageRequest);
}
@ApiOperation("ajax:根据id查询住宿申请")
@GetMapping("/admin/room-request/{id}")
@ResponseBody
public RoomRequest getRoomRequestById(@PathVariable Long id) {
return roomRequestService.getRoomRequestById(id);
}
@ApiOperation("跳转到住宿申请编辑界面")
@GetMapping("/admin/room-request/edit")
public String toEditRoomRequestById(@RequestParam(defaultValue = "-1") long id, Model model) {
val roomRequest = roomRequestService.getRoomRequestById(id);
if (roomRequest != null) {
model.addAttribute("operation", "编辑住宿申请");
model.addAttribute("roomRequest", roomRequest);
model.addAttribute("statuses", roomRequestService.listAllStatuses());
model.addAttribute("students", studentService.listAllStudents());
model.addAttribute("rooms", roomService.listAllRooms());
return "admin/room-request-input";// Thymeleaf模板的名字,表示 templates/admin/room-request-input.html
} else {
return "redirect:/admin/room-request-list";
}
}
@ApiOperation("跳转到住宿申请添加界面")
@GetMapping("/admin/room-request/create")
public String toCreateRoomRequest(Model model) {
model.addAttribute("operation", "添加住宿申请");
model.addAttribute("roomRequest", new RoomRequest());
model.addAttribute("statuses", roomRequestService.listAllStatuses());
model.addAttribute("students", studentService.listAllStudents());
model.addAttribute("rooms", roomService.listAllRooms());
return "admin/room-request-input";
}
@GetMapping("/admin/room/{id}")
@ResponseBody
public Room getRoomById(@PathVariable Long id) {
return roomService.getRoomById(id);
}
@ApiOperation("跳转到寝室详情界面")
@GetMapping("/admin/room/detail")
public String toDisplayRoomDetailById(@RequestParam(defaultValue = "") Long id, Model model) {
val room = roomService.getRoomWithStudentsById(id);
if (room != null) {
model.addAttribute("room", room);
return "admin/room-detail";// Thymeleaf模板的名字,表示 templates/admin/room-detail.html
} else {
return "redirect:/admin/room-list";
}
}
@ApiOperation("跳转到寝室编辑界面")
@GetMapping("/admin/room/edit")
public String toEditRoomById(@RequestParam(defaultValue = "-1") long id, Model model) {
val room = roomService.getRoomById(id);
if (room != null) {
model.addAttribute("operation", "编辑寝室");
model.addAttribute("room", room);
model.addAttribute("categories", categoryService.listAllCategories());
return "admin/room-input";// Thymeleaf模板的名字,表示 templates/admin/room-input.html
} else {
return "redirect:/admin/room-list";
}
}
@ApiOperation("跳转到寝室添加界面")
@GetMapping("/admin/room/create")
public String toCreateRoom(Model model) {
model.addAttribute("operation", "添加寝室");
model.addAttribute("room", new Room());
model.addAttribute("categories", categoryService.listAllCategories());
return "admin/room-input";
}
@ApiOperation("保存住宿申请")
@GetMapping("/admin/room-request/save")
public String saveRoomRequest(RoomRequest roomRequest, RedirectAttributes attributes) {
val result = roomRequestService.saveRoomRequest(roomRequest);
if (result.isSuccess()) {
attributes.addFlashAttribute("info", "操作成功");
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",保存住宿申请失败");
}
return "redirect:/admin/room-request-list";
}
@ApiOperation("ajax:根据若干id删除住宿申请")
@RequestMapping(value = "/admin/room-request/delete", method = {RequestMethod.POST})
@ResponseBody
public LayuiResult<String> deleteRoomRequestByIds(String ids) {
val idList = parseLongList(ids);
if (roomRequestService.deleteRoomRequestByIds(idList).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
return new LayuiResult<>(FAILED, null, null);
}
}
@ApiOperation("新增住宿申请")
@GetMapping("/room-request/new")
@ResponseBody
public LayuiResult<String> addNewRoomRequest(String studentId, Long roomId) {
if (roomRequestService.newRoomRequest(studentId, roomId).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
return new LayuiResult<>(FAILED, null, null);
}
}
}
package com.demo.controller;
@Api("图片文件上传api")
@Controller
@Slf4j
public class FileUploadController {
private final static String NAME = "file";
@Resource
private PictureConfig pictureConfig;
/**
* 本地图片文件上传接口 "/upload"
*
* @param request 图片文件上传请求,要求参数名是 file, (例如:用原生form提交,input标签需要添加 name="file" )
* @return JSON格式的对象, code == 0 表示上传成功 , code == 1 表示上传失败
*/
@ApiOperation("ajax:本地图片文件上传")
@PostMapping("/upload")
@ResponseBody
public LayuiResult<Object> upload(HttpServletRequest request) {
MultipartHttpServletRequest mRequest;
if (request instanceof MultipartHttpServletRequest) {
mRequest = (MultipartHttpServletRequest) request;
} else {
return result(1, "failure:请求异常", null);
}
val multipartFile = mRequest.getFile(NAME);
if (null == multipartFile) {
return result(1, "failure:参数异常,请检查参数名是否为" + NAME, null);
}
val originalFilename = multipartFile.getOriginalFilename();
if (StringUtils.isEmpty(originalFilename)) {
return result(1, "failure:文件名为空", null);
}
val path = pictureConfig.getPath();
val dest = new File(path + originalFilename);
if (!dest.getParentFile().exists()) {
if (!dest.getParentFile().mkdirs()) {
return result(1, "failure:服务器存储路径创建失败", null);
}
@Controller
@Slf4j
public class FileUploadController {
private final static String NAME = "file";
@Resource
private PictureConfig pictureConfig;
/**
* 本地图片文件上传接口 "/upload"
*
* @param request 图片文件上传请求,要求参数名是 file, (例如:用原生form提交,input标签需要添加 name="file" )
* @return JSON格式的对象, code == 0 表示上传成功 , code == 1 表示上传失败
*/
@ApiOperation("ajax:本地图片文件上传")
@PostMapping("/upload")
@ResponseBody
public LayuiResult<Object> upload(HttpServletRequest request) {
MultipartHttpServletRequest mRequest;
if (request instanceof MultipartHttpServletRequest) {
mRequest = (MultipartHttpServletRequest) request;
} else {
return result(1, "failure:请求异常", null);
}
val multipartFile = mRequest.getFile(NAME);
if (null == multipartFile) {
return result(1, "failure:参数异常,请检查参数名是否为" + NAME, null);
}
val originalFilename = multipartFile.getOriginalFilename();
if (StringUtils.isEmpty(originalFilename)) {
return result(1, "failure:文件名为空", null);
}
val path = pictureConfig.getPath();
val dest = new File(path + originalFilename);
if (!dest.getParentFile().exists()) {
if (!dest.getParentFile().mkdirs()) {
return result(1, "failure:服务器存储路径创建失败", null);
}
}
try {
multipartFile.transferTo(dest);
} catch (Exception e) {
log.error("文件上传失败: error = " + e.getMessage());
return result(1, "failure:文件保存失败", null);
private PictureConfig pictureConfig;
/**
* 本地图片文件上传接口 "/upload"
*
* @param request 图片文件上传请求,要求参数名是 file, (例如:用原生form提交,input标签需要添加 name="file" )
* @return JSON格式的对象, code == 0 表示上传成功 , code == 1 表示上传失败
*/
@ApiOperation("ajax:本地图片文件上传")
@PostMapping("/upload")
@ResponseBody
public LayuiResult<Object> upload(HttpServletRequest request) {
MultipartHttpServletRequest mRequest;
if (request instanceof MultipartHttpServletRequest) {
mRequest = (MultipartHttpServletRequest) request;
} else {
return result(1, "failure:请求异常", null);
}
val multipartFile = mRequest.getFile(NAME);
if (null == multipartFile) {
return result(1, "failure:参数异常,请检查参数名是否为" + NAME, null);
}
val originalFilename = multipartFile.getOriginalFilename();
if (StringUtils.isEmpty(originalFilename)) {
return result(1, "failure:文件名为空", null);
}
val path = pictureConfig.getPath();
val dest = new File(path + originalFilename);
if (!dest.getParentFile().exists()) {
if (!dest.getParentFile().mkdirs()) {
return result(1, "failure:服务器存储路径创建失败", null);
}
}
try {
multipartFile.transferTo(dest);
} catch (Exception e) {
log.error("文件上传失败: error = " + e.getMessage());
return result(1, "failure:文件保存失败", null);
}
return result(0, "success", "/sdms-images/" + originalFilename);
}
private LayuiResult<Object> result(Integer code, String msg, String pictureURL) {
val m = new HashMap<String, Object>();
m.put("pictureURL", pictureURL);
return new LayuiResult<>(code, msg, null, Collections.singletonList(m));
}
}
package com.demo.common.exception;