SSM 框架的批量删除

来源:互联网 发布:计算器编程代码 编辑:程序博客网 时间:2024/05/29 03:47
JSP  页面部分   批量删除 里面  ${QX.del}是来判断该用户是否有该权限
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn"  uri="http://java.sun.com/jsp/jstl/functions"%>


<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html
<html lang="en">
<head>
<base href="<%=basePath%>">
<!-- jsp文件头和头部 -->
<%@ include file="../admin/top.jsp"%> 

</head> 


<body>


<table id="table_report" class="table table-striped table-bordered table-hover">

<thead>
<tr>
 <th class="center"><label><input type="checkbox"
id="zcheckbox" /><span class="lbl"></span></label></th>
<th class='center' >图片</th>
<th class='center' >货物编号</th>
<th class='center' >货物名称</th>
<th class='center' >货物规格</th>
<th class='center' >所属品牌</th>
<th class='center' >计量单位</th>
<th class='center' >零售价</th>
<th class='center' >自提价格</th>
<th class='center' >配送价格</th>
<th class='center' >货物分类</th>
<th class='center' >供应商</th>
<th class='center' >库存量</th>
<th class="center">已售量</th>
<th class="center">是否上架</th>
<th class="center">是否推荐</th>
<th class="center">操作</th>
</tr>
</thead>

<tbody>

<!-- 开始循环 -->
<c:choose>
<c:when test="${not empty axGoodsList}">
<c:if test="${QX.cha == 1 }">
<c:forEach items="${axGoodsList}" var="agl" >
<tr>
<td class='center' style="width: 30px;"><label><input
type='checkbox' name='ids' value="${agl.pk_ax_goods}" /><span
class="lbl"></span></label></td>
<td class="center"  style="height: 30px; width: 30px;">
       <img id="Img1"
src="<%=basePath%>../uploadFiles/uploadImgs/${agl.imgurl}"
 onclick="photo('${agl.pk_ax_goods}');"  >
</td>
<td class="center">${agl.goodcode}</td>
<td class="center">${agl.goodname}</td>
<td class="center">${agl.goodspec}</td>
<td class="center">${agl.brands}</td>
<td class="center">${agl.measurename}</td>
<td class="center">${agl.price}</td>
<td class="center">${agl.def18}</td>
<td class="center">${agl.def19}</td>
<td class="center">${agl.goodcla}</td>
<td class="center">${agl.supplier}</td>
<td class="center">${agl.inventory}</td>
<td class="center" >${agl.salenumber}</td>
<!-- <td style="width: 60px;" class="center"><c:if
test="${agl.status =='N' }">
<span class="label label-important arrowed-in">冻结</span>
</c:if> <c:if test="${agl.status =='Y' }">
<span class="label label-success arrowed">正常</span>
</c:if></td> -->
<td  class="center">
         <c:if test="${agl.issale =='Y'}"><span>是 </span></c:if>
         <c:if test="${agl.issale =='N'}"><span>否 </span></c:if>
</td>
<td  class="center">
         <c:if test="${agl.isrecommend =='0'}"><span>是 </span></c:if>
         <c:if test="${agl.isrecommend =='1'}"><span>否 </span></c:if>
</td>
<td  class="center" style="width:38px;">
<div id="sss"  class='hidden-phone visible-desktop btn-group ' >
<a class='btn btn-mini btn-pink'   title="详情" onclick="look('${agl.pk_ax_goods}');"><i class='icon-eye-open'></i></a>
<c:if test="${QX.edit == 1 }">
<c:if test="${user.USERNAME != 'admin'}">
<a class='btn btn-mini btn-info' title="编辑" onclick="edit('${agl.pk_ax_goods }');"><i class='icon-edit'></i></a>
</c:if>
<c:if test="${user.USERNAME == 'admin'}">
<a class='btn btn-mini btn-info' title="您不能编辑"><i
class='icon-edit'></i></a>
</c:if>
</c:if>
<c:choose>
<c:when test="${user.USERNAME=='admin'}">
<a class='btn btn-mini btn-danger' title="不能删除"><i
class='icon-trash'></i></a>
</c:when>
<c:otherwise>

<a class='btn btn-mini btn-purple' title="修改小图标"
onclick="editTP('${agl.pk_ax_goods}');"> <i
class='icon-picture'></i></a>
<a class='btn btn-mini' title="修改大图标"
onclick="editTP1('${agl.pk_ax_goods}');"> <i
class='icon-picture'></i></a>

<c:if test="${QX.del == 1 }">
<a class='btn btn-mini btn-danger' title="删除" onclick="del('${agl.pk_ax_goods }','${agl.goodname }');"><i class='icon-trash'></i></a>
</c:if>
</c:otherwise>
</c:choose>
</div>
</td>
</tr>
</c:forEach>
</c:if>
<c:if test="${QX.cha == 0 }">
<tr>
<td colspan="10" class="center">您无权查看</td>
</tr>
</c:if>
</c:when>
<c:otherwise>
<tr class="main_info">
<td colspan="10" class="center">没有相关数据</td>
</tr>
</c:otherwise>
</c:choose>
</tbody>
</table>
<div class="page-header position-relative">
<table style="width: 100%;">
<tr>
<td style="vertical-align: top;"><c:if
test="${QX.add == 1 }">
<a class="btn btn-small btn-success" onclick="add();">新增</a>
</c:if> <c:if test="${QX.del == 1 }">
<a class="btn btn-small btn-danger"
onclick="makeAll('确定要删除选中的数据吗?');" title="批量删除"><i
class='icon-trash'></i></a>
</c:if></td>
<td style="vertical-align: top;"><div class="pagination"
style="float: right; padding-top: 0px; margin-top: 0px;">${page.pageStr}</div></td>
</tr>
</table>
</div>
</form>
</div>




