ajax+mybatis实现分页查询+模糊查询+多条件查询

ajax+mybatis实现分页查询+模糊查询+多条件查询

实现的效果

ajax+mybatis实现分页查询+模糊查询+多条件查询_第1张图片

前台js

<script>
function sreach(page) {
    var seach_title=$("#seach_title").val();
    var seach_lname=$("#seach_lname").val();
    $.ajax({
        url:"article/mh",
        data:{"page":page,"seach_title":seach_title,"seach_lname":seach_lname},
        type:"post",
        dataType:"json",
        success:function (data) {
            $("#sumCount").html(data.count);//总条数打印到页面上
            var h="";
            for (var i = 0; i <data.list.length ; i++) {
                h+="\n" +
                    "\n" +
                    ""+data.list[i].id+"\n" +
                    ""+data.list[i].uname+"\n" +
                    ""+data.list[i].title+"\n" +
                    ""+data.list[i].lname+"\n" +
                    ""+data.list[i].content+"\n" +
                    ""+data.list[i].heat+"\n" +
                    ""+data.list[i].createtime+"\n" +
                    "\n" +
                    " +
                    "οnclick=\"member_password('修改用户','article-update.html','1001','600','400',sendOne('"+data.list[i].id+"'))\" href=\"javascript:;\"\n" +
                    "                            title=\"修改用户\">\n" +
                    "                            \n" +
                    "                        \n" +
                    "                        +data.list[i].id+"')\" style=\"text-decoration:none\">\n" +
                    "                            \n" +
                    "                        \n" +
                    "                    \n" +
                    "                ";
            }
                    $("#selectall").html(h);

                    //动态非配分页
                    var s=data.page-1>0?data.page-1:1;
                    var x=data.page+1>data.sumpage?data.sumpage:data.page+1;
                    var f=  '首页'+
                        '+s+')">上一页'+
                        '+x+')">下一页'+
                        '+data.sumpage+')">末页'+
                        '当前页/总页:'+data.page+'/'+data.sumpage+''+
                        ''+
                        '';
                    $("#fy").html(f);
                },
                error:function (data) {
                    alert("error");
                }
            })
        }
    
script>

controller层

@RequestMapping("/mh")
    @ResponseBody
    public String mh(HttpServletRequest request){ //模糊查询+分页+关联
        String page=request.getParameter("page");
        if(page==null || "".equals(page)){
            page="1";
        }
        String seach_title=request.getParameter("seach_title");//搜索标题
        String seach_lname=request.getParameter("seach_lname");//搜索标签
        User user = (User) request.getSession().getAttribute("user");
        //页面传来的值 存在condition对象中
        condition condition=new condition();
        condition.setPage(Integer.parseInt(page));
        condition.setSeach_title(seach_title);
        condition.setSeach_lname(seach_lname);
        condition.setUid(user.getId());

        List<Map<String,Object>> list = articleService.mh(condition);//模糊查询+分页+关联
        //模糊查询总条数
        List<Map<String, Object>> count2 = articleService.count2(condition);
        int sum = count2.size();//总条数
        int sumpage=sum%4>0?(sum/4+1):(sum/4);//页面的总页数
        //将从后台查询到的值封装在dgb对象中
        datagridebean dgb=new datagridebean();
        dgb.setList(list);
        dgb.setCount(sum);
        dgb.setPage(Integer.parseInt(page));//当前页
        dgb.setSumpage(sumpage);
        //将数据转为json格式 给页面
        String json = JSON.toJSONStringWithDateFormat(dgb, "yyyy-MM-dd");
        return json;
    }

service的impl

  @Override
    public List<Map<String, Object>> mh(condition condition) {
        int page = condition.getPage();
        int f=(page-1)*4;
        condition.setPage(f);
        return adminlableMapper.mh(condition);
    }

mapper.xml

 <select id="mh" parameterType="com.example.admin.entity.condition" resultType="Map" >
        select art.*,u.uname,la.lname
        from article art,user u,lable la
        where art.uid=u.id and art.lid=la.id and title like '%${seach_title}%'
        and lname like '%${seach_lname}%' and u.id=#{uid}
        ORDER BY id limit #{page},4
    select>

    <select id="count2" parameterType="com.example.admin.entity.condition" resultType="Map" >
        select art.*,u.uname,la.lname
        from article art,user u,lable la
        where art.uid=u.id and art.lid=la.id and title like '%${seach_title}%'
        and lname like '%${seach_lname}%' and u.id=#{uid}
    select>

你可能感兴趣的:(java,spring,boot,ajax)