分析一下删除的业务逻辑
》编写sql 查询当前部门是否给其他部门作上级
》编写删除 sql 删除指定部门编号的部门数据
各种情况分析
删除时 没有给其他部作上级部门的
delete from pe_dept where dept_id=‘33’
删除时 当前部门给其他部门作上级部门的
delete from pe_dept where dept_id=‘100101’
判断 记录数==0 是可删除的
select * from pe_dept where parent_id=‘100101’
select count(*) from pe_dept where parent_id=‘33’
具体代码实现
company-list.jsp
function deleteById() {
var id = getCheckId()
if(id) {
if(confirm("你确认要删除此条记录吗?")) {
location.href="${path}/system/dept/delete.do?depId="+id;
}
}else{
alert("请勾选待处理的记录,且每次只能勾选一个")
}
}
<button type="button" class="btn btn-default" title="删除" onclick='deleteById()'><i class="fa fa-trash-o"></i> 删除</button>
DeptController
// location.href="${path}/system/dept/delete.do?depId="+deptId;
@RequestMapping(path="/delete",method ={ RequestMethod.GET})
public String delete(String depId){
iDeptService.deleteDeptById(depId);
return "redirect:/system/dept/toList.do";//修改完成之后跳到列表页面
}
IDeptService
boolean deleteDeptById(String deptId);
DeptServiceImpl
@Override
public boolean deleteDeptById(String deptId) {
//先查询count
int count = iDeptDao.findParentCount(deptId);
//再根据count判断
if(count==0){//没有给其他部门作上级
iDeptDao.deleteById(deptId);
return true;
}else{
return false;
}
}
IDeptDao
int findParentCount(String deptId);
void deleteById(String deptId);
IDeptDao.xml
<!-- 统计当前部门作为其他部门的上级的数量-->
<select id="findParentCount" parameterType="string" resultType="int">
select count(*) from pe_dept where parent_id=#{deptId}
</select>
<!-- 删除指定deptId的部门-->
<delete id="deleteById" parameterType="string">
delete from pe_dept where dept_id=#{deptId}
</delete>