页面回显
前端点击编辑按钮,后端查询id对应的回显数据,返回页面
Controller层
@RequestMapping(path="/updateUI",method ={ RequestMethod.GET, RequestMethod.POST})
public String updateUI(Model model, String deptId){
String companyId =getLoginCompanyId();
l.info("toUpdate deptId="+deptId);
//查询部门
Dept dept = iDeptService.findById(deptId);
l.info("toUpdate dept="+dept);
List<Dept> list = iDeptService.findAllParent(companyId);
model.addAttribute("dept",dept);
model.addAttribute("list",list);
return "system/dept/dept-update";
}
Service层
@Override
public List<Dept> findAllParent(String loginCompanyId) {
List<Dept> all = iDeptDao.findAll(loginCompanyId);
return all;
}
Dao层
<select id="findAll" resultMap="findOneMap" parameterType="string">
select * from pe_dept where company_id =#{companyId}
</select>
下拉菜单回显
<select class="form-control" name="parentId">
<option value="">请选择</option>
<c:forEach items="${list}" var="item">
<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 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>
更新操作
Controller层
@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(getLoginCompanyName());
dept.setCompanyId(getLoginCompanyId());
Dept parent = new Dept();//下拉菜单
parent.setDeptId(parentId);
dept.setParent(parent);
l.info("update dept="+dept);
//2 保存到数据库
iDeptService.updateDept(dept);
return "redirect:/dept/list";//修改完成之后跳到列表页面
}
Service层
@Override
public void updateDept(Dept dept) {
//与保存的区别 》1:前者insert 后者是update 》2:前者需要产生id,后者有id
iDeptDao.update(dept);
}
Dao层
<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>