UserMapper.xml
<?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.neuedu.dao.UserMapper">
<select id="list" resultType="User" parameterType="User">
select id,username,password from user
<where>
<if test="username != null">
username like concat('%',#{username},'%')
</if>
</where>
</select>
<insert id="addUser" parameterType="User">
insert into user(username,password) values (#{username},#{password})
</insert>
<select id="getElementById" parameterType="java.lang.Integer" resultType="User">
select id,username, password from user where id= #{id}
</select>
<update id="update" parameterType="User">
update user set username= #{username}, password={password}
where id = #{id}
</update>
<delete id="del" parameterType="java.lang.Integer">
delete from user where id = #{id}
</delete>
<!--
如果方法的参数是一个数组,无论参数交什么名字,collection都必须叫做array;
如果传入的是一个对象,对象的某个属性是数组,我们要循环数组的时候,用属性名,
例如,一个对象有一个属性是 ids,是数组, 如果我们要循环 collection="ids"
更方便的办法,是自己生成代码,
代码是怎么自动生成的,一个方法是导入jar
2、我们自己生成代码
有时间的话讲解分页
-->
<delete id="batchdel" >
delete from user where id in
<foreach collection="array" item="item" separator="," open="(" close=")">
#{item}
</foreach>
</delete>
</mapper>
user/list.jsp
<%--
Created by IntelliJ IDEA.
User: Neuedu
Date: 2020/8/4
Time: 10:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<title></title>
<link rel="stylesheet" href="/css/pintuer.css">
<link rel="stylesheet" href="/css/admin.css">
<script src="/js/jquery.js"></script>
<script src="/js/pintuer.js"></script>
<style>
.searchtxt {
display: inline-block;
width: 300px;
}
</style>
</head>
<body>
<div class="panel admin-panel">
<div class="panel-head"><strong class="icon-reorder"> 用户管理</strong></div>
<div class="padding border-bottom">
<ul class="search">
<li>
<button type="button" class="button border-green" id="btn" ><span class="icon-check"></span> 反选</button>
<button type="submit" class="button border-red" id="batchdel"><span class="icon-trash-o"></span> 批量删除</button>
</li>
<li>
<form method="post">
<input type="text" class="input input-big searchtxt" name="username" size="20" placeholder="请输入用户名" value="${username}" />
<button type="submit" class="button border-main" ><span class="icon-search"></span> 搜索</button>
</form>
</li>
<li>
<a href="add" class="button border-sub" ><span class="icon-plus-square-o"></span> 添加</a>
</li>
</ul>
</div>
<form id="fm" method="post" action="batchdel">
<table class="table table-hover text-center">
<thead>
<tr>
<th width="120"><input id="checkall" type="checkbox" /></th>
<th>用户名</th>
<th>密码</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users}" var="u">
<tr>
<td><input class="checkone" type="checkbox" name="ids" value="${u.id}" />
${u.id}</td>
<td>${u.username}</td>
<td>${u.password}</td>
<td>
<div class="button-group">
<a class="button border-sub" href="update?id=${u.id}" ><span class="icon-edit"></span> 修改</a>
<a class="button border-red" href="javascript:void(0)" onclick="del(${u.id})"><span class="icon-trash-o"></span> 删除</a>
</div>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</form>
</div>
<script type="text/javascript">
function del(id) {
if(confirm('确定要删除该数据吗?')) {
window.location.href = 'del?id=' + id
}
}
$(function() {
$('#checkall').change(function(){
const flag = $(this).prop('checked')
$('.checkone').each(function(index,element){
$(element).prop('checked',flag)
})
})
$('#btn').click(function(){
$('.checkone').each(function(index,element){
const flag = $(element).prop('checked')
$(element).prop('checked',!flag)
})
})
$('#batchdel').click(function(){
if(confirm('确定要批量删除吗?')) {
let flag = false
for(let i = 0; i < $('.checkone').length; i++) {
if( $('.checkone').eq(i).prop('checked')) {
flag = true
break
}
}
if(flag) {
$('#fm').submit()
}else {
alert('没有任何记录被选中')
}
}
})
})
</script>
</body></html>
</body>
</html>