hibernate checkbox 根据id批量删除

<td >
<label class="i-checks">
<input type="checkbox" name="subBox" value="${list.id}">
<i></i>
</label>
</td>

<script type="text/javascript">
$(function() {
$("#del-vod").click(function() {
var ids= new Array();
var i=0;
$("input:checkbox[name='subBox']:checked").each(function() { // 遍历name=test的多选框
ids[i++]=$(this).val(); // 每一个被选中项的值
});
if(ids=="") {
alert("请至少选择一条记录");
return false;
}
if(window.confirm("确定删除吗?")) {
window.location.href="deletes.do?ids="+ids; 
}

});
}) 
</script>

 

dao层

//该方法效率慢,生成多条HQL

public void deletes(List<Integer> idList) {
for (Integer id : idList) {
Timeliftinfo timeinfo = (Timeliftinfo)getSession().load(Timeliftinfo.class, id);
if(timeinfo != null) {
getSession().delete(timeinfo);
}
}
}

 

//优化后,只生成一条HQL

 

public void deletes(List<Integer> idList) {
String hql = "";
for (int i = 0; i < idList.size(); i++) {
if(i==0) {
hql = "id="+idList.get(i);
} else {
hql =hql + " or id="+idList.get(i);
}
Session session= this.getSession();
Query q= session.createQuery("delete from Timeliftinfo where "+hql);
q.executeUpdate();
}

}

 

 

 

Service调用接口,action

//批量删除
@RequestMapping(value = "/vms/media_main/deletes.do")
public ModelAndView deletes(String ids) {
String[] id = ids.split(",");
List<Integer> list = new ArrayList<Integer>();
for (int i=0;i<id.length;i++){
int a = new Integer(id[i]);
list.add(a);
}
timeService.deletes(list);
Map<String,Object> data = new HashMap<String, Object>();
data.put("message","删除成功");
data.put("url","SY_timeliftinfoList.do");
return new ModelAndView("vms/media_main/save",data);
}

 

posted @ 2017-02-10 15:37  -J  阅读(2653)  评论(0编辑  收藏  举报