zoukankan      html  css  js  c++  java
  • springboot+mybatis+thymeleaf利用pagehelper实现多条件查询+分页

    我们都知道在开发过程中多条件查询+分页一直是一个老生常谈得问题,那么今天就来浅谈一下springboot是如何实现得,要想实现分页这个功能那我们就得在确定每页显示行数得情况下知道总页数等等一些变量,当然,这些变量都可以通过查询数据库得知,今天所要讲的是如何在查询一遍数据库得前提下得到这些变量以及我们多条件查询出来得变量集合,话不多话,请看代码

    前台页面代码

    <form  th:action="@{'/user/findUserByCondition'}">
                                
                                
             <input name="username" class="form-control" id="H_user" type="text"
                                        placeholder="请输入用户名..." />
                                
                                
            <input name="email" class="form-control" id="H_key" type="text"
                                        placeholder="请输入邮箱..." />
                                
                                
            <input type="submit" value="查询" />
                                
    </form>



    这里我用的是form表单得形式传数据,当然也可以用ajax得形式,如果用ajax得形式传数据得话那controller层就得变化了,因为需要接受你传过来得数据,感兴趣得朋友可以自己试一试

    <div>
                                    <div class="clearfix">
                                        <ul class="pagination m-b-5 pull-right">
                                            <li th:if="${users.hasPreviousPage}"><a
                                                th:href="@{'/user/touser?pageIndex='+${users.prePage}}"
                                                aria-label="Previous"> <i class="fa fa-angle-left"></i>&nbsp;上一页
                                            </a></li>
    
                                            <th:block th:each="nav : ${users.navigatepageNums}">
                                                <li th:class="${nav==users.pageNum}?'active':''"><a
                                                    th:href="@{'/user/touser?pageIndex='+${nav}}"
                                                    th:text="${nav}"></a></li>
                                            </th:block>
    
                                            <th:block th:if="${users.hasNextPage}">
                                                <li><a
                                                    th:href="@{'/user/touser?pageIndex='+${users.nextPage}}"
                                                    aria-label="Next"> 下一页&nbsp;<i class="fa fa-angle-right"></i>
                                                </a></li>
                                            </th:block>
    
                                            <li><span th:text="'共'+${users.pages}+'页'"></span></li>
                                        </ul>
                                    </div>
                                </div>

    这是分页得功能,简而言之,就是上一页下一页或者跳转到哪一页等等

    @RequestMapping("/touser")
        public String toUser(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize, HttpServletRequest request,
                HttpServletResponse response) {
    //        UserVoExample userVoExample = new UserVoExample();
    //        userVoExample.setOrderByClause("created desc");
    //        userVoExample.createCriteria();
    //        
    //        
    //        userVoExample.setUsername(Common.username);
    //        userVoExample.setEmail(Common.email);
            
            ConditionUser conditionUser=new ConditionUser();
            conditionUser.setUsername(Common.username);
            conditionUser.setEmail(Common.email);
            
            PageInfo<User> pageInfoUsers = userService.getUserWithPage1(conditionUser, pageIndex, pageSize);
            request.setAttribute("users", pageInfoUsers);
            return "user_page";
        }
    
        @RequestMapping("/findUserByCondition")
        public String findUserByCondition(User user,
                @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                @RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize, HttpServletRequest request) {
    //        UserVoExample userVoExample = new UserVoExample();
    //        userVoExample.setOrderByClause("created desc");
    //        userVoExample.createCriteria();
            
            
            
            
            
            Common.username = user.getUsername();
            Common.email = user.getEmail();
            
            String tusername=Common.username;
            String temail=Common.email;
            
            ConditionUser conditionUser=new ConditionUser();
            conditionUser.setUsername(tusername);
            conditionUser.setEmail(temail);
    //        userVoExample.setUsername(tusername);
    //        userVoExample.setEmail(temail);
            
            PageInfo<User> pageInfoUsers = userService.getUserWithPage1(conditionUser, pageIndex, pageSize);
            request.setAttribute("users", pageInfoUsers);
            return "user_page";
        }

    这是控制层得代码,里面得Common是提供得一个公共类,用于存放查询得多条件,代码如下:

    //公共类
    public class Common {
    
        public static String username=null;
        public static String email=null;
        
    }

    上述控制层还牵扯到一个PageInfo类,这个类中拥有上一页,下一页,总页数等等属性,是专门为我们分页量身打造得

    下面看我们得Service层

    @Override
        public PageInfo<User> getUserWithPage1(ConditionUser conditionUser, Integer pageIndex, Integer pageSize) {
            PageHelper.startPage(pageIndex, pageSize);
            List<User>users=userDao.findUserByCondition(conditionUser);
            
            return new PageInfo<>(users, 3);
        }

    上述代码得ConditionUser类是多条件得类以及分页,具体代码如下

    package com.gateway.entity;
    
    public class ConditionUser {
    
        
        private String username;
        private String email;
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public ConditionUser(String username, String email) {
            super();
            this.username = username;
            this.email = email;
        }
        public ConditionUser() {
            super();
            // TODO Auto-generated constructor stub
        }
        @Override
        public String toString() {
            return "ConditionUser [username=" + username + ", email=" + email + "]";
        }
        
    }

    剩下得就是dao层得接口以及mapper得xml文件了,还有查出来得集合在页面上显示,这里就不写代码了

  • 相关阅读:
    Oracle之内存结构(SGA、PGA)
    学员报名WDP培训之前必须阅读
    常见滤波方法
    C++ 为什么拷贝构造函数参数必须为引用?赋值构造函数参数也必须为引用吗?
    C++ explicit关键字详解
    A、B、C、D和E类IP地址
    BOOL和bool的区别
    互斥量 临界区 信号量 条件变量 效率对比
    Unhandled exception at 0x........ in XXXX.exe: 0xC0000005:错误
    链表的插入操作错误
  • 原文地址:https://www.cnblogs.com/1a2b/p/9032291.html
Copyright © 2011-2022 走看看