Java之使用spring+springMVC+myBatis实现用户管理系统(二)

用户搜索功能

在之前的搜索栏上增加了模糊查找的功能,对部分代码进行改写
之前的版本在
Java之使用spring+springMVC+myBatis实现用户管理系统

  1. controller
@RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(defaultValue = "1") int currentPage,String username){
        PageInfo<User> pageInfo=userService.findAll(currentPage,username);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }
  1. Dao
    在start和username前加上@Param进行注解
List<User> findAll(@Param("start") int start,@Param("username") String username);
  1. service
 @Override
    public PageInfo<User> findAll(int currentPage,String username) {
        PageInfo<User> pageInfo=new PageInfo<>();
        pageInfo.setSize(5);

        int tc=userDao.getTotalCount(username);
        pageInfo.setTatalCount(tc);
        int tp=(int)Math.ceil(tc/5.0);
        pageInfo.setTotalPage(tp);
        if (currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage>tp){
            pageInfo.setCurrentPage(tp);
        }else {
            pageInfo.setCurrentPage(currentPage);
        }

        int start=(pageInfo.getCurrentPage()-1)*5;
        List<User> userList=userDao.findAll(start,username);
        pageInfo.setList(userList);

        return pageInfo;
    }


  1. iUserService
PageInfo<User> findAll(int currentPage,String username);

  1. UserMapper.xml
    当输入不为空值的username进行搜索
    <select id="findAll" resultType="com.zhongruan.been.User">
       select * from tb_usr
       <if test="username!=null and username!=''">
            where username LIKE concat("%",#{username},"%")
       </if>
        limit #{start},5
   </select>
  1. 修改搜索的页数
    完成到这一步的时候会出现一个小bug,搜索完的总页数还是全部记录的页数在这里插入图片描述
    原因在在于在 service类中tp(totalpage)和tc(totalcount)获取的仍是总数,所以需要在UserMapper.xml中修改 gettotalCount的查询条件,和搜索条件保持一致
	<select id="getTotalCount" resultType="int">
        select count(*) from tb_usr
        <if test="username!=null and username!=''">
            where username LIKE concat("%",#{username},"%")
        </if>
    </select>
  1. 其他类的修改
    dao
    同理对username进行注解
    int getTotalCount(@Param("username")String username);
  1. 但是在点击上一页和下一页的时候,查询结果的数据就不会被正确显示(以查询1为例)
    在这里插入图片描述
    原因在于点击上一页和下一页的时候没有把username的值传过去,所以可以将值存在session里,可以在下次执行的时候取出来。
    默认给type一个值0,当type为1搜索的时候给session赋值,执行上一页下一页时候从session中取出username的值。
    jsp
<form action="${pageContext.request.contextPath}/user/findAll.dotype=1"method="post">
	<div class="col-md-4 data1">
		<input type="text" class="form-control" name="username"aceholder="username" value="">
	</div>
	<button type="submit" class="btn bg-maroon">搜索</button>
</form>

conrtoller

 @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(defaultValue = "1") int currentPage,String username,@RequestParam(defaultValue = "0")int type,HttpSession session){

        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;
    }

在这里插入图片描述

在这里插入图片描述

ajax

Ajax(Asynchronous JavaScript and XML),直译为“异步的JavaScript与XML技术”,是一种创建交互式网页应用的网页开发技术,用于创建快速动态网页,由杰西·詹姆士·贾瑞特所提出。与传统的Web应用相比,Ajax通过浏览器与服务器进行少量的数据交换就可以实现网页的异步更新,在不重新加载整个网页的情况下,即可对网页进行更新。

批量删除

使用ajax进行批量的删除。
jsp代码

<button type="button" class="btn btn-default" title="刷新" onclick="deleteAll()">
	<i class="fa fa-refresh"></i> 删除
</button>
function deleteAll() {
				var checkedNum=$("input[name='ids']:checked").length;//选中复选框的数量
				alert(checkedNum);
				if(checkedNum==0){
					alert("请至少选择一个进行删除!!!");
					return;
				}
				if(confirm("确认要删除这些用户吗?")){
					var userList=new Array();
					$("input[name='ids']:checked").each(
							function () {
								userList.push($(this).val())//塞入选中的值
							}
					);
					alert(userList);//打印出返回值
					$.ajax({
						type:"post",
						url: "${pageContext.request.contextPath}/user/deleteAll.do",
						data:{userList:userList.toString()},
						success:function () {
							alert("删除成功");
							location.reload();
						},
						error:function () {
							alert("删除失败");
						}
					});
				}
			}

controller

 @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";
    }

Dao

void deleteAll(@Param("ids") List<Integer> ids);

UserService

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

IUserService

void deleteAll(List<Integer> ids);

UserMapper.xml

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

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
这样就完成了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值