仓库最新地址:https://gitee.com/guo1635/hoj-v1

在二次开发过程中,发现有很多功能需要重新配置数据表,特别后续的一些开关、简单配置项,于是想直接开发一个配置表,把后续的配置项都加入到这个表中。

一、新增数据表

新增了一个configuration表,主要用来记录配置项。

DROP TABLE IF EXISTS `configuration`;
CREATE TABLE `configuration`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '配置项名称',
  `code` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '配置项编码',
  `parent_code` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '父级配置项编码',
  `value` longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '配置项的值',
  `remark` longtext CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '说明',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `code`(`code`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

一个表分成两层结构,parent_code是父项,如果某一项是根目录,那么它的父项应该是-1

我这里插入了一些数据,把原来首页签到的功能改成配置项了。

INSERT INTO `configuration` VALUES (1, '签到信息', 'signInfo', '-1', NULL, '首页签到信息');
INSERT INTO `configuration` VALUES (2, '签到列表', 'signList', 'signInfo', '吉,凶,大吉,大凶,中吉,无畏,自然', '求到的签');
INSERT INTO `configuration` VALUES (3, '签到事项', 'goodBad', 'signInfo', '打扫房间,学习,刷题,看电视,写小说,背课文', '中间用英文的逗号隔开即可');
INSERT INTO `configuration` VALUES (4, '显示开关', 'showSwitch', '-1', NULL, '决定首页上是否展示相应的模块');
二、代码修改

新增了一个controller

package top.hcode.hoj.controller.admin;


import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.pojo.vo.ConfigurationVO;
import top.hcode.hoj.service.admin.system.ConfigurationService;

import java.util.List;

/**
 * 配置项信息
 *
 *
 */

@RestController
@RequestMapping("/api/admin/configuration")
public class ConfigurationController{

    @Autowired
    private ConfigurationService configurationService;


    /**
     * 获取所有的父级节点
     * */

    @GetMapping("/get-all-parents")
    @RequiresRoles(value = {"root", "admin",}, logical = Logical.OR)
    @RequiresAuthentication
    public CommonResult<List<ConfigurationVO>> getAllParents(){
        ConfigurationVO configurationVO = new ConfigurationVO();
        configurationVO.setParentCode("-1");//所有父级节点的父级节点都是-1'
        List<ConfigurationVO> configurationVOList = configurationService.quaryAll(configurationVO);
        return  CommonResult.successResponse(configurationVOList);
    }

    /**
     * 根据code查询配置项
     */
    @GetMapping("/get-configuration-code")
    @RequiresAuthentication
    public CommonResult<List<ConfigurationVO>> quaryByCode(@RequestParam("code") String code){
        ConfigurationVO configurationVO = new ConfigurationVO();
        configurationVO.setCode(code);
        List<ConfigurationVO> configurationVOList = configurationService.quaryAll(configurationVO);
        return  CommonResult.successResponse(configurationVOList);
    }


    /**
     * 根据父级code查询父级下面的所有配置项
     */
    @GetMapping("/get-configuration-parent-code")
    @RequiresAuthentication
    public CommonResult<List<ConfigurationVO>> quaryChildByCode(@RequestParam("code") String code){
        ConfigurationVO configurationVO = new ConfigurationVO();
        configurationVO.setParentCode(code);
        List<ConfigurationVO> configurationVOList = configurationService.quaryAll(configurationVO);
        return  CommonResult.successResponse(configurationVOList);
    }
    /**
     *批量插入操作
     */
    @PostMapping("/batchInsert")
    @RequiresRoles(value = {"root", "admin",}, logical = Logical.OR)
    @RequiresAuthentication
    public CommonResult batchInsert(@RequestBody List<ConfigurationVO> configList) {
        try {
            int count = configurationService.batchInsert(configList);
            return CommonResult.successResponse(count);
        } catch (Exception e) {
            return CommonResult.errorResponse("批量插入失败");
        }
    }

    @PutMapping("/updateByCode")
    @RequiresRoles(value = {"root", "admin",}, logical = Logical.OR)
    @RequiresAuthentication
    public CommonResult updateBycode(@RequestBody ConfigurationVO configurationVO) {
        try {
            if(configurationVO.getCode()!=null){
                return CommonResult.successResponse( configurationService.updateByCode(configurationVO));
            }else {
                return CommonResult.errorResponse("没有code");
            }
        }catch (Exception e) {
            return CommonResult.errorResponse("更新失败");
        }
    }

}

前端效果如下,在原来的系统配置最下面增加了一个显示,后续会逐步增加配置项。