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

點(diǎn)擊全選

再次點(diǎn)擊全選

刪除提示

確認(rèn)刪除

代碼部分,含有簡(jiǎn)單單個(gè)刪除
(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";
}
代碼思路:
從前臺(tái)勾選的選擇框中傳過來的值用“,”分開,然后遍歷存放到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表示集合中每一個(gè)元素進(jìn)行迭代時(shí)的別名. (直接找到對(duì)應(yīng)的delList集合里面的所有元素,item="item"中的item(后一個(gè))必須與#{item} 中的item一致)
index指 定一個(gè)名字,用于表示在迭代過程中,每次迭代到的位置.
open表示該語句以什么開始,separator表示在每次進(jìn)行迭代之間以什么符號(hào)作為分隔 符.
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() {
//判斷至少寫了一項(xiàng)
var checkedNum = $("input[name='checkbox']:checked").length;
if (checkedNum == 0) {
alert("請(qǐng)至少選擇一項(xiàng)!");
return false;
}
//創(chuàng)建數(shù)組,存儲(chǔ)選擇的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)要?jiǎng)h除這"+checkedList.length+"條數(shù)據(jù)嗎?")
if(flag){
//傳參,后端繼續(xù)進(jìn)行刪除操作,傳到后端的是一個(gè)String數(shù)組
window.location.href="http://localhost:8080/batchDeletes?ids=" rel="external nofollow" +checkedList;
}
})
//單個(gè)刪除
function delByID(id){
window.location.href="http://localhost:8080/delByID?id=" rel="external nofollow" +id
}
</script>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
feign調(diào)用返回object類型轉(zhuǎn)換方式
這篇文章主要介紹了feign調(diào)用返回object類型轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
詳解Java中的File文件類以及FileDescriptor文件描述類
在Java中File類可以用來新建文件和目錄對(duì)象,而FileDescriptor類則被用來表示文件或目錄的可操作性,接下來我們就來詳解Java中的File文件類以及FileDescriptor文件描述類2016-06-06
Java 線程狀態(tài)和等待喚醒機(jī)制和線程池的實(shí)現(xiàn)
這篇文章主要介紹了Java 線程狀態(tài)和等待喚醒機(jī)制和線程池的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Java中StringUtils工具類的一些用法實(shí)例
這篇文章主要介紹了Java中StringUtils工具類的一些用法實(shí)例,本文著重講解了isEmpty和isBlank方法的使用,另外也講解了trim、strip等方法的使用實(shí)例,需要的朋友可以參考下2015-06-06
反射機(jī)制:getDeclaredField和getField的區(qū)別說明
這篇文章主要介紹了反射機(jī)制:getDeclaredField和getField的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

