zoukankan      html  css  js  c++  java
  • 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);
    

      

  • 相关阅读:
    只使用代码创建WPF程序
    C#第一课
    VMware虚拟机的网络设置
    mac系统下配置Eclipse Java/C++ 开发环境
    【读书笔记】【物联网】《物物连起大世界物联网的应用与前景》
    移动硬盘无法格式化
    Win7下chm文件打不开的解决办法[转]
    MapInfo数据到ARCGIS数据Shapefile的转换
    mapx不具开发功能
    [读书笔记]熟悉SQLPLUS Oracle数据库环境
  • 原文地址:https://www.cnblogs.com/yanxiaoge/p/11851581.html
Copyright © 2011-2022 走看看