基于springBoot批量删除数据
图片展示
同过input单选框 获取要删除数据的id
1.定义数据层接口
package com.cy.pj.sys.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.cy.pj.sys.pojo.SysLog;
@Mapper
public interface SysLogDao {
/**
* 添加基于id执行日志删除的方法
*/
int deleteObjects(@Param("ids")Integer...ids);
}
2.编写mapper映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cy.pj.sys.dao.SysLogDao">
<delete id="deleteObjects">
delete from sys_Logs
<choose>
<when test="ids!=null and ids.length>0">
<where>
<foreach collection="ids" item="id" separator="or">
id=#{id}
</foreach>
</where>
</when>
<otherwise>
where 1=2
</otherwise>
</choose>
</delete>
</mapper>
3.写service接口层
package com.cy.pj.sys.service;
import com.cy.pj.sys.pojo.PageObject;
import com.cy.pj.sys.pojo.SysLog;
public interface SysLogService {
int deleteObjects(Integer...ids);
}
4.写service接口实现类
package com.cy.pj.sys.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.pojo.PageObject;
import com.cy.pj.sys.pojo.SysLog;
import com.cy.pj.sys.service.SysLogService;
@Service
public class SysLogServiceImpl implements SysLogService {
@Autowired
private SysLogDao sysLogDao;
@Override
public int deleteObjects(Integer... ids) {
//判定参数的合法性
if (ids==null || ids.length==0)
throw new IllegalArgumentException("请选择一个");
//执行删除操作
int rows;
try {
rows = sysLogDao.deleteObjects(ids);
}catch(Throwable e) {
e.printStackTrace();
//发出报警信息(例如给运维人员发短信)
throw new ServiceException("系统故障,正在恢复");
}
//对结果进行验证
if (rows==0)
throw new ServiceException("记录可能已经不存在");
return rows;
}
}
5.写concoller层
package com.cy.pj.sys.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cy.pj.sys.pojo.JsonResult;
import com.cy.pj.sys.service.SysLogService;
@Controller
public class SysLogController {
@Autowired
SysLogService sysLogService;
@RequestMapping("doDeleteObjects")
@ResponseBody
public JsonResult doDeleteObjects(Integer...ids) {
sysLogService.deleteObjects(ids);
return new JsonResult("删除 OK");
}
}
6.现在开始前端的代码和js代码 其功能有通过input选中全部或取消全部
<!-- 删除按钮 -->
<div class="input-group-btn">
<button type="button" class="btn btn-default btn-delete">删除</button>
</div>
<!-- 要显示数据的表单 -->
<div class="box-body table-responsive no-padding">
<table class="table table-hover">
<thead>
<tr>
<th><input type="checkbox" id="checkAll">全选</th>
<th>用户名</th>
<th>操作</th>
<th>请求方法</th>
<th>请求参数</th>
<th>IP</th>
<th>执行时长</th>
</tr>
</thead>
<tbody id="tbodyId">
<tr>
<td colspan="7">数据正在加载中...</td>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript">
$(function(){
/* 获取删除按扭事件 */
$(".input-group-btn").on("click",".btn-delete",doDeleteObjects);
/* 点击表头的input选中,然后表body部分也全部选中input */ $("#checkAll").click(doChangeTBodyCheckBoxState);
/* 当出现body中有input没有被选中 咋取消表头input的选项,当body部分的input全部被选中这是也自动选中表头的input选项*/
$("#tbodyId").on("click",".cBox",doChangeTHeadCheckBoxState);
});
/*点击删除按钮进行对数据删除*/
function doDeleteObjects(){
//获取选中的id值
var ids=doGetCheckedIds();
if(ids.length==0){
alert("至少选择一个");
return;
}
//2.发异步请求执行删除操作
var url ="doDeleteObjects";
var params={"ids":ids.toString()};
$.post(url,params,function(result){
if(result.state==1){
alert(result.message);
doGetObjects();
}else{
alert(result.message);
}
});
$("#checkAll").prop("checked",false);
}
/*获取要删除数据的id*/
/* 定义获取用户选中的记录id的函数。 */
function doGetCheckedIds(){
//定义一个数组,用于存储选中的checkbox的id值
var array=[];//new Array();
//获取tbody中所有类型为checkbox的input元素
$("#tbodyId input[type=checkbox]").
//迭代这些元素,每发现一个元素都会执行如下回调函数
each(function(){
//假如次元素的checked属性的值为true
if($(this).prop("checked")){
//调用数组对象的push方法将选中对象的值存储到数组
array.push($(this).val());
}
});
console.log("array",array)
return array;
}
/* Thead中全选元素的状态影响tbody中checkbox对象状态 */
function doChangeTBodyCheckBoxState(){
//1.获取当前点击对象的checked属性的值
var flag=$(this).prop("checked");//true or false
//2.将tbody中所有checkbox元素的值都修改为flag对应的值
//第一种方案
$("#tbodyId input:checkbox").prop("checked",flag);
//第二种方案
/*
$("#tbodyId input[type='checkbox']")
.prop("checked",flag);
*/
}
/* Tbody中checkbox的状态影响thead中全选元素的状态 */
function doChangeTHeadCheckBoxState(){
//1.设定默认状态值
var flag = true;
//2.迭代所有tbody中的checkbox值并进行与操作
$("#tbodyId .cBox")
.each(function(){
flag=flag&&$(this).prop("checked");
});
//3.修改全选元素checkbox的值为flag
$("#checkAll").prop("checked",flag);
}
</script>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。