基于javaweb+mysql的springboot+jpa学生宿舍学生住宿申请管理系统(管理员、学生)(java+springboot+jpa+thymeleaf+html+mysql)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
后台管理员:管理用户、学生、班级、宿舍、处理住宿申请、分配宿舍
前台学生:查询宿舍、申请宿舍
后台管理员:
前台学生:
技术框架
JSP JavaScript SpringBoot SpringDataJPA thymeleaf MySQL HTML
基于javaweb+mysql的SpringBoot+JPA学生宿舍学生住宿申请管理系统(管理员、学生)(java+springboot+jpa+thymeleaf+html+mysql)
if (result.isSuccess()) {
attributes.addFlashAttribute("info", "操作成功");
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",更新信息失败");
}
return "redirect:/login";
}
}
@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})
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);
}
@ApiOperation("ajax:根据id查询用户")
@GetMapping("/admin/user/{id}")
@ResponseBody
public User getUserById(@PathVariable Long id) {
return userService.getUserById(id);
}
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",保存学生失败");
}
return "redirect:/admin/student-list";
}
@ApiOperation("ajax:根据若干id删除学生")
@RequestMapping(value = "/admin/student/delete", method = {RequestMethod.POST})
@ResponseBody
public LayuiResult<String> deleteStudentByIds(String ids) {
val idList = parseStringList(ids);
if (studentService.deleteStudentByIds(idList).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
return new LayuiResult<>(FAILED, null, null);
}
}
}
@Api("寝室相关api")
@Controller
public class RoomController {
@RequestMapping(value = "/admin/student/delete", method = {RequestMethod.POST})
@ResponseBody
public LayuiResult<String> deleteStudentByIds(String ids) {
val idList = parseStringList(ids);
if (studentService.deleteStudentByIds(idList).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
return new LayuiResult<>(FAILED, null, null);
}
}
}
@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) {
@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);
return "admin/teaching-class-input";// Thymeleaf模板的名字,表示 templates/admin/teaching-class-input.html
} else {
return "redirect:/admin/teaching-class-list";
}
}
@ApiOperation("跳转到班级添加界面")
@GetMapping("/admin/teaching-class/create")
public String toCreateTeachingClass(Model model) {
model.addAttribute("operation", "添加班级");
model.addAttribute("teachingClass", new TeachingClass());
return "admin/teaching-class-input";
}
@ApiOperation("保存班级")
@GetMapping("/admin/teaching-class/save")
public String saveTeachingClass(TeachingClass teachingClass, RedirectAttributes attributes) {
val result = teachingClassService.saveTeachingClass(teachingClass);
if (result.isSuccess()) {
attributes.addFlashAttribute("info", "操作成功");
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",保存班级失败");
}
return "redirect:/admin/teaching-class-list";
}
@ApiOperation("ajax:根据若干id删除班级")
@RequestMapping(value = "/admin/teaching-class/delete", method = {RequestMethod.POST})
@ResponseBody
public LayuiResult<String> deleteTeachingClassByIds(String ids) {
val idList = parseLongList(ids);
if (teachingClassService.deleteTeachingClassByIds(idList).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
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";
}
}
/**
* 全局异常处理
*/
@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());
}
}
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);
return "admin/teaching-class-input";// Thymeleaf模板的名字,表示 templates/admin/teaching-class-input.html
} else {
return "redirect:/admin/teaching-class-list";
}
}
@ApiOperation("跳转到班级添加界面")
@GetMapping("/admin/teaching-class/create")
public String toCreateTeachingClass(Model model) {
model.addAttribute("operation", "添加班级");
model.addAttribute("teachingClass", new TeachingClass());
return "admin/teaching-class-input";
}
@ApiOperation("保存班级")
@GetMapping("/admin/teaching-class/save")
public String saveTeachingClass(TeachingClass teachingClass, RedirectAttributes attributes) {
val result = teachingClassService.saveTeachingClass(teachingClass);
if (result.isSuccess()) {
attributes.addFlashAttribute("info", "操作成功");
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",保存班级失败");
}
return "redirect:/admin/teaching-class-list";
}
@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);
return "admin/teaching-class-input";// Thymeleaf模板的名字,表示 templates/admin/teaching-class-input.html
} else {
return "redirect:/admin/teaching-class-list";
}
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);
}
}
@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());
@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);
return "admin/teaching-class-input";// Thymeleaf模板的名字,表示 templates/admin/teaching-class-input.html
} else {
return "redirect:/admin/teaching-class-list";
}
}
@ApiOperation("跳转到班级添加界面")
@ResponseBody
public LayuiResult<String> deleteStudentByIds(String ids) {
val idList = parseStringList(ids);
if (studentService.deleteStudentByIds(idList).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
return new LayuiResult<>(FAILED, null, null);
}
}
}
@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("跳转到学生编辑界面")
@GetMapping("/admin/student/edit")
public String toEditStudentById(@RequestParam(defaultValue = "") String id, Model model) {
val student = studentService.getStudentById(id);
if (student != null) {
model.addAttribute("operation", "编辑学生");
model.addAttribute("student", student);
model.addAttribute("teachingClasses", teachingClassService.listAllTeachingClasses());
return "admin/student-input";// Thymeleaf模板的名字,表示 templates/admin/student-input.html
} else {
return "redirect:/admin/student-list";
}
}
@ApiOperation("跳转到学生添加界面")
@GetMapping("/admin/student/create")
public String toCreateStudent(Model model) {
model.addAttribute("operation", "添加学生");
model.addAttribute("student", new Student());
model.addAttribute("teachingClasses", teachingClassService.listAllTeachingClasses());
return "admin/student-input";
}
@ApiOperation("保存学生")
@GetMapping("/admin/student/save")
public String saveStudent(Student student, RedirectAttributes attributes) {
val result = studentService.saveStudent(student);
if (result.isSuccess()) {
attributes.addFlashAttribute("info", "操作成功");
} else {
attributes.addFlashAttribute("error", result.getMsg() + ",保存学生失败");
}
return "redirect:/admin/student-list";
}
@ApiOperation("ajax:根据若干id删除学生")
@RequestMapping(value = "/admin/student/delete", method = {RequestMethod.POST})
@ResponseBody
public LayuiResult<String> deleteStudentByIds(String ids) {
val idList = parseStringList(ids);
if (studentService.deleteStudentByIds(idList).isSuccess()) {
return new LayuiResult<>(SUCCESS, null, null);
} else {
return new LayuiResult<>(FAILED, null, null);
}
}
}
@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")
*/
@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);
}
}
/**
* 全局异常处理
*/
@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);
}
}
@Api("用户相关api")
@Controller
public class UserController {
@Resource
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";
}