java實現(xiàn)多選批量刪除功能
本文為大家分享了java實現(xiàn)多選批量刪除的具體代碼,幫助大家更好的理解批量刪除功能的實現(xiàn)過程,供大家參考,具體內(nèi)容如下
本文用到的框架是:springmvc+mybatis
實現(xiàn)思路:多選復(fù)選框多個刪除,點擊全選全部選中,再次點擊全部取消,為了保證操作的安全,應(yīng)該提示框進(jìn)行提升,用戶再次點擊確認(rèn)刪除進(jìn)行刪除,把選中的多個復(fù)選框的值傳到后端進(jìn)行循環(huán)刪除,最后刷新數(shù)據(jù),公司中為了保證數(shù)據(jù)安全一般不會真正刪除而是把數(shù)據(jù)修改狀態(tài)進(jìn)行隱藏,也就是修改,這邊以完全刪除為例
部分效果截圖(頁面簡陋)

點擊全選

再次點擊全選

刪除提示

確認(rèn)刪除

代碼部分,含有簡單單個刪除
(1)controller
@RequestMapping("/batchDeletes")
//批量刪除
public String delAnimal(String ids){
List<String> delList = new ArrayList<String>();
String[] strs = ids.split(",");
for (String str : strs) {
delList.add(str);
}
//開始循環(huán)批量刪除
testService.batchDeletes(delList);
//重定向刷新數(shù)據(jù)
return "redirect:/showAnimal";
}
@RequestMapping("/delByID")
public String delByID(int id){
testService.delByID(id);
//重定向刷新數(shù)據(jù)
return "redirect:/showAnimal";
}
代碼思路:
從前臺勾選的選擇框中傳過來的值用“,”分開,然后遍歷存放到delList集合里面,直接刪delList集合里面的所有字符串。
(2)service
void batchDeletes(List delList); void delByID(int id);
(3)serviceImpl
@Override
public void batchDeletes(List delList) {
testDao.batchDeletes(delList);
}
@Override
public void delByID(int id) {
testDao.delByID(id);
}
(4)dao
void batchDeletes(List delList); void delByID(int id);
(5)mapper.xml
<!--批量刪除 -->
<delete id="batchDeletes" parameterType="java.util.List">
delete from animal where id in
<!--循環(huán)刪除 -->
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
<delete id="delByID" parameterType="int">
delete from animal where id=#{id}
</delete>
如上的mybatis指代的意思如下:
foreach元素的屬性主要有 item,index,collection,open,separator,close。
item表示集合中每一個元素進(jìn)行迭代時的別名. (直接找到對應(yīng)的delList集合里面的所有元素,item="item"中的item(后一個)必須與#{item} 中的item一致)
index指 定一個名字,用于表示在迭代過程中,每次迭代到的位置.
open表示該語句以什么開始,separator表示在每次進(jìn)行迭代之間以什么符號作為分隔 符.
close表示以什么結(jié)束.
前端頁面代碼
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: wx_weiyihe
Date: 2021/8/24
Time: 14:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<html>
<head>
<title>Title</title>
</head>
<body>
<input type="button" value="批量刪除" id="deleteButton">
<table border="1px" cellspacing="0px">
<tr>
<th align="center">
<input type="checkbox" id="SelectAll" onclick="selectAll();" /> 全選</th>
<th>ID</th>
<th>名稱</th>
<th>年齡</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="animal">
<tr>
<td align="center"><input type="checkbox" name="checkbox" value="${animal.id}"></td>
<td>${animal.id}</td>
<th>${animal.name}</th>
<th>${animal.age}</th>
<th><input type="button" value="刪除" onclick="delByID('${animal.id}')"></th>
</tr>
</c:forEach>
</table>
</body>
<script>
//全選(全不選)
function selectAll(){
//如果選擇全選按鈕
if ($("#SelectAll").is(":checked")) {
$(":checkbox").prop("checked", true);//所有選擇框都選中
} else { //如果沒有選擇全選按鈕
$(":checkbox").prop("checked", false); //全部不選中
}
}
//批量刪除
$("#deleteButton").on("click", function() {
//判斷至少寫了一項
var checkedNum = $("input[name='checkbox']:checked").length;
if (checkedNum == 0) {
alert("請至少選擇一項!");
return false;
}
//創(chuàng)建數(shù)組,存儲選擇的id
var checkedList = new Array();
$("input[name='checkbox']:checked").each(function () {
//把當(dāng)前選中的復(fù)選框的id存入數(shù)組中
checkedList.push($(this).val());
});
//提示刪除
var flag=confirm("確認(rèn)要刪除這"+checkedList.length+"條數(shù)據(jù)嗎?")
if(flag){
//傳參,后端繼續(xù)進(jìn)行刪除操作,傳到后端的是一個String數(shù)組
window.location.href="http://localhost:8080/batchDeletes?ids=" rel="external nofollow" +checkedList;
}
})
//單個刪除
function delByID(id){
window.location.href="http://localhost:8080/delByID?id=" rel="external nofollow" +id
}
</script>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring-webflux?響應(yīng)式編程的實例詳解
Spring 提供了兩個并行堆棧,一種是基于帶有 Spring MVC 和 Spring Data 結(jié)構(gòu)的 Servlet API,另一個是完全反應(yīng)式堆棧,它利用了 Spring WebFlux 和 Spring Data 的反應(yīng)式存儲庫,這篇文章主要介紹了Spring-webflux?響應(yīng)式編程,需要的朋友可以參考下2022-09-09
Java中的synchronized和ReentrantLock的區(qū)別詳細(xì)解讀
這篇文章主要介紹了Java中的synchronized和ReentrantLock的區(qū)別詳細(xì)解讀,synchronized是Java內(nèi)建的同步機制,所以也有人稱其為 IntrinsicLocking,它提供了互斥的語義和可見性,當(dāng)一個線程已經(jīng)獲取當(dāng)前鎖時,其他試圖獲取的線程只能等待或者阻塞在那里,需要的朋友可以參考下2024-01-01

