SaaS-Export项目 部门修改回显

后台测试回显代码

在这里插入图片描述

TestDeptService

  @Test
    public void test02(){
        //deptId=100101
        String deptId="100101";
        Dept dept = iDeptService.findById(deptId);

        l.info("test04  dept="+dept);
    }

IDeptService

 //查找指定id的部门
    Dept findById(String deptId);

DeptServiceImpl

  @Override
    public Dept findById(String deptId) {
        return iDeptDao.findById(deptId);
    }

DeptController

  // ${path}/system/dept/toUpdate.do?deptId=${dept.deptId}
   @RequestMapping(path="/toUpdate",method ={ RequestMethod.GET, RequestMethod.POST})
   public String toUpdate(Model model, String deptId){

        l.info("toUpdate deptId="+deptId);

        return "system/dept/dept-update";
   }

select下拉菜单的回显

    <div class="col-md-2 title">上级部门</div>
    <div class="col-md-10 data line-height36">
        <select class="form-control" name="parentId">
        	<%-- 成为顶级部门那么就没有上级部门,也就是上级部门为null,这里传空值 --%>
            <option value="">成为顶级部门</option>
            <c:forEach items="${list}" var="item">
                <%-- dept表示正在编辑的部门数据,不能选自己作为上级部门--%>
                <c:if test="${dept.deptId != item.deptId}">
                    <%-- 选中当前的上级部门,给下拉框添加一个选择效果 --%>
                    <option ${dept.parent.deptId == item.deptId ?'selected':''}  value="${item.deptId}">${item.deptName}</option>
                </c:if>
 
            </c:forEach>
        </select>
    </div>

select下拉菜单的回显,value才是传递给后台的值

	<select name="parentId">
 	    <option value="1">显示的值</option>
    </select>

radio单选框的回显

    <div class="col-md-2 title">状态</div>
        <div class="col-md-10 data">
    	<div class="form-group form-inline">
    	    <div class="radio"><label><input type="radio" ${dept.state==0?'checked':''} name="state" value="0">停用</label></div>
    	    <div class="radio"><label><input type="radio" ${dept.state==1?'checked':''} name="state" value="1">启用</label></div>
    	</div>
    </div>

部门修改后台

TestDeptService

@Test
    public void test03(){

        String deptId="4028827c4fb633bd014fb64467470000";
        Dept dept = iDeptService.findById(deptId);
        //1 模拟页面的修改
        dept.setDeptName("ym");

        dept.setCompanyName("吉首大学");
        dept.setCompanyId("1");

        Dept parent = new Dept();//下拉菜单
        parent.setDeptId("100");
        dept.setParent(parent);

        dept.setState(0);//停用

        //2 保存到数据库
        iDeptService.updateDept(dept);

        l.info("test05  dept="+dept);
    }

IDeptService

//保存编辑页面的部门数据
    void updateDept(Dept dept);

DeptServiceImpl

@Override
    public void updateDept(Dept dept) {
        //与保存的区别 》1:前者insert 后者是update  》2:前者需要产生id,后者有id
        iDeptDao.update(dept);
    }

IDeptDao

 void update(Dept dept);

DeptDaoImpl.xml

在实体类deptBean中没有定义parent_id这个变量,只有parent这个Dept对象
可以使用parent.deptId来修改parent_id的值
在所有的更新中,主键 不能进行修改

 <update id="update" parameterType="dept">
     update  pe_dept set

            dept_name     =  #{deptName    }  ,
            parent_id     =  #{parent.deptId}  ,
            state         =  #{state       }  ,
            company_id    =  #{companyId   }  ,
            company_name  =  #{companyName }

     where dept_id= #{deptId}
    </update>

DeptController

 //action="${path}/system/dept/update.do"
    @RequestMapping(path="/update",method ={ RequestMethod.GET, RequestMethod.POST})
    public String update(Dept dept,String parentId){

        l.info("update dept="+dept);
        l.info("update parentId="+parentId);

        //当前写死companyId与companyName以后再修改
        dept.setCompanyName("吉首大学");
        dept.setCompanyId("1");

        Dept parent = new Dept();//下拉菜单
        parent.setDeptId(parentId);
        dept.setParent(parent);

        l.info("update dept="+dept);
        //2 保存到数据库
        iDeptService.updateDept(dept);

        return "redirect:/system/dept/toList.do";//修改完成之后跳到列表页面
    }

出现的问题

在这里插入图片描述

上级部门

使用c:if来进行判断,判断回显的id,与下列列表list中的id是否相同,如果不相同就显示,相同则不显示

 <%-- dept表示正在编辑的部门数据,不能选自己作为上级部门--%>
    <c:if test="${dept.deptId != item.deptId}">
<option ${dept.parent.deptId == item.deptId ?'selected':''}  value="${item.deptId}">${item.deptName}</option>
</c:if>

选择自己作为顶级部门报错

当选择自己为顶级部门时,提交的parent_id为空字符串 parent_id = NULL
无法写入数据库
可以使用mybatis动态标签来解决

mybatis动态标签-if标签

if标签如何使用?
》》 1 if标签的test属性必填,一般只用true或false作为结果。
》》 2 判断条件property != null或 property == null,适用于任何类型的字段,用于判断属性值是否为空。
》》 4 当有多个判断条件时,使用and或or

<update id="update" parameterType="dept">
     update  pe_dept set

            dept_name     =  #{deptName    }  ,
            <if test="parent.deptId  == null or parent.deptId == '' ">
                parent_id   = NULL  ,
            </if>
            <if test="parent.deptId !=null and parent.deptId != '' ">
                parent_id     = ${parent.deptId}  ,
            </if>
            state         =  #{state       }  ,
            company_id    =  #{companyId   }  ,
            company_name  =  #{companyName }

     where dept_id= #{deptId}
    </update>

mybatis动态标签-choose标签

可以使用choose标签,类似switch

  <update id="update" parameterType="dept">
        update  pe_dept set

        dept_name     =  #{deptName    }  ,
        <choose>
        //相当于if
            <when test="parent.deptId  == null or parent.deptId == ''">
                  parent_id   = NULL  ,
            </when>
            相当于else
            <otherwise>
                parent_id    = #{parent.deptId}  ,
            </otherwise>
        </choose>

        state         =  #{state       }  ,
        company_id    =  #{companyId   }  ,
        company_name  =  #{companyName }

        where dept_id= #{deptId}
    </update>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值