Spring Boot学生信息管理系统项目实战-2.字典管理和模板管理

1.获取源码

源码是捐赠方式获取,详细请QQ联系我 :)

 

2.实现效果

3.项目源码

只挑重点讲,详细请看源码。

 

3.1 字典管理

字典管理这里分为字典的编码和名称和字典数据的增删改查。

  • 前端页面:
<div class="layui-row" id="editDict" style="display:none;">
    <div class="layui-col-md10">
        <form class="layui-form layui-from-pane" id="saveDict" style="margin-top:20px">
            <input type="text" name="id" id="id" hidden="hidden">

            <div class="layui-form-item">
                <label class="layui-form-label">字典编码</label>
                <div class="layui-input-inline">
                    <input type="text" name="code" id="code" required lay-verify="required" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">字典名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" id="name" required lay-verify="required" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item" id="check">
                <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="saveDict">保存</button>
                    <!--<button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
                </div>
            </div>
        </form>
    </div>
</div>
  • 后端代码:

/**
     * 保存(新增/修改)
     *
     * @param entity
     * @return
     */
    @PostMapping("/save")
    public Result<String> modify(@RequestBody Dict entity) {
        String code = entity.getCode();
        int count = dictService.checkCode(entity);
        if(count>0){
            return ResultUtil.fail("编码重复!");
        }
        if(entity.getId()!=null){
            dictService.modify(entity);
        }else{
            dictService.add(entity);
        }

        return ResultUtil.ok();
    }

    @Transactional
    @PostMapping("/remove")
    public Result<String> remove(@RequestBody Long[] ids) {
        // 判断字典是否被用
        Dict dict=null;
        int count=0;
        for (int i = 0; i < ids.length; i++) {
            dict = dictService.get(ids[i]);
            count = dictService.checkUsed(ids[i]);
            if(count>0){
                return ResultUtil.fail("字典:"+dict.getCode()+"已被模板使用,无法删除!");
            }
        }

        for (int i = 0; i < ids.length; i++) {
            dictDataService.removeByDictId(ids[i]);
        }

        dictService.removes(ids);

        return ResultUtil.ok();
    }

    @GetMapping("/get")
    public Result<Dict> get(@RequestParam("id") Long id) {
        Dict entity = dictService.get(id);

        return ResultUtil.ok(entity);
    }

    @PostMapping("/list")
    public ResultBean<List<Dict>> getPageResult(
            @RequestParam(required = false) String code,
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {

        Map<String, Object> param = new HashMap<>();

        // 计算起始行号
        int offset = (page - 1) * limit;
        int rows = limit;

        param.put("code",code);
        param.put("name",name);
        param.put("offset", offset);
        param.put("rows", rows);

        // 统计记录数
        int totalRows = dictService.count(param);

        // 获取当前页结果集
        List<Dict> entities = dictService.getPageResult(param);

        ResultBean result = new ResultBean(0, "查询成功", totalRows, entities);

        return result;

    }

 

3.2 模板管理

模板管理主要是定义学生信息的导入模板。支持动态扩展,注意一点需要模板列和学生信息列名保持一致。

  • 前端代码
<!--编辑表单-->
<div class="layui-row" id="editTemplate" style="display:none;">
    <div class="layui-col-md10">
        <form class="layui-form layui-from-pane" id="saveTemplate" style="margin-top:20px">
            <input type="text" name="id" id="id" hidden="hidden">

            <div class="layui-form-item">
                <label class="layui-form-label">模板编码</label>
                <div class="layui-input-inline">
                    <input type="text" name="code" id="code" required lay-verify="required" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">模板名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" id="name" required lay-verify="required" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">内容类型</label>
                <div class="layui-input-inline">
                    <select id="contentType" name="contentType" lay-verify="required">
                        <option value="学生信息">学生信息</option>
                        <option value="班级信息">班级信息</option>
                        <option value="教师信息">教师信息</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否默认</label>
                <div class="layui-input-inline">
                    <input type="checkbox" id="isDefault" name="isDefault" lay-skin="switch" lay-filter="isDefaultCheck" lay-text="ON|OFF">
                </div>
            </div>

            <div class="layui-form-item" id="check">
                <div class="layui-input-block">
                    <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="saveTemplate">保存</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
  • 后端代码
/**
     * 保存(新增/修改)
     *
     * @param entity
     * @return
     */
    @PostMapping("/save")
    public Result<String> modify(@RequestBody Template entity) {
        String code = entity.getCode();
        int count = templateService.checkCode(entity);
        if(count>0){
            return ResultUtil.fail("编码重复!");
        }
        if(entity.getId()!=null){
            templateService.modify(entity);
        }else{
            templateService.add(entity);
        }

        return ResultUtil.ok();
    }

    @Transactional
    @PostMapping("/remove")
    public Result<String> remove(@RequestBody Long[] ids) {
        // 默认模板不能删除
        Template template = null;
        for (int i = 0; i < ids.length; i++) {
            template = templateService.get(ids[i]);
            if(template.getIsDefault()==1){
                return ResultUtil.fail("模板:"+template.getCode()+"是默认模板,无法删除!");
            }
            templateDataService.removeByTemplateId(ids[i]);
        }
        // 继续删除
        for (int i = 0; i < ids.length; i++) {
            templateDataService.removeByTemplateId(ids[i]);
        }

        templateService.removes(ids);

        return ResultUtil.ok();
    }

    @GetMapping("/get")
    public Result<Template> get(@RequestParam("id") Long id) {
        Template entity = templateService.get(id);

        return ResultUtil.ok(entity);
    }

    @PostMapping("/list")
    public ResultBean<List<Template>> getPageResult(
            @RequestParam(required = false) String code,
            @RequestParam(required = false) String name,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {

        Map<String, Object> param = new HashMap<>();

        // 计算起始行号
        int offset = (page - 1) * limit;
        int rows = limit;

        param.put("code",code);
        param.put("name",name);
        param.put("offset", offset);
        param.put("rows", rows);

        // 统计记录数
        int totalRows = templateService.count(param);

        // 获取当前页结果集
        List<Template> entities = templateService.getPageResult(param);

        ResultBean result = new ResultBean(0, "查询成功", totalRows, entities);

        return result;

    }

 

 

 

 

源码是捐赠方式获取,详细请QQ联系我 :)

 

 

 

 

posted @ 2020-12-16 18:15  一锤子技术员  阅读(4)  评论(0编辑  收藏  举报  来源