基于javaweb+mysql的springboot宿舍管理系统(java+springboot+thymeleaf+html+layui+mysql)

基于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请求

使用说明

  1. 使用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


@Controller
public class AllocationController {

    @Resource
    private RoomAllocationService allocationService;

    @GetMapping(value = {"/admin/allocation-list"})
    public String toAdminAllocationList() {
        return "admin/allocation-list"; // Thymeleaf模板的名字,表示 templates/admin/allocation-list.html
    }

    @ApiOperation("ajax:分页查询学生住宿信息")
    @RequestMapping(value = "/admin/allocations", method = {RequestMethod.POST})
    @ResponseBody
    public Page<Student> fetchPage(@RequestBody PageRequest pageRequest) {
        return allocationService.fetchPage(pageRequest);
    }

    @ApiOperation("ajax:根据若干学号为学生完成解约")
    @RequestMapping(value = "/admin/allocation/release", method = {RequestMethod.POST})
    @ResponseBody
    public LayuiResult<String> releaseStudentByIds(String ids) {
        val idList = parseStringList(ids);
        if (allocationService.releaseStudentByIds(idList).isSuccess()) {
            return new LayuiResult<>(SUCCESS, null, null);
        } else {
            return new LayuiResult<>(FAILED, null, null);
        }
    }
}

        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);
        }
    }
}

    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);
        }
    }
}

/**
 * 将JSON返回值统一包装成LayuiResult对象
 * 注:对使用了@ResponseBody的Restful接口有效
 */
@RestControllerAdvice(basePackages = {"com.demo.controller"})
    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")

@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

@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("跳转到班级添加界面")
    @GetMapping("/admin/teaching-class/create")
        }
        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));
    }

}

/**
 * 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理自定义的异常 ApiException
    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";
    }
}

        // 如果接口返回的类型已经是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类型
            val objectMapper = new ObjectMapper();
            try {
                // 将数据包装在LayuiResult里后,再转换为json字符串响应给前端
                val result = new LayuiResult<>(LayuiResult.ResultCode.SUCCESS, null, Collections.singletonList((String) data));
                return objectMapper.writeValueAsString(result);
            } catch (JsonProcessingException e) {
                throw new ApiException();
            }
        } else if (data instanceof Page) {
            // Page类型
            return new LayuiResult<>((Page<?>) data);
        } else {
            // 其它的对象类型
            List<Object> objectList;
            if (data == null) {
                objectList = Collections.emptyList();
            } else {
                objectList = Collections.singletonList(data);
            }
            return new LayuiResult<>(LayuiResult.ResultCode.SUCCESS, null, objectList);
        }
    }
}


@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) {
            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);
    @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);
        }
    }
}

/**
 * 将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
            return "redirect:/admin/category-list";
        }
    }

    @ApiOperation("跳转到寝室类型添加界面")
    @GetMapping("/admin/category/create")
    public String toCreateCategory(Model model) {
        model.addAttribute("operation", "添加寝室类型");
        model.addAttribute("category", new Category());
        return "admin/category-input";
    }

    @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);
        }
    }
}


@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);
            }
        }
        val result = userService.updateUser(user);
        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})
    @ResponseBody

    @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";
    }
}

@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
     * 处理方法参数错误的异常 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);
    }
}

@Controller
public class AllocationController {

    @Resource
    private RoomAllocationService allocationService;

    @GetMapping(value = {"/admin/allocation-list"})
    public String toAdminAllocationList() {
        return "admin/allocation-list"; // Thymeleaf模板的名字,表示 templates/admin/allocation-list.html
    }

    @ApiOperation("ajax:分页查询学生住宿信息")
    @RequestMapping(value = "/admin/allocations", method = {RequestMethod.POST})
    @ResponseBody
    public Page<Student> fetchPage(@RequestBody PageRequest pageRequest) {
        return allocationService.fetchPage(pageRequest);
    }

    @ApiOperation("ajax:根据若干学号为学生完成解约")
        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);
        }
    }
}

请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值