我的中软国际实习Day7
基于ssm框架的模糊查询、批量删除
UserController
@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") int currentPage,String username,HttpSession session,@RequestParam(defaultValue = "0") int type){
if(type==1){
session.setAttribute("searchname",username);
}else{
username=(String) session.getAttribute("searchname");
}
PageInfo<User> pageInfo=userService.findAll(currentPage,username);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("pageInfo",pageInfo);
modelAndView.setViewName("user-list");
return modelAndView;
}
@RequestMapping("deleteAll.do")
public String deleteAll(String userList){
String[] strs=userList.split(",");
List<Integer> ids=new ArrayList<>();
for(String s:strs){
ids.add(Integer.parseInt(s));
}
userService.deleteAll(ids);
return "redirect:findAll.do";
}
UserMapper.xml
<select id="findUserByUserName" parameterType="String" resultType="com.zhongruan.bean.User">
select *from tb_user where username=#{username}
</select>
<select id="findAll" resultType="com.zhongruan.bean.User">
select *from tb_user
<if test="username!=null and username!=''">
WHERE username LIKE concat("%",#{username},"%")
</if>
limit #{start},5
</select>
<delete id="deleteById" parameterType="int">
delete from tb_user where id=#{id}
</delete>
<insert id="add" parameterType="user">
insert into tb_user(username,password) value (#{username},#{password})
</insert>
<select id="selectById" parameterType="int" resultType="user">
select * from tb_user where id=#{id}
</select>
<update id="update" parameterType="user">
update tb_user set username=#{username},password=#{password} where id=#{id}
</update>
<select id="getTotalCount" resultType="int">
select count(*) from tb_user
<if test="username!=null and username!=''">
WHERE username LIKE concat("%",#{username},"%")
</if>
</select>
<delete id="deleteAll" parameterType="list">
delete from tb_user where id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</delete>
user-list
<button type="button" class="btn btn-default" title="删除" onclick="deleteAll()">
<i class="fa fa-refresh"></i> 删除
</button>
</div>
</div>
</div>
<form action="${pageContext.request.contextPath}/user/findAll.do?type=1"
method="post">
<div class="col-md-4 data1">
<input type="text" class="form-control" name="username"
placeholder="username" value="">
</div>
<button type="submit" class="btn bg-maroon">搜索</button>
</form>
<!--工具栏/-->
<!--数据列表-->
<table id="dataList"
class="table table-bordered table-striped table-hover dataTable">
<thead>
<tr>
<th class="" style="padding-right: 0px"><input
id="selall" type="checkbox" class="icheckbox_square-blue">
</th>
<th class="sorting_asc">ID</th>
<th class="sorting_desc">用户名</th>
<th class="sorting_asc sorting_asc_disabled">密码</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${pageInfo.list}" var="user">
<tr>
<td><input id="ids" name="ids" type="checkbox" value="${user.id}"></td>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.password}</td>
<td class="text-center">
<a href="${pageContext.request.contextPath}/user/toUpdate.do?id=${user.id}" class="btn bg-olive btn-xs">更新</a>
<a href="${pageContext.request.contextPath}/user/deleteById.do?id=${user.id}" class="btn bg-olive btn-xs">删除</a>
<a href="#" class="btn bg-olive btn-xs">添加角色</a>
</td>
</tr>
</c:forEach>
</tbody>
<!--
<tfoot>
<tr>
<th>Rendering engine</th>
<th>Browser</th>
<th>Platform(s)</th>
<th>Engine version</th>
<th>CSS grade</th>
</tr>
</tfoot>-->
</table>
<!--数据列表/-->
</div>
<!-- 数据表格 /-->
</div>