zoukankan      html  css  js  c++  java
  • mybatis配合pagehelper分页助手查询

    Maven:

    参考:

    springBoot2.x整合pagehelper5.1.2:https://blog.csdn.net/Carlson_Chis/article/details/85637489

    在pom.xml中配置依赖:

            <!-- mybatis的分页插件 -->
            <!--pageHelper基本依赖 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.1.2</version>
            </dependency>
            <!-- 我不加这两个依赖分页不会成功 -->
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
                <version>1.2.5</version>
            </dependency>
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>1.2.5</version>
            </dependency>

    service中使用:

    /**
         * 根据查询条件分页并排序查询品牌信息
         * @param key
         * @param page
         * @param rows
         * @param sortBy
         * @param desc
         * @return
         */
        public PageResult<Brand> queryBrandsByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
    
            // 初始化example对象
            Example example = new Example(Brand.class);
            Example.Criteria criteria = example.createCriteria();
    
            // 根据name模糊查询,或者根据首字母查询
            if (StringUtils.isNotBlank(key)) {
                criteria.andLike("name","%" + key + "%").orEqualTo("letter",key);
            }
    
            // 添加分页条件
            PageHelper.startPage(page,rows);
    
            // 添加排序条件
            if (StringUtils.isNotBlank(sortBy)) {
                // 通过判断desc是true还是false,确定升序还是降序
                example.setOrderByClause(sortBy + " " + (desc ? "desc" : "asc"));   // 相当于“id desc”
            }
    
            // 将查询到的结果保存在Brand类型的list中
            List<Brand> brands = this.brandMapper.selectByExample(example);
    
            // 包装成pageInfo
            PageInfo<Brand> pageInfo = new PageInfo<>(brands);
    
            // 包装成分页的结果集返回
            return new PageResult<>(pageInfo.getTotal(),pageInfo.getList());
    
        }
  • 相关阅读:
    进程和线程
    进程通信、同步与调度
    文件和文件系统
    【nexys3】【verilog】小设计——拆弹游戏
    Qt4开发环境搭建(Qt4.8.7+mingw4.8.2+Qt Creator4.2.0)
    GPL和LGPL
    【rpi】使用putty远程连接rpi(ssh)
    mysql 命令 小结
    安装mysql zip 安装包 Navicat连接
    python虚拟环境 virtualenv工具
  • 原文地址:https://www.cnblogs.com/flypig666/p/11732710.html
Copyright © 2011-2022 走看看