ssm+layui项目中实现搜索

1 篇文章 0 订阅

1、结果界面
在这里插入图片描述
2、
在这里插入图片描述

<div class="search">
        <div class="layui-inline">
            <input class="layui-input" name="keyword" id="keyword" autocomplete="off" placeholder="根据手机号搜索">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
    </div>

3、
在这里插入图片描述

$('.search .layui-btn').on('click', function () {
            searchCount();
            table.reload('test', {
                where: {
                    "keyword": $("#keyword").val()
                }
            });
        })

4、
在这里插入图片描述

function searchCount() {
            $.ajax({
                url: "http://localhost:8080/maven02/user/searchCount",
                type: "get",
                data: {"flag": "student", "keyword": $("#keyword").val()},
                dataType: "json",
                success: function (value) {
                    console.log(value)
                    pagecount = value.amount
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(typeof (errorThrown))
                }
            })
        }

5、mapper

    <select id="searchByPhone" resultMap="BaseResultMap">
        select * from user
        <where>
            <if test="keyword!=null and keyword!=''">
                and phone like '%${keyword}%'
            </if>
            <if test="flag!=null and flag!=''">
                and flag=#{flag}
            </if>
        </where>
        limit #{offset}, #{limit}
    </select>

    <select id="searchCount" resultType="java.lang.Integer">
        select count(*) from user
        <where>
            <if test="keyword!=null and keyword!=''">
                and phone like '%${keyword}%'
            </if>
            <if test="flag!=null and flag!=''">
                and flag=#{flag}
            </if>
        </where>
    </select>

6、dao

  List<User> searchByPhone(@Param("keyword") Integer keyword,@Param("flag") String flag,
                             @Param("offset") int offset, @Param("limit") int limit);

  Integer searchCount(@Param("keyword") Integer keyword,@Param("flag") String flag);

7、service

  List<User> searchByPhone(Integer keyword, String flag, int offset, int limit);

  Integer searchCount(Integer keyword, String flag);

8、serviceImpl

 public List<User> searchByPhone(Integer keyword, String flag, int offset, int limit) {
        return this.userDao.searchByPhone(keyword, flag, offset, limit);
    }

 public Integer searchCount(Integer keyword, String flag) {
        return this.userDao.searchCount(keyword, flag);
    }

9、controller

 @ResponseBody
 @RequestMapping(value = "/searchCount", method = RequestMethod.GET)
    public String searchCount(HttpServletRequest request) {
        String key = request.getParameter("keyword");
        Integer keyword = Integer.valueOf(key);
        String flag = request.getParameter("flag");
        Integer countInteger = userService.searchCount(keyword, flag);
        String count = String.valueOf(countInteger);
        String data = "{\"amount\":" + count + "}";
        return data;
    }

//搜索与分页查询展示用的是同一个
@ResponseBody
@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public String selectAll(@RequestParam(value = "keyword", required = false) String keyword,
                            HttpServletRequest request) throws Exception {
        String flag = request.getParameter("flag");
        String offsetString = request.getParameter("page");
        String limitString = request.getParameter("limit");
        Integer limit = Integer.valueOf(limitString);
        Integer offset = (Integer.valueOf(offsetString) - 1) * limit;
        if (keyword == null) {
            List<User> users = userService.queryAllByLimit(flag, offset, limit);
            String[] columns = {"id", "username", "password", "phoneNumber", "classnum", "flag"};
            String data = ObjtoLayJson.ListtoJson(users, columns);
            return data;
        } else {
            Integer key = Integer.valueOf(keyword);
            List<User> users = userService.searchByPhone(key, flag, offset, limit);
            String[] columns = {"id", "username", "password", "phoneNumber", "classnum", "flag"};
            String data = ObjtoLayJson.ListtoJson(users, columns);
            return data;
        }
    }
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值