实现这个功能的基本思路如下:
1:在jsp页面中获取到要删除对象的ID,一个或多个,批量当然是多个
2:在action中就要接收这些ID,并对这些ID进行初步的处理
3:在实现层进行数据库的操作
步骤一:在jsp页面中,用jquery获取到对象的ID
<tr align="center" bgcolor="#FAFAF1" height="22">
<th width="4%"><input name="selectall" type="checkbox" id="selectall" value="101" class="np">全选</th>
<th width="6%">作业名称</th>
<th width="20%">作业简介</th>
<th width="10%">作业成绩</th>
<th width="10%">作业上传时间</th>
<th width="10%">提交人</th>
<th width="6%">操作</th>
</tr>
<s:iterator value="#session.allStaskpageResultSet.list" id="stask">
<!-- select stask.num,
stask.name as staskName,
stask.content,
stask.grade,
stask.uploadtime,
student.name as studentName -->
<tr align='center' bgcolor="#FFFFFF" onMouseMove="javascript:this.bgColor='#FCFDEE';" onMouseOut="javascript:this.bgColor='#FFFFFF';" height="50" >
<td><input name="atask" type="checkbox" id="ask" value="<s:property value="#stask[0]" />" class="np"></td>
<td><s:property value="#stask[1]" /></td>
<td><s:property value="#stask[2]" /></td>
<td><s:property value="#stask[3]" /></td>
<td><s:date name="#stask[4]" format="yyyy-MM-dd"/></td>
<td><s:property value="#stask[5]" /></td>
<td><a href="#" onclick="check()">删除</a></td>
</tr>
</s:iterator>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
这里是用struts2标签,对数据进行迭代的输出
jquery全选的代码:
$("#selectall").click(
function(){
if(this.checked){
$("input[name='atask']").each(function(){this.checked=true;});
}else{
$("input[name='atask']").each(function(){this.checked=false;});
}
}
);
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
jquery点击删除按钮时,对ID的获取,并跳转到action:
function check() {
var msg = "您真的确定要删除吗?";
if (confirm(msg)==true){
var allcheckbox = "";
var becheckbox = "";
$("input[name=atask]").each(function(){ //遍历table里的全部checkbox
allcheckbox += $(this).val() + ","; //获取所有checkbox的值
if($(this).attr("checked")) //如果被选中
becheckbox += $(this).val() + ","; //获取被选中的值
});
if(becheckbox.length > 0) //如果获取到
becheckbox = becheckbox.substring(0, becheckbox.length - 1); //把最后一个逗号去掉
window.location = "astask_batch_delete.action?checkTnum="+becheckbox;
}else{
return false;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
然后就是在action中,对数据进行接收:
private String checkTnum;
public String getCheckTnum() {
return checkTnum;
}
public void setCheckTnum(String checkTnum) {
this.checkTnum = checkTnum;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
批量删除的方法:
public String batch_delete(){
String [] checkedNums = checkTnum.split(",");
adminService.batchDeleteStask(checkedNums);
return "batch_delete";
}
- 1
- 2
- 3
- 4
- 5
这是对接收到的ID进行处理,然后再调用Service层的批量删除方法:
@Transactional
public void batchDeleteAsk(String [] anums){
String hql = "";
for(int i=0;i<anums.length;i++) {
if(i==0) {
hql = "anum="+anums[i];
} else {
hql =hql + " or anum="+anums[i];
}
}
hql= "delete from Ask where "+hql;
adminDao.batchDelete(hql,anums);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
这是进行hql查询语句的拼凑,先判断长度,当ID就只有一个的时候,就删除一个,当多个时,就可以删除多个,可以在后台看hibernate输出的查询语句,是用or来连接各个ID的条件的
下面,在admin接口中定义了批量删除的方法
//批量删除留言
public void batchDelete(final String hql,String[] anums);
- 1
- 2
下面是实现数据库的语句
@Override
public void batchDelete(String hql,String[] anums) {
Session session=sessionFactory.getCurrentSession();
Query query=session.createQuery(hql);
query.executeUpdate();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
这里,也可以用JDBC的本地语句来实现,到这里,就已经基本完成这个功能了,粗略的思路就是这样。
--------------------- 本文来自 aouixh 的优快云 博客 ,全文地址请点击:https://blog.youkuaiyun.com/aouixh/article/details/46057893?utm_source=copy