saas-export项目-部门管理-编辑功能-页面回显-更新操作

打开编辑页面

DeptController

    //打开修改页面
    @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";
    }

数据回显

根据deptId查找所要修改的数据

TestDeptService

 @Test
    public void test04(){
        String deptId = "100101";
        Dept dept = iDeptService.findById(deptId);
        l.info("test04 dept="+dept);
    }

IDeptService

Dept findById(String deptId);

DeptServiceImpl

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

IDeptDao

 Dept findById(String deptId);

IDeptDao.xml

<select id="findById" parameterType="string" resultMap="findOneMap">
        select  * from  pe_dept where dept_id = #{dept_id}
    </select>
οnclick='location.href="${path}/system/dept/toUpdate.do?deptId=${dept.deptId}"'>编辑</button>

还需要查询所有部门

原因:因为编辑页面中有一个下拉菜单操作,需要查询到所有的部门名称,进行添加。
I

前台操作,进行数据显示

DeptController

 //打开修改页面
    @RequestMapping(path = "/toUpdate",method = {RequestMethod.GET,RequestMethod.POST})
    public String toUpdate(Model model,String deptId){
        String companyId = "1";
        l.info("toUpdate companyId="+companyId);
        //查询部门
        Dept dept = iDeptService.findById(deptId);
        l.info("toUpdate dept="+dept);
        List<Dept> list = iDeptService.findAll(companyId);
        model.addAttribute("dept",dept);
        model.addAttribute("list",list);
        l.info("toUpdate deptId="+deptId);
        return "system/dept/dept-update";
    }

下拉菜单回显

<select class="form-control" name="parentId">
                                        <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>

select标签的回显

<select name="parentId">
        <option value="1">部门1</option>
        <option value="2">部门2</option>
        <option selected value="3">部门3</option>
        <option value="4">部门4</option>
    </select>

单选的回显操作

<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>

单选框的回显

查询数据是0
<input type="radio" name="sex" value="1" ><input type="radio" name="sex" value="0" checked >

部门更新数据功能

TestDeptService

 @Test
    public void test05(){

        String deptId="8a7e82be61400c000161400c05810000";
        Dept dept = iDeptService.findById(deptId);
        //1 模拟页面的修改
        dept.setDeptName("营销");

        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) {
        iDeptDao.update(dept);
    }

IDeptDao

void update(Dept dept);

IDeptDao.xml

<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>
  • 在更新操作中,id的值是不能进行修改的
  • 并且在当前的Dept实体类中,没有parentId,只有Dept parent

部门修改功能前台代码

dept-update.jsp

 <form id="editForm" action="${path}/system/dept/update.do" method="post">

DeptController

 @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";//修改完成之后跳到列表页面
    }

部门修改测试

1.自己可选自己作为上级部门

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

》当选择顶级部门时,提交的parent_id为空字符串
修改

 <select class="form-control" name="parentId">
                                        <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>
  • 通过if标签来判断

部门修改-设置顶级部门

  • 一个部门可以没有上级部门,此时该部门为顶级部门
  • 表示顶级部门
    parent_id = NULL

mybatis动态标签-if标签

  • 定义
    Mybatis提供的动态sql的标签支持
  • 作用
    可以根据值编写条件,如果条件成立,拼接sql,否则不拼接
  • 使用
    》if标签的test属性必填,一般只用true或false作为结果
    》判断条件property != null或property == null,适用于任何类型的字段,用于判断属性值是否为空
    》当有多个判断条件时,使用and或or

用解决if标签解决后

<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>

解决方法2

mybatis动态标签-choose标签

  • 若是没有else标签,可以使用choose标签,类似switch
 <update id="update" parameterType="dept">
        update  pe_dept set

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

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

        where dept_id= #{deptId}
    </update>
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值