Skip to content

分页

概述

通过注解实现分页功能,只需要加@Paginate注解即可对其进行分页,而不需要再代码中再去做分页操作,该分页注解的实现基础是PageHelper。

注意事项

只有紧跟在@Paginate后的第一个Mybatis的查询(Select)方法会被分页。

操作步骤

  1. 首先,你需要检查当前业务模块是否依赖pangea-common-mybatis包,如没有则依赖pangea-common-mybatis,如下代码:

    xml
    <dependency>
        <groupId>com.hisense.pangea</groupId>
        <artifactId>pangea-common-mybatis</artifactId>
        <version>${pangea.version}</version>
    </dependency>
  2. 在需要对结果进行分页处理的操作上加上@Paginate注解,例如:

    a. 在接口加上@Paginate

    java
    /**
     * 查询数据库列表
     */
    @GetMapping("/dbCreateTable")
    @Paginate
    public List<CreateTable> dataList(CreateTable createTable) {
        return createTableService.selectDbTableList(createTable);
    }

    b. 在service方法加上@Paginate

    java
    /**
         * 查询据库列表
         *
         * @param genTable 业务信息
         * @return 数据库表集合
         */
        @Override
        @Paginate
        public List<GenTable> selectDbTableList(GenTable genTable) {
            List<GenTable> genTableList = genTableMapper.selectDbTableList(genTable);
            DynamicDataSourceContextHolder.clearDataSourceType();
            return genTableList;
        }
  3. 分页参数介绍

参数名称参数描述参数值
pageNum当前选中的页面可选参数,默认是1
pageSize每页显示的条目数可选参数,默认是20
orderByColumn排序字段可选参数
isAsc排序顺序可选参数,默认是asc
  1. 分页数据结果展示

    a. pageNum=1&pageSize=3&orderByColumn=name&isAsc=asc,如下

    java
    {
        "code": "0",
        "data": {
            "pageNum": 1,
            "rows": [
                {
                    "MAIL": "123555@qq.com",
                    "PAGEHELPER_ROW_ID": 1,
                    "ID": 481,
                    "NAME": "1",
                    "AGE": 10
                },
                {
                    "MAIL": "123@qq.com",
                    "PAGEHELPER_ROW_ID": 2,
                    "ID": 479,
                    "NAME": "1",
                    "AGE": 10
                },
                {
                    "MAIL": "123555@qq.com",
                    "PAGEHELPER_ROW_ID": 3,
                    "ID": 488,
                    "NAME": "1,2",
                    "AGE": 10
                }
            ],
            "total": "10"
        },
        "msg": "操作成功",
        "ts": "1603705060306"
    }

    b. pageNum=1&pageSize=5&orderByColumn=name&isAsc=desc,结果如图

    java
    {
    "code": "0",
    "data": {
        "pageNum": 1,
        "rows": [
            {
                "MAIL": "3@hisense.com",
                "PAGEHELPER_ROW_ID": 1,
                "ID": 490,
                "NAME": "3,2",
                "AGE": 30
            },
            {
                "MAIL": "1@163.com",
                "PAGEHELPER_ROW_ID": 2,
                "ID": 489,
                "NAME": "3,1",
                "AGE": 20
            },
            {
                "MAIL": "hm@163.com",
                "PAGEHELPER_ROW_ID": 3,
                "ID": 483,
                "NAME": "3",
                "AGE": 16
            },
            {
                "MAIL": "2@123.com",
                "PAGEHELPER_ROW_ID": 4,
                "ID": 491,
                "NAME": "2",
                "AGE": 40
            },
            {
                "MAIL": "666@qq.com",
                "PAGEHELPER_ROW_ID": 5,
                "ID": 480,
                "NAME": "2",
                "AGE": 22
            }
        ],
        "total": "10"
    },
    "msg": "操作成功",
    "ts": "1603704941430"
    }

注意事项

  • 分页只对数据库进行的第一次操作的结果生效。
  • 若在service工具类中,A方法调用B方法,在B上添加@Paginate,如下,则,分页失效。
java
    public List listMyApp(String key) {
        String deptName = deptService.getDeptNameByKey(key);
        return selectAppList(deptName);
    }
    
    @Paginate
    public List selectAppList(String deptName) {
        return appService.selectAppList(deptName);
    }

建议在controller层拆分,controller调不同的service方法。

java
    @RestController
    public class TestController extends BaseController {
        private final TestService testService;
    
        public TestController(TestService testService) {
            this.testService = testService;
        }
    
        /**
         * 查询列表
         */
        @GetMapping("/list")
        public List genList(String key) {
            String deptName = testService.listMyApp(key);
            return testService.selectAppList(deptName);
        }
    }
    
    @Service
    public class TestService {
        private final TestMapper testMapper;
            
        public TestService(TestMapper testMapper) {
            this.testMapper = testMapper;
        }
        
        public String listMyApp(String key) {
            return deptName = testMapper.getDeptNameByKey(key);
        }
            
        @Paginate
        public List selectAppList(String deptName) {
            return testMapper.selectAppList(deptName);
        }
    }