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); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM