皮皮辉用AJAX实现批量删除

什么是 AJAX ?

  • AJAX = 异步 JavaScript 和 XML

  • AJAX 是一种用于创建快速动态网页的技术。

    通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。
    传统的网页(不使用 AJAX)如果需要更新内容,必需重载整个网页面。

实现批量删除

IuserInfoDao里

加入如下代码
void deleteAll(List ids);

IuserInfoService里

加入如下代码:
void deleteAll(List ids);

IuserInfoServiceImpl里

加入如下代码:

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

IuserInfoController里

加入如下代码:

@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++){
            ids.add(Integer.parseInt(strs[i]));
        }
        userInfoService.deleteAll(ids);
        return "";
    }

UserInfoMapper里

加入sql语句

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

user-list.jsp里

  • 在这里用到AJAX
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.2.1.min.js"></script>
	<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.toString()},
				success:function () {
					alert("删除成功");
					location.reload();
                },
				error:function () {
					alert("删除失败");
                }
			})
        }
	</script>
  • 工具栏里加入下图代码块
    在这里插入图片描述

日常一个BUG

  • 我觉得以后我可以去做测试人员,嗯,是这样
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: 
        ###Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 2
发布了10 篇原创文章 · 获赞 1 · 访问量 354

猜你喜欢

转载自blog.csdn.net/qq_38147101/article/details/96429984