<!-- 引入 -->
<script type="text/javascript">window.jQuery || document.write("<script src='static/js/jquery-1.9.1.min.js'>\x3C/script>");
</script>
<!-- 这个是弹出提示框必须的 -->
<script src="static/js/bootstrap.min.js"></script>
       <!-- 这个是批量删除必须的 -->
<script type="text/javascript" src="static/js/bootbox.min.js"></script><!-- 确认窗口 -->
<!-- 引入 -->



<script type="text/javascript">




//批量操作
function makeAll(msg){
bootbox.confirm(msg, function(result) {
if(result) {
var str = '';
var emstr = '';
var phones = '';
for(var i=0;i < document.getElementsByName('ids').length;i++)
{
 if(document.getElementsByName('ids')[i].checked){
  if(str=='') str += document.getElementsByName('ids')[i].value;
  else str += ',' + document.getElementsByName('ids')[i].value;
 
  if(emstr=='') emstr += document.getElementsByName('ids')[i].id;
  else emstr += ';' + document.getElementsByName('ids')[i].id;
 
  if(phones=='') phones += document.getElementsByName('ids')[i].alt;
  else phones += ';' + document.getElementsByName('ids')[i].alt;
 }
}
if(str==''){
bootbox.dialog("您没有选择任何内容!", 
[
 {
"label" : "关闭",
"class" : "btn-small btn-success",
"callback": function() {
//Example.show("great success");
}
}
]
);
$("#zcheckbox").tips({
side:3,
           msg:'点这里全选',
           bg:'#AE81FF',
           time:8
       });
return;
}else{
if(msg == '确定要删除选中的数据吗?'){
top.jzts();
$.ajax({
type: "POST",
url: '<%=basePath%>axgoods/deleteAll.do?tm='+new Date().getTime(),
    data: {v:str},
dataType:'json',
//beforeSend: validateData,
cache: false,
success: function(data){
$.each(data.list, function(i, list){
top.jzts();
setTimeout("self.location.reload()",100);
});
}
});
}
}
}
});
}



</script>




   




==================
后台部分
@RequestMapping(value = "/deleteAll")
@ResponseBody
public Object deleteAll() {
PageData pd = new PageData();
Map<String, Object> map = new HashMap<String, Object>();
try {
pd = this.getPageData();
List<PageData> pdList = new ArrayList<PageData>();
String v = pd.getString("v");


if (null != v && !"".equals(v)) {
String Arrayvehicle[] = v.split(",");
if (Jurisdiction.buttonJurisdiction(menuUrl, "del")) {
axGoodsService.deleteAll(Arrayvehicle);
}
pd.put("msg", "ok");
} else {
pd.put("msg", "no");
}
pdList.add(pd);
map.put("list", pdList);
} catch (Exception e) {
logger.error(e.toString(), e);
} finally {
logAfter(logger);
}
return AppUtil.returnObject(pd, map);
}



=====service

public void deleteAll(String[] v) throws Exception {
this.daoSupport.update("AxGoodsMapper.deleteAll", v);
}



mybatis操作数据库部分




  <!-- 批量删除 -->
<delete id="deleteAll" parameterType="String" flushCache="false">
update ax_goods set dr=1
where
pk_ax_goods in
<foreach item="item" index="index" collection="array" open="("
separator="," close=")">
#{item}
</foreach>
</delete>







MyBatis的foreach语句详解


foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item,index,collection,open,separator,close。item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:

1.    如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

2.    如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

3.    如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key

下面分别来看看上述三种情况的示例代码:

1.单参数List的类型:
 

  <select id="dynamicForeachTest"resultType="Blog">
       select *from t_blog where id in
      <foreach collection="list" index="index" item="item"open="(" separator="," close=")">
         #{item}
      </foreach>
   </select>
上述collection的值为list,对应的Mapper是这样的

List<Integer> ids = newArrayList<Integer>();
 

     ids.add(1);
      ids.add(3);
      ids.add(6);

2.单参数array数组的类型:
 

  <select id="dynamicForeach2Test"resultType="Blog">
       select *from t_blog where id in
      <foreach collection="array" index="index"item="item" open="(" separator="," close=")">
         #{item}
      </foreach>
   </select>
上述collection为array,对应的Mapper代码:

2.单参数array数组的类型:
 

  <select id="dynamicForeach2Test"resultType="Blog">
       select *from t_blog where id in
      <foreach collection="array" index="index"item="item" open="(" separator="," close=")">
         #{item}
      </foreach>
   </select>
上述collection为array,对应的Mapper代码:

3.自己把参数封装成Map的类型
 

  <select id="dynamicForeach3Test"resultType="Blog">
       select *from t_blog where title like "%"#{title}"%" and id in
      <foreach collection="ids" index="index" item="item"open="(" separator="," close=")">
         #{item}
      </foreach>
   </select>
上述collection的值为ids,是传入的参数Map的key,对应的Mapper代码:

List<Integer> ids = newArrayList<Integer>();
 

     ids.add(1);
      ids.add(2);
      ids.add(3);
      ids.add(6);
      ids.add(7);
      ids.add(9);
      Map<String, Object> params = newHashMap<String, Object>();
      params.put("ids", ids);
      params.put("title", "中国");










 
原创粉丝点击