• 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文件了,还有查出来得集合在页面上显示,这里就不写代码了

  • 相关阅读:
    scrapy爬虫框架学习
    python:面向对象—— __slots__来限制实例的属性命名范围
    列表list 的常用操作
    python replace()用法
    python 字符串 空字符串 len()
    python 字符串 编码问题
    python中语句、函数、类、模块、包之间的关系
    matplotlib 绘制正玄余玄曲线
    Es6/CommonJs/AMD模块系统的差异以及用法
    BOM和DOM的区别
  • 原文地址:https://www.cnblogs.com/1a2b/p/9032291.html
Copyright © 2020-2023  润新知