暑期 java 实训 7 批量删除

添加权限控制

在 head.jsp 开头中添加

<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags" %>

完成登录后显示名称, 只需要将下面代码放入head.jsp 和 aside.jsp 文件中

<security:authentication property="principal.username"></security:authentication>

管理角色权限控制,只有管理员可以完成, 将链接地址包含在<security:authorize access="hasRole('ADMIN')"> 内部

<security:authorize access="hasRole('ADMIN')">
    <a
            href="${pageContext.request.contextPath}/user/allUser.do?page=1&size=5"> <i
            class="fa fa-circle-o"></i> 用户管理
    </a>
</security:authorize>

批量删除功能

1、导入jQuery库

在这里插入图片描述

2、在 user-list.jsp 中引用

<head>中添加

<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.2.1.min.js"></script>

3、编写 jQuery函数

    <script type="text/javascript">
        function deleteAll() {
            var checkNum = $("input[name='ids']:checked").length;
            if (checkNum == 0) {
                alert("请至少选择一项");
                return;
            }
            if (confirm("确定删除吗?")) {
                var userList = new Array();
                $("input[name='ids']:checked").each(function () {
                    userList.push($(this).val())
                });
            }
            $.ajax({
                type: "post",
                url: "${pageContext.request.contextPath}/user/deleteAll.do",
                data: {userList: userList.toLocaleString()},
                success: function () {
                    alert("删除成功");
                    location.reload()
                },
                error: function () {
                    alert("删除失败");
                }

            })
        }
    </script>

4、调用jQuery函数

在 user-list.jsp中修改界面,布局出一个按钮来触发该函数

<button type="button"
        class="btn btn-default" title="删除" onclick="deleteAll()">
    <i class="fa fa-refresh"></i> 删除
</button>

5、完成控制层

接收前台来的字符串,分割为数组

@RequestMapping("/deleteAll.do")
@ResponseBody
public String deleteAll(String userList) {
    String[] strs = userList.split(",");
    List<Integer> ids = new ArrayList<>();
    for (int i = 0; i < strs.length; i++) {
        //将string类型转为 int
        ids.add(Integer.parseInt(strs[i]));
    }
    userInfoService.deleteAll(ids);
    return "";
}

6、完成服务处

   public void deleteAll(List<Integer> ids) {
        userInfoDao.deleteAll(ids);
    }

7、完成 dao 层

public void deleteAll(List<Integer> ids);

8、完成 mapper 映射

<delete id="deleteAll" parameterType="list">
    delete from userinfo where id in
    <foreach collection="list" item="id" open="(" close=")" separator=",">
        #{id}
    </foreach>
</delete>

9、完成效果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值