• 项目笔记:原始方法实现一对多数据库关联关系


    一、一对多关系建立:一条策略对应多条规则:

      策略XML和实体:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="com.vrv.paw.domain">
        <class name="GenuineManagement" table="vrv_paw_genuineManagement">
            <cache usage="transactional" />
            <id name="id" column="id" type="integer">
                <generator class="native" />
            </id>
            <property name="policyName" type="string" length="32" />
            <property name="method" type="integer" />
            <property name="flag" type="integer" />
            <property name="isPublish" type="boolean"/>
            <property name="pubAreaRange" type="string" length="255" />
            <property name="pubAreaTxt" type="text" />
            <property name="publishArea" type="text" />
            <property name="publishGroup1" type="text" />
            <property name="publishGroup2" type="text" />
            <property name="publishDevice" type="text" />
            <property name="publishTime" type="timestamp" />
        </class>
    </hibernate-mapping>
    public class GenuineManagement implements Serializable {
    
        private static final long serialVersionUID = -5449557095173090598L;
        //序号
        private Integer id;
        //策略名称
        private String policyName;
        //管控措施:0/1/2/3
        private Integer method;
        //是否开启管控:检查0、管控1
        private Integer flag;
        //是否发布
        private Boolean isPublish;
        /** 发布区域机构代码 */
        private String pubAreaRange;
        /** 发布区域名称 */
        private String pubAreaTxt;
        /** 发布区域 **/
        private String publishArea;
        /** 发布组 **/
        private String publishGroup1;
        /** 发布组 **/
        private String publishGroup2;
        /** 发布设备**/
        private String publishDevice;
        /** 发布时间**/
        private Timestamp publishTime;
        //规则库集合
        private List<ControlMeasures> controlMeasuresList;
        
        public List<ControlMeasures> getControlMeasuresList() {
            return controlMeasuresList;
        }
        public void setControlMeasuresList(List<ControlMeasures> controlMeasuresList) {
            this.controlMeasuresList = controlMeasuresList;
        }
            //下面其他get和set方法及构造方法
    }

      注意:除了与XML上一一对应外,还加了List<ControlMeasures>用于对应规则库。

      规则库XML和实体:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="com.vrv.paw.domain">
        <class name="ControlMeasures" table="vrv_paw_controlMeasures">
            <cache usage="transactional" />
            <id name="id" column="id" type="integer">
                <generator class="native" />
            </id>
            <property name="unGenRightRule" type="integer" />
            <property name="genRightRule" type="integer" />
            <property name="softName" type="string" length="255" />
            <property name="softId" type="integer" />
            <property name="gmId" type="integer" />
        </class>
    </hibernate-mapping>
    public class ControlMeasures implements Serializable {
    
        private static final long serialVersionUID = -5449557095173090598L;
        //序号
        private Integer id;
        //非正版规则,取值0/1/3
        private Integer unGenRightRule;
        //正版规则,取值0/1/3
        private Integer genRightRule;
        //软件名
        private String softName;
        //软件id
        private Integer softId;
        //策略ID
        private Integer gmId;
        
        public Integer getGmId() {
            return gmId;
        }
        public void setGmId(Integer gmId) {
            this.gmId = gmId;
        }
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public Integer getUnGenRightRule() {
            return unGenRightRule;
        }
        public void setUnGenRightRule(Integer unGenRightRule) {
            this.unGenRightRule = unGenRightRule;
        }
        public Integer getGenRightRule() {
            return genRightRule;
        }
        public void setGenRightRule(Integer genRightRule) {
            this.genRightRule = genRightRule;
        }
        public String getSoftName() {
            return softName;
        }
        public void setSoftName(String softName) {
            this.softName = softName;
        }
        public Integer getSoftId() {
            return softId;
        }
        public void setSoftId(Integer softId) {
            this.softId = softId;
        }
        
        public ControlMeasures(){
            super();
        }
        public ControlMeasures(Integer id, Integer unGenRightRule, Integer genRightRule, String softName, Integer softId,Integer gmId) {
            this.id = id;
            this.unGenRightRule = unGenRightRule;
            this.genRightRule = genRightRule;
            this.softName = softName;
            this.softId = softId;
            this.gmId = gmId;
        }
        
    }

      注意:规则库加了一个gmId用于对应那条策略。

    二、然后再新建策略的时候,都要先保存策略,拿到策略id,再遍历该条策略下的规则库,设置策略id给规则库gmId,再保存规则库

    //新建策略
    public void createStrategy() {
        this.msg = RESULT_SUCCESS;
        try {
            // 在添加之前查询一下该策略名称是否唯一
            if (null != genuineManagementService.queryByName(genuineManagement.getPolicyName())) {
                this.msg = "RepeatName";
            } else {
                genuineManagement.setIsPublish(false);
                genuineManagementService.save(genuineManagement);//先保存策略,拿到策略id
                for(ControlMeasures controlMeasures : genuineManagement.getControlMeasuresList()){
                    controlMeasures.setGmId(genuineManagement.getId());//设置策略id给规则库的gmId
                    controlMeasuresService.save(controlMeasures);
                }
            }
        } catch (Exception e) {
            this.msg = RESULT_ERROR;
            e.printStackTrace();
        } finally {
            print(JsonUtil.build(new String[] { "msg","id","policyName"}, this.msg,genuineManagement.getId(),genuineManagement.getPolicyName()));
        }
    }

      修改策略的时候,先更新策略,再通过策略id去规则库表删除该策略id对应的gmId的数据,然后再遍历该策略下规则库,再保存各条规则库

    //修改策略
    public void editStrategy() {
        this.msg = RESULT_SUCCESS;
        try {
            genuineManagement.setIsPublish(false);
            genuineManagementService.update(genuineManagement);//先更新策略
            controlMeasuresService.deleteGmId(genuineManagement.getId());//再通过策略id去规则库表删除该策略id对应的gmId的数据,然后再遍历该策略下规则库,再保存各条规则库
            for(ControlMeasures controlMeasures : genuineManagement.getControlMeasuresList()){
                controlMeasures.setGmId(genuineManagement.getId());
                controlMeasuresService.save(controlMeasures);
            }
        } catch (Exception e) {
            this.msg = RESULT_ERROR;
            e.printStackTrace();
        } finally {
            print(JsonUtil.build("msg", this.msg));
        }
    }

      删除策略时,先删除规则库,再删除策略

    //删除策略
    public void deleteStrategy() {
        this.msg = RESULT_SUCCESS;
        boolean flag = true;
        try {
            if(selectedIds != null && !"".equals(selectedIds)){
                String[] ids = selectedIds.split(",");
                for(String id :ids){
                    GenuineManagement gm = genuineManagementService.queryById(Integer.parseInt(id));
                    if(gm != null && gm.getIsPublish()){
                        flag = false;
                        this.msg = RESULT_FAIL;
                        break;
                    }
                }
                if(flag){
                    Integer[] delIds = new Integer[ids.length];
                    for(int i=0;i<ids.length;i++){
                        delIds[i] = Integer.parseInt(String.valueOf(ids[i]));
                        controlMeasuresService.deleteGmId(delIds[i]);//先删除规则库
                    }
                    genuineManagementService.deleteByIds(delIds);//再删除策略
                }
            }
        } catch (Exception e) {
            this.msg = RESULT_ERROR;
            e.printStackTrace();
        } finally {
            print(JsonUtil.build("msg", this.msg));
        }
    }

      此外需要注意的是,策略实体里面有list,所以在用ajax传值的时候,那个list集合获取不到值,后来发现需要在前台传值的时候做下小操作,即模拟表单数据传值

    stategy = {
        'genuineManagement.policyName' : name,
        'genuineManagement.flag' : isControl,    
        'genuineManagement.method' : controlMeasures,
        'genuineManagement.controlMeasuresList[0].id' :1,
        'genuineManagement.controlMeasuresList[0].name' :'asd',
        'genuineManagement.controlMeasuresList[1].id' :1,
        'genuineManagement.controlMeasuresList[1].name' :'asd'
    };
    //获取配置的策略项的值
    function getStrategy() {
        var stategy = {};
        var name = $('#strategyName').val();
        var isControl = $('input[name="isControl"]:checked').val();
        var controlMeasures = $("input[name='controlMeasures']:checked").val();
        var controlMeasuresList = $("#ruleManagementTable").datagrid("getRows");
        var formObj = new Object();
        formObj['genuineManagement.policyName']=name;
        formObj['genuineManagement.flag']=isControl;
        formObj['genuineManagement.method']=controlMeasures;
        for(var i in controlMeasuresList){
            formObj['genuineManagement.controlMeasuresList['+i+'].softName'] = controlMeasuresList[i].softName;
            formObj['genuineManagement.controlMeasuresList['+i+'].genRightRule'] = controlMeasuresList[i].genRightRule;
            formObj['genuineManagement.controlMeasuresList['+i+'].unGenRightRule'] = controlMeasuresList[i].unGenRightRule;
            formObj['genuineManagement.controlMeasuresList['+i+'].softId'] = controlMeasuresList[i].softId;
        }
        return formObj;
    }

      最主要是我们要循环模拟list里面的这个数据。

    //修改策略:侧边滑入中的修改按钮事件
    $('#editStrategyBtn').click(function() {
        var strategyID = $('#strategyID').val();
        var dataObj = getStrategy();
        if (dataObj['genuineManagement.policyName'].replace(/(^s)|(s$)/g, '').length == 0) {
            showMsg("","策略名不能为空",false);
            return false;
        }
        dataObj['genuineManagement.id'] = strategyID;//修改的时候记得要加上策略id
        $.ajax({
            type : "POST",
            url : "${basePath}/genuineManagementAction_editStrategy.do",
            data : dataObj,
            dataType : "json",
            success : function(data) {
                if (data.msg === "success") {
                    $('#boxclose').click();
                    $('#inside_tableElement').datagrid('load');
                    showMsg("系统提示", "策略修改成功", false);
                } else if (data.msg === "RepeatName") {
                    showMsg("","提示:策略名称重复",false);
                } else {
                    showMsg("系统提示", "策略修改失败", false);
                }
            }
        });
    });

      这样模拟数据,后台就可以获取到值了。

  • 相关阅读:
    SpringBoot插件——EasyCode的使用(以MySQL为例)
    Springboot整合mybaties
    linux破解navicat for mysql
    springboot整合thymeleaf——引用静态资源
    整合thymeleaf
    Error: errCode: -404011 cloud function execution error | errMsg: clou……错误
    JqueryMobile与php跳转问题
    Hbuilder环境下配置php
    Bean的三种实例化方式
    利用TFTP命令上传下载H3C交换机配置文件
  • 原文地址:https://www.cnblogs.com/goloving/p/7609595.html
Copyright © 2020-2023  润新知