springboot + ajax + mybatis 实现批量删除
实现思路:
1. checkbox全选获取批量删除的id数组
2. ajax以字符串的形式将id数组传给控制器
3. 控制器将字符串分割成List数组作为参数传给mapper
具体代码:
1. 前端代码
<table>
<thead>
<tr>
<th>#</th>
<th>id</th>
<th>文件名</th>
<th>文件类型</th>
<th>上传时间</th>
<th>上传用户</th>
<th>文件大小</th>
</tr>
</thead>
<tbody>
<tr th:each="resource:${resources}">
<td>
<input type="checkbox" name="checkId" id="checkId" th:value="${resource.id}"/>
</td>
<td th:text="${resource.id}">Row 1 Data 1</td>
<td th:text="${resource.fileName}">Row 1 Data 2</td>
<td th:text="${resource.fileType}">Row 1 Data 1</td>
<td th:text="${resource.fileTime}">Row 1 Data 2</td>
<td th:text="${resource.fileUploader}">Row 1 Data 1</td>
<td th:text="${resource.fileSize}">Row 1 Data 2</td>
</tr>
</tbody>
</table><br>
<input type="checkbox" name="selectAll" id="selectAll" onclick="selectAll(this);">全选 
<button type="button" onclick="deleteLogic();">批量删除</button>
2. js代码
/**
* checkbox全选/全不全
* @param checkbox
*/
function selectAll(checkbox) {
$('input[name="checkId"]').prop('checked', $(checkbox).prop('checked'));
} /**
* 批量删除
*/
function deleteLogic() { var checkNum = $("input[name='checkId']:checked").length; if(checkNum==0){
alert("至少选择一项");
return;
} if(confirm("确定要删除吗?")){
var checkList = new Array();
$("input[name='checkId']:checked").each(function () {
checkList.push($(this).val())
});
} $.ajax({
url:"/deleteAll",
type:"post",
data:{
checkList:checkList.toString()
},
datatype:"json",
success:function (data) {
location.reload();
alert("删除成功!")
},
error:function (msg) {
alert("删除失败!")
}
})
}
3. Controller代码
@PostMapping("/deleteAll")
@ResponseBody
public String deleteAll(String checkList){
System.out.println("==>"+checkList);
String[] strs = checkList.split(",");
List<Integer> ids = new ArrayList<>();
for(String str:strs){
ids.add(Integer.parseInt(str));
}
resourcesService.deleteAll(ids);
return "success";
}
4. mapper.xml代码
<delete id="deleteAll" parameterType="list">
delete from resources where id in
<foreach collection="list" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
springboot + ajax + mybatis 实现批量删除的更多相关文章
- Mybatis实现批量删除数据
Mybatis实现批量删除操作 学习内容: 1. 使用 2. 代码实现 2.1 UserMapper.java 接口 2.2 UserMapper.xml 总结: 学习内容: 1. 使用 这里通过动态 ...
- mybatis的批量删除
公司工程用的是Mybatis的example的类,自动生成了对数据库的操作,批量操作的今天用到了,两种方式,一种需要拓展它生成的类,另一种自带的. 批量删除的id是以集合List传递 id以List& ...
- (后端)Mybatis实现批量删除操作(转)
原文地址:https://blog.csdn.net/javaee_sunny/article/details/52511842 一. 这里主要考虑两种参数类型:数组或者集合. 而这点区别主要体现在E ...
- mybatis springmvc批量删除 2最新
service层: @Override public void batchDeletes(List list) { creditDao.batchDeletes(list); } 控制层control ...
- 使用Ajax实现的批量删除操作(C#)
今天做了一个简单的批量删除操作,虽然简单,但是很多问题出现,终究还是技术不够熟练. 现在在这里跟大家分享一下.仅供学习... 1.在前台获取用户点击的信息id,把这里id封装到一个数组里面:(rows ...
- mybatis的批量删除操作
需求描述:将符合某条件的几条记录删除 解决思路:对于多个id,可以使用sql关键词 in ,只要满足数据库的id在你的id集合或者list中,就删除,从而实现批量删除.循环delete方法是在是low ...
- Mybatis实现批量删除
知识点:当传入参数为数组时,XX.xml文件中,标签为collection属性 参考博客:https://blog.csdn.net/javaee_sunny/article/details/5251 ...
- mybatis postgresql 批量删除
一.需求介绍 前端是一个列表页面,列表可以进行复选框的选择,后台进行关联表数据的删除. 二.框架介绍 springboot+mybatis 数据库用的postgresql 三.具体代码(前端js) 1 ...
- Mybatis批量删除之Error code 1064, SQL state 42000;
(一)小小的一次记载. (二):最近的项目都是使用MyBatis,批量新增自己都会写了,但是一次批量删除可把我给折腾了下,写法网上都有,但是照着做就是不行,最后问公司的人,问网友才得到答案,那就是jd ...
随机推荐
- C++的ofstream与ifstream使用
基本理解: ofstream是从内存到硬盘,ifstream是从硬盘到内存,其实所谓的流缓冲就是内存空间; 在C++中,有一个stream这个类,所有的I/O都以这个“流”类为基础的,包括我们要认识的 ...
- diji模板
void diji(int x){ fill(dis,dis+n,INT_MAX); dis[x] = ; ;i < n;i++) pre[i] = i; ){ int minn = INT_M ...
- springboot使用自带连接池连接postgre
Application配置spring.datasource.url=jdbc:postgresql://***:5432/postgresspring.datasource.username=pos ...
- 【HTML】框架集(Framesets)
1.Frameset的使用 所谓框架便是网页画面分成几个框窗,同时取得多个 URL.只 要 <FRAMESET> <FRAME> 即可,而所有框架标记 要放在一个总起的 htm ...
- db2,用户名密码不对导致无法连接数据库: Reason: User ID or Password invalid. ERRORCODE=-4214, SQLSTATE=28000
文章目录 背景 解决 背景 qa需要db2的demo,运维给安装完db2,启动报错 com.ibm.db2.jcc.am.io: [jcc][t4][2013][11249][4.7.112] Con ...
- 9、TestNG介绍与安装
介绍 TestNG是一个受JUnit和NUnit启发的测试框架,但引入了一些新功能,使其更加强大和易于使用,例如: 注释. 在任意大的线程池中运行你的测试,这些线程池有各种可用的策略(所有方法在他们自 ...
- 引入scss(@import)和其中易错点
1.引入文件方式 @import 'url'; ./ :当前目录 ../ :上级目录 src/api/styles: 绝对路径 2.一般在main.js中引用当做全局样式 import 'styles ...
- csv 基本操作, 报错解决(UnicodeEncodeError: 'utf-8' codec can't encode characters in position 232-233: surrogates not allowed)
最常用的一种方法,利用pandas包 import pandas as pd #任意的多组列表 a = [1,2,3] b = [4,5,6] #字典中的key值即为csv中列名 dataframe ...
- ajax实现异步刷新
1. 导入 json 包: jackson-annotations-2.8.9.jar jackson-core-2.8.9.jar jackson-databind-2.8.9.jar json.j ...
- 图像处理_Image
1. 安装 输入 pip install PIL报错: ERROR: Could not find a version that satisfies the requirement PI ...