一、编写dao
//删除根据ID
@Delete("delete from product where id=#{id}")
public void delete(Integer id);
二、编写service及实现类
//删除单个
public void delete(Integer id);
//删除多个
public void deletes(Integer[] ids);
@Override
public void delete(Integer id) {
productDao.delete(id);
}
//根据ID删除多条数据
public void deletes(Integer[] ids){
if (ids!=null){
for (Integer id:ids){
productDao.delete(id);
}
}
}
三、编写controller控制器
//删除一条数据
@RequestMapping("/delete")
public String delete(Integer id){
productService.delete(id);
return "redirect:findByProduct";
}
//删除多条数据
@RequestMapping("/deletes")
public String deletes(Integer[] ids){
productService.deletes(ids);
return "redirect:findByProduct";(重定向刷新页面)
}
四、编写web文件
<button type="button" class="btn btn-default" title="删除"
onclick='deletes()'>
<i class="fa fa-trash-o"></i> 删除
</button>
<form id="delForm" action="${pageContext.request.contextPath}/product/deletes" method="post">
...(此处省略表单内表格的列名)
<td><input name="ids" type="checkbox" value="${product.id}"></td>(checkbox复选框必去给value值,根据value获取的id进行多条删除)
...(此处省略表单内表格所对应列的数据)
<button type="button" class="btn bg-olive btn-xs" onclick='delOne(${product.id})'>删除</button>(删除单条数据)
</from>
<script type="text/javascript">
//删除单个
function delOne(id){
if (confirm("你确定要删除吗????")) {
//执行删除
location.href="${pageContext.request.contextPath}/product/delete?id="+id;
}
}
function deletes(){
if (confirm("你确定要删除吗????")){
var delForm =$("#delForm");
delForm.submit();
}
}
</script>