Java实现分页

一、limit关键字

service层

@Service

@Transactional

public class ImplStudentService implements StudentService {

@Resource

private  StudentDao  studentDao;

    @Override

    public List  selectAllStudent(String province, Integer offset, Integer limit) {

        return studentDao.selectAll(province,offset,limit);

    }

}

sql语句

select * from student where province = #{province} limit #{offset},#{limit}

二、hibernate分页

service层

@Override

  public List getStudents(Integer  pageNo,Integer  pageSize) throws Exception {

  // 分页数据

  int[] startIdAndCount = new int[2];

  startIdAndCount[0] = pageNo * pageSize;

  startIdAndCount[1] = pageSize;

  return studentDao.selectStudentsByPage(startIdAndCount);

}

dao层

@Override

public List findByHqlPage(int[] startIdAndCount) throws Exception {

String hql = "...";

try {

Query query = getSession().createQuery(hql);

// 设置分页

if (startIdAndCount != null && startIdAndCount.length > 0) {

int rowStartIdx = Math.max(0, startIdAndCount[0]);

if (rowStartIdx > 0) {

query.setFirstResult(rowStartIdx);// 设置开始取值的索引

}

if (startIdAndCount.length > 1) {

int rowCount = Math.max(0, startIdAndCount[1]);

if (rowCount > 0) {

query.setMaxResults(rowCount);// 设置结束取值的索引

}

}

}

return query.list();

} catch (RuntimeException re) {

log.error("分页查询失败!", re);

throw re;

}

}

三、截取List查询结果分页(简单粗暴)

...

List students = studentlDao.getAllStudents();

int count = 0;

if(studentEnrolls != null && studentEnrolls.size() > 0) {

count = studentEnrolls.size();

int fromIndex = pageNo * pageSize;

int toIndex = (pageNo + 1) * pageSize;

if(toIndex > count) {

toIndex = count;

}

List pageList = studentEnrolls.subList(fromIndex, toIndex);

...

————————————————

四、mybatis框架pageHelper插件分页

Spring整合:

导入pom.xml

      com.github.pagehelper

      pagehelper

      5.1.2

配置项目配置文件(我在spring和mybatis整合的配置文件中配置的,如果在mybatis核心配置文件中配置,百度一下)

       

       

       

       

           

                classpath*:com/yyz/mapper/*Mapper.xml

           

       

       

       

           

               

                   

                       

                       

                   

                            helperDialect=mysql

                   

                            reasonable=true

                   

                            params=count=countSql

                   

                            supportMethodsArguments=true

                   

                            autoRuntimeDialect=true

                       

                   

               

           

       

       

       

————————————————

SpringBoot整合:

    com.github.pagehelper

    pagehelper-spring-boot-starter

    最新版本

配置项目application.yml文件

#bybatis分页插件配置

pagehelper

        helper-dialect: mysql #数据库 

        reasonable: true 

        support-methods-arguments: true 

        params: count=countSql

标题分页插件参数:分页插件提供了多个可选参数,这些参数使用时,按照上面配置方式中的示例配置即可。分页插件可选参数如下:

dialect:默认情况下会使用 PageHelper 方式进行分页,如果想要实现自己的分页逻辑,可以实现Dialect(com.github.pagehelper.Dialect) 接口,然后配置该属性为实现类的全限定名称。 使用自定义dialect 实现时,下面的参数没有任何作用。

helperDialect:分页插件会自动检测当前的数据库链接,自动选择合适的分页方式。oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby特别注意:使用 SqlServer2012 数据库时,需要手动指定为 sqlserver2012,否则会使用 SqlServer2005的方式进行分页。

offsetAsPageNum:默认值为 false,该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为 true时,会将 RowBounds 中的 offset 参数当成 pageNum 使用,可以用页码和页面大小两个参数进行分页。

rowBoundsWithCount:默认值为false,该参数对使用 RowBounds 作为分页参数时有效。当该参数设置为true时,使用 RowBounds 分页会进行 count 查询。

pageSizeZero:默认值为 false,当该参数设置为 true 时,如果 pageSize=0 或者RowBounds.limit = 0 就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是 Page 类型)。

reasonable:分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0时会查询第一页,pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。

params:为了支持startPage(Object params)方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值,可以配置 pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值,默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero。

supportMethodsArguments:支持通过 Mapper接口参数来传递分页参数,默认值false,分页插件会从查询方法的参数值中,自动根据上面 params配置的字段中取值,查找到合适的值时就会自动分页。

aggregateFunctions:默认为所有常见数据库的聚合函数,允许手动添加聚合函数(影响行数),所有以聚合函数开头的函数,在进行count 转换时,会套一层。其他函数和列会被替换为 count(0),其中count列可以自己配置。

重要提示:

当 offsetAsPageNum=false 的时候,由于 PageNum 问题,RowBounds查询的时候 reasonable 会强制为 false。使用 PageHelper.startPage 方法不受影响。

service层

@Override

public ResponseResult selectAllStudent(Integer pageNum, Integer pageSize) {

    Map map = new HashMap<>();

    PageHelper.startPage(pageNum,pageSize);

    List  students = studentMapper.selectAllStudents();

    PageInfo pageInfo = new PageInfo(students);

    long total = pageInfo.getTotal();

    map.put("result",pageInfo);

    map.put("count",total);

    return ResponseResultUtil.success(map);

}

详细请看 SpringBoot集成MyBatis的分页插件PageHelper

五、springData分页

service层

...

Sort.Order travelDate = new Sort.Order(Sort.Direction.DESC, "travelDate");

Sort.Order createdTime = new Sort.Order(Sort.Direction.DESC, "createdTime");

Sort sort = new Sort(travelDate, createdTime);

Pageable pageable = new PageRequest(page, pageSize, sort);

List items = null;

try {

    items = travelRepository.getTravelItemsByTravelDateBetweenAndUserId(theStartDate, theEndDate, openId, pageable);

} catch (Exception e) {

    throw new DatabaseRelatedException("TravelRepository异常");

}

...

dao层:接口继承的是PagingAndSortingRepository接口,注意要加@Repository注解


来自:

https://blog.csdn.net/riemann_/article/details/90454331

你可能感兴趣的:(Java实现分页)