• Spring Boot----PageHelper


    不使用PageHelper

      需要进行两次查询,一次查询总数,用来计算前端分页的页码数量,一次查询数据;

    public PageResult query(IplogQueryObject qo) {
    		int totalCount = this.iplogMapper.queryForCount(qo);
    		if (totalCount > 0) {
    			List<Iplog> list = this.iplogMapper.query(qo);
    			PageResult pageResult = new PageResult(list, totalCount, qo.getCurrentPage(),
    					qo.getPageSize());
    			return pageResult;
    		}
    		return PageResult.empty(qo.getPageSize());
    	}
    

      在dao需要写limit

    <select id="query" resultMap="BaseResultMap">
        SELECT <include refid="base_column" />
        FROM iplog
        <include refid="base_where" />
        ORDER BY loginTime DESC
        LIMIT #{start},#{pageSize}
      </select>
    

      

    使用PageHelper

    依赖

    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper‐spring‐boot‐starter</artifactId>         
        <version>1.2.4</version>
    </dependency>
     
    在application.yml中配置pageHelper操作的数据库类型

    PageHelper测试

      注意:PageHelper只对第一个查询有效

      定义接口

    @Mapper
    public interface CourseMapper {
        Page<CourseInfo> findCourseListPage(CourseListRequest courseListRequest)
    };
      定义mapper.xml映射文件(不需要加limit)
    <select id="findCourseListPage" resultType="com.xuecheng.framework.domain.course.ext.CourseInfo" parameterType="com.xuecheng.framework.domain.course.request.CourseListRequest">
      SELECT * FROM course_base
    </select>
    

      测试

        //测试分页
        @Test
        public void testPageHelper() {
            PageHelper.startPage(1, 10);//查询第一页,每页显示10条记录
            CourseListRequest courseListRequest = new CourseListRequest();
            Page<CourseInfo> courseListPage = courseMapper.findCourseListPage(courseListRequest);
            List<CourseInfo> result = courseListPage.getResult();
          Long l = courseListPage.getTotal();
            System.out.println(courseListPage);
        }
    

      使用二

            PageHelper.startPage(pageNumber, pageSize);
            classCountStatistics = schoolClassMapper.getMajorCountStatistics(startYear, schoolTerm, classYear, null, schoolId, 0, id);
            PageInfo<ClassStuSelectionCountInfoVo> classStuSelectionCountInfoVoPageInfo = new PageInfo<>(classCountStatistics);
    

      

  • 相关阅读:
    nodeJs学习-10 模板引擎 ejs语法案例
    nodeJs学习-09 模板引擎 jade、ejs
    nodeJs学习-08 cookie、session
    nodeJs学习-07 express、body-parser;链式操作next
    RedHat6.5-Linux安装telnet服务
    druid数据源配置
    rpm安装MySQL
    黎活明给程序员的忠告
    为什么要使用JS模板引擎
    Angularjs调用公共方法与共享数据
  • 原文地址:https://www.cnblogs.com/yanxiaoge/p/11851581.html
Copyright © 2020-2023  润新知