主页
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="<%=basePath%>user/list.action">成员列表</a>
</body>
</html>
----------------------------------------------------------------------------------------------------------------
列表页
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<body>
<form action="<%=basePath%>user/del.action" method="post">
<table border="1">
<tr><td>选择</td><td>序号</td><td>用户名</td><td>密码</td></tr>
<c:forEach items="${userlist}" var="user">
<tr>
<td><input type="checkbox" value="${user.uid}" name="id"></td>
<td>${user.uid}</td>
<td>${user.username}</td>
<td>${user.password}</td>
</tr>
</c:forEach>
<tr><td colspan="4"><input type="checkbox" name="All" id="All" οnclick="selectAll()">全选/反选</td></tr>
</table>
<input type="submit" value="删除">
</form>
</body>
<script type="text/javascript">
document.getElementById('All').οnclick=function(){
var checkElements=document.getElementsByName('id');
if(this.checked){
for(var i=0;i<checkElements.length;i++){
var checkElement=checkElements[i];
checkElement.checked="checked";
}
}else{
for(var i=0;i<checkElements.length;i++){
var checkElement=checkElements[i];
checkElement.checked=null;
}
}
}
</script>
</html>
----------------------------------------------------------------------------------------------------------------
Controller
package controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
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.RequestMethod;
import service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
// 不用request,根据input name属性,一样直接传递
//---------批量删除----------
@RequestMapping("/list")
public String userlist(HttpSession httpSession) {
httpSession.setAttribute("userlist", userService.userlist());
return "userlist";
}
@RequestMapping("/del")
public String DelMatch(HttpServletRequest request) {
String Uid[]=request.getParameterValues("id");
for(int i=0;i<Uid.length;i++) {
userService.DelMatch(Uid[i]);
}
return "redirect:/user/list.action";
}
}
----------------------------------------------------------------------------------------------------------------
Service
package service;
import java.util.List;
import pojo.User;
public interface UserService {
public List<User> userlist();
public String DelMatch(String id);
}
ServiceImpl
package service;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import dao.UserMapper;
import pojo.User;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<User> userlist() {
List<User> list=userMapper.selectAllUser();
return list;
}
@Override
public String DelMatch(String id) {
int Id=Integer.parseInt(id);
userMapper.deleteByIds(Id);
return null;
}
}
----------------------------------------------------------------------------------------------------------------
Mapper.java
package dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import pojo.User;
import pojo.UserExample;
public interface UserMapper {
int countByExample(UserExample example);
int deleteByExample(UserExample example);
int deleteByPrimaryKey(Integer uid);
int insert(User record);
int insertSelective(User record);
List<User> selectByExample(UserExample example);
User selectByPrimaryKey(Integer uid);
int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);
int updateByExample(@Param("record") User record, @Param("example") UserExample example);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
List<User> selectAllUser();
void deleteByIds(int Id);
}
Mapper.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="dao.UserMapper" >
<resultMap id="BaseResultMap" type="pojo.User" >
<id column="uid" property="uid" jdbcType="INTEGER" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
</resultMap>
<sql id="Example_Where_Clause" >
<where >
<foreach collection="oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause" >
<where >
<foreach collection="example.oredCriteria" item="criteria" separator="or" >
<if test="criteria.valid" >
<trim prefix="(" suffix=")" prefixOverrides="and" >
<foreach collection="criteria.criteria" item="criterion" >
<choose >
<when test="criterion.noValue" >
and ${criterion.condition}
</when>
<when test="criterion.singleValue" >
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue" >
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue" >
and ${criterion.condition}
<foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List" >
uid, username, password
</sql>
<select id="selectByExample" resultMap="BaseResultMap" parameterType="pojo.UserExample" >
select
<if test="distinct" >
distinct
</if>
<include refid="Base_Column_List" />
from user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null" >
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where uid = #{uid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from user
where uid = #{uid,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="pojo.UserExample" >
delete from user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="pojo.User" >
insert into user (uid, username, password
)
values (#{uid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="pojo.User" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="uid != null" >
uid,
</if>
<if test="username != null" >
username,
</if>
<if test="password != null" >
password,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="uid != null" >
#{uid,jdbcType=INTEGER},
</if>
<if test="username != null" >
#{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
#{password,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="pojo.UserExample" resultType="java.lang.Integer" >
select count(*) from user
<if test="_parameter != null" >
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map" >
update user
<set >
<if test="record.uid != null" >
uid = #{record.uid,jdbcType=INTEGER},
</if>
<if test="record.username != null" >
username = #{record.username,jdbcType=VARCHAR},
</if>
<if test="record.password != null" >
password = #{record.password,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map" >
update user
set uid = #{record.uid,jdbcType=INTEGER},
username = #{record.username,jdbcType=VARCHAR},
password = #{record.password,jdbcType=VARCHAR}
<if test="_parameter != null" >
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="pojo.User" >
update user
<set >
<if test="username != null" >
username = #{username,jdbcType=VARCHAR},
</if>
<if test="password != null" >
password = #{password,jdbcType=VARCHAR},
</if>
</set>
where uid = #{uid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="pojo.User" >
update user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR}
where uid = #{uid,jdbcType=INTEGER}
</update>
<select id="selectAllUser" resultType="pojo.User">
select * from user
</select>
<delete id="deleteByIds" parameterType="int" >
delete from user where uid=#{uid}
</delete>
</mapper>