首页 > 其他分享 >复杂条件查询功能

复杂条件查询功能

时间:2022-08-18 10:11:06浏览次数:54  
标签:功能 rows String 复杂 value 查询 pb currentPage condition

复杂条件查询功能分析

 

 

 

 

 

 

 

总记录数统计的代码实现

UserDao接口:

    /**
     * 查询总记录数
     * @return
     * @param condition
     */
    int FindTotalCount(Map<String, String[]> condition);

    /**
     * 分页查询每页记录
     * @param start
     * @param rows
     * @param condition
     * @return
     */
    List<User> fingByPage(int start, int rows, Map<String, String[]> condition);

UserDaoImpl实现类:

 

@Override
    public int FindTotalCount(Map<String, String[]> condition) {
        //定义模板初始化sql
        String sql = "select count(*) from user where 1 = 1 ";
        StringBuilder sb = new StringBuilder(sql);
        //遍历map
        Set<String> keySet = condition.keySet();
        //定义参数的集合
        List<Object> params = new ArrayList<>();
        for (String key : keySet) {

            //排除分页条件参数
            if("currentPage".equals(key) || "rows".equals(key)){
                continue;
            }

            //获取value
            String value = condition.get(key)[0];
            //判断value是否有值
            if(value != null && !"".equals(value)){
                //有值
                sb.append(" and "+key+" like ? ");
                params.add("%"+value+"%");//? 条件的值
            }
        }
        System.out.println(sb.toString());
        System.out.println(params);
        return template.queryForObject(sb.toString(),Integer.class,params.toArray());
    }

 

UserService接口:

    /**
     * 分页条件查询
     * @param currentPage
     * @param rows
     * @param condition
     * @return
     */
    PageBean<User> findUserByPage(String currentPage, String rows, Map<String, String[]> condition);

UserServiceImpl实现类:

@Override
    public PageBean<User> findUserByPage(String _currentPage, String _rows, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        //判读最小页码
        if (currentPage < 1){
            currentPage = 1;
        }

        //1、创建空的PageBean对象
        PageBean<User> pb = new PageBean<>();

        //3、调用dao查询总记录数
        int totalCount = dao.FindTotalCount(condition);
        pb.setTotalCount(totalCount);

        //5、计算总页码
        int totalPage = (totalCount%rows)==0 ? totalCount/rows : (totalCount/rows)+1;

        //判断最大页码
        if (currentPage >totalPage){
            currentPage = totalPage;
        }

        //2、设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //4、调用dao查询list集合
        //计算开始的记录索引
        int start = (currentPage-1)*rows;
        List<User> list = dao.fingByPage(start,rows,condition);
        pb.setList(list);


        pb.setTotalPage(totalPage);

        return pb;
    }

FindUserByPageServlet类:

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        //1、获取参数
        String currentPage = request.getParameter("currentPage");//当前页码
        String rows = request.getParameter("rows");//每页显示条数

        if (currentPage == null || "".equals(currentPage)){
            currentPage = "1";
        }
        if (rows == null || "".equals(rows)){
            rows = "5";
        }

        //获取条件查询参数
        Map<String, String[]> condition = request.getParameterMap();


        //2、调用servic查询
        UserService service = new UserServiceImpl();
        PageBean<User> pb = service.findUserByPage(currentPage,rows,condition);
        //3、将PageBean存入request
        request.setAttribute("pb",pb);
        //4、转发到list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

list.jsp:

<form class="form-inline" action="${pageContext.request.contextPath}/findUserByPageServlet" method="post">
            <div class="form-group">
                <label for="exampleInputName2">姓名</label>
                <input type="text" class="form-control" id="exampleInputName2" name="name">
            </div>
            <div class="form-group">
                <label for="exampleInputName3">籍贯</label>
                <input type="text" class="form-control" id="exampleInputName3" name="address">
            </div>

            <div class="form-group">
                <label for="exampleInputEmail2">邮箱</label>
                <input type="email" class="form-control" id="exampleInputEmail2" name="email">
            </div>
            <button type="submit" class="btn btn-default">查询</button>
        </form>

 

 

 

 

 

 

 

 

每页数据条件查询的代码实现

UserDaoImpl实现类:

    @Override
    public List<User> fingByPage(int start, int rows, Map<String, String[]> condition) {
        String sql = "select * from user where 1 = 1";
        StringBuilder sb = new StringBuilder(sql);
        //遍历map
        Set<String> keySet = condition.keySet();
        //定义参数的集合
        List<Object> params = new ArrayList<>();
        for (String key : keySet) {

            //排除分页条件参数
            if("currentPage".equals(key) || "rows".equals(key)){
                continue;
            }

            //获取value
            String value = condition.get(key)[0];
            //判断value是否有值
            if(value != null && !"".equals(value)){
                //有值
                sb.append(" and "+key+" like ? ");
                params.add("%"+value+"%");//? 条件的值
            }
        }
        //添加分页查询
        sb.append(" limit ?,? ");
        //添加分页查询参数值
        params.add(start);
        params.add(rows);
        return template.query(sb.toString(),new BeanPropertyRowMapper<User>(User.class),params.toArray());
    }

list.jsp:

<body>
<div class="container">
    <h3 style="text-align: center">用户信息列表</h3>
    <div style="float: left;">

        <form class="form-inline" action="${pageContext.request.contextPath}/findUserByPageServlet" method="post">
            <div class="form-group">
                <label for="exampleInputName2">姓名</label>
                <input type="text" class="form-control" id="exampleInputName2" name="name" value="${condition.name[0]}">
            </div>
            <div class="form-group">
                <label for="exampleInputName3">籍贯</label>
                <input type="text" class="form-control" id="exampleInputName3" name="address" value="${condition.address[0]}">
            </div>

            <div class="form-group">
                <label for="exampleInputEmail2">邮箱</label>
                <input type="text" class="form-control" id="exampleInputEmail2" name="email" value="${condition.email[0]}">
            </div>
            <button type="submit" class="btn btn-default">查询</button>
        </form>

    </div>

    <div style="float:right;margin: 5px;">
        <a class="btn btn-primary" href="${pageContext.request.contextPath}/add.jsp">添加联系人</a>
        <a class="btn btn-primary" href="javascript:void(0);" id="delSelected">删除选中</a>
    </div>
    <form id="form" action="${pageContext.request.contextPath}/delSelectedServlet" method="post">
        <table border="1" class="table table-bordered table-hover">
        <tr class="success">
            <th><input type="checkbox" id="firstCb"></th>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
            <th>籍贯</th>
            <th>QQ</th>
            <th>邮箱</th>
            <th>操作</th>
        </tr>

        <c:forEach items="${pb.list}" var="user" varStatus="s">
            <tr>
                <th><input type="checkbox" name="uid" value="${user.id}"></th>
                <td>${s.count}</td>
                <td>${user.name}</td>
                <td>${user.gender}</td>
                <td>${user.age}</td>
                <td>${user.address}</td>
                <td>${user.qq}</td>
                <td>${user.email}</td>
                <td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findUserServlet?id=${user.id}">修改</a>&nbsp;
                    <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id});">删除</a>
                </td>
            </tr>
        </c:forEach>

    </table>
    </form>
    <div>
        <nav aria-label="Page navigation">
            <ul class="pagination">
                <c:if test="${pb.currentPage == 1}">
                    <li class="disabled">
                </c:if>
                <c:if test="${pb.currentPage != 1}">
                    <li>
                </c:if>

                    <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage-1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>

                <c:forEach begin="1" end="${pb.totalPage}" var="i">
                    <c:if test="${pb.currentPage == i}">
                        <li class="active"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li>
                    </c:if>

                    <c:if test="${pb.currentPage != i}">
                        <li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li>
                    </c:if>
                </c:forEach>
                <li>
                    <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage+1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
                <span style="font-size: 25px;margin-left: 5px;">
                    共${pb.totalCount}条记录,共${pb.totalPage}页
                </span>

            </ul>
        </nav>


    </div>
</div>
</body>

 

标签:功能,rows,String,复杂,value,查询,pb,currentPage,condition
From: https://www.cnblogs.com/xjw12345/p/16597646.html

相关文章

  • 自动化脚本如何切换环境?Pytest这些功能你必须要掌握
     测试人员每天都跟不同的环境打交道,比如线上环境,测试环境,预上线环境等等,那么作为自动化测试人员写的代码,我们也要具备能自由切换环境的能力,那么今天小编就给大家聊一下,......
  • SAP BW怎么查询传输请求号内容与所有请求号存放的表
    一:打开gui,输入事务码-SE11     在数据库表输入:E070(这个表就是存放所有传输请求号的)---点击显示  二:进入后点击下图红框处,查看表的数据 三:输入请求号,点......
  • 部分功能实现笔记
    部分功能实现笔记以下内容均来自武沛齐老师的课程笔记Fields的选择classMyForm(ModelForm):xx=form.CharField*("...")#新加不在数据库中的字段classMe......
  • 如何判断一个系统中的哪些功能会被使用?
    可以根据业务部门人员配比、业务人员对业务投入的日常时间比例情况,来判断一系列功能是否会被客户用到。比如两个客户都有相同的业务部门,一个100人与一个10人,那么他们对系......
  • 讲一讲加密数据如何进行模糊查询
    在上一篇讲一讲数据安全,如何有效预防脱库中我们提到了加密后的数据对模糊查询不是很友好,本篇就针对加密数据模糊查询这个问题来展开讲一讲实现的思路。为了数据安全我们......
  • mysql/表sql语句补充/关键字查询
    操作表的SQL语句补充alter1.修改表名 altertable表名rename新表名;2.新增字段 altertableadd字段名字段类型(数字)约束条件3.新增指定字段排在第一位 ......
  • MySQL查询关键数据方法
    MySQL查询关键数据方法操作表的SQL语句补充1.修改表名 altertable表名reame新表名;2.新增字段名 altertable表名add字段名字段类型(数字)约束条件;#默认队尾......
  • 20220817 第一组 于芮 mysql数据库查询(第三十四天)
     小白成长记——第三十四天   今天主要学习了mysql数据库的查询语句,对于一个合格的程序猿来说,掌握数据库的查询语句是非常重要的,所以今天不仅学习了理论知识,还作了......
  • MySQL之表查询关键字
    操作表的SQL语句补充修改表名创建一张表:createtableaa(idintprimarykeyauto_increment);altertable表名rename新表名;altertableaarenameabb;新增字......
  • 2022-8-17数据库查询联系
    DQL查询语言子查询按照结果集的行列数不同,子查询可以分为以下几类:标量子查询:结果集只有一行一列(单行子查询)列子查询:结果集有一列多行行子查询:结果集有一行多列......