Java中如何实现分页功能

发布时间:2022-02-24 10:57:09 作者:iii
来源:亿速云 阅读:276

本文小编为大家详细介绍“Java中如何实现分页功能”,内容详细,步骤清晰,细节处理妥当,希望这篇“Java中如何实现分页功能”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

一、limit关键字

通过使用这个方法我们来看下相关的service层的代码:

@Service
@Transactional
public class ImplStudentService implements StudentService {
@Resource
private  StudentDao  studentDao;
    @Override
    public List<Student>  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);
 }

跟 limit 关键字方法不同的是,在 hibernate 方法中使用的是 dao层,代码如下所示:

  @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);
 }

三、截取List查询结果分页

对于这个方法会显得比较的简单我们直接来看代码:

  @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);
 }

四、mybatis框架pageHelper插件分页

首先我们来看 Spring整合部分:

导入pom.xml 代码如下所示:

  @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);
 }

我们通过配置项目的配置文件代码如下所示:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <!-- 依赖数据源 -->        <property name="dataSource" ref="dataSource"/>        <!-- 注册加载myBatis映射文件 -->        <property name="mapperLocations">            <array>                <value>classpath*:com/yyz/mapper/*Mapper.xml</value>            </array>        </property>        <!-- PageHelper分页配置 -->        <property name="plugins">            <array>                <bean class="com.github.pagehelper.PageInterceptor">                    <property name="properties">                        <!--使用下面的方式配置参数,一行配置一个,后面会有所有的参数介绍 -->                        <value>                    <!--helperDialect属性来指定分页插件使用哪种方言。-->                            helperDialect=mysql                    <!--分页合理化参数,设置为true时,pageNum<=0时会查询第一页,pageNum>pages(超过总数时),会查询最后一页。-->                            reasonable=true                    <!--为了支持startPage(Object params)方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值,                        可以配置 pageNum,pageSize,count,pageSizeZero,reasonable-->                            params=count=countSql                    <!--支持通过Mapper接口参数来传递分页参数,默认值false,分页插件会从查询方法的参数值中,自动根据上面 params 配                     置的字段中取值,查找到合适的值时就会自动分页。-->                            supportMethodsArguments=true                    <!--默认值为 false。设置为 true 时,允许在运行时根据多数据源自动识别对应方言的分页-->                            autoRuntimeDialect=true                        </value>                    </property>                </bean>            </array>        </property>        <!-- 给数据库实体起别名 -->        <property name="typeAliasesPackage" value="com.yyz.entity;"/> </bean>

SpringBoot整合:

  @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);
 }

配置项目 application.xml 文件,代码如下所示:

  @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);
 }

对于在分页插件中为我们也提供了下面这些参数:

当然在这些方法参数中,当 offsetAsPageNum=false 的时候,由于代码中 PageNum 的问题 RowBound 在查询的时候 reasonable 会强制为 false ,但是使用PageHelper.startPage 方法不受影响。我们来看下相关内容的 service层的代码:

 @Overridepublic ResponseResult selectAllStudent(Integer pageNum, Integer pageSize) {    Map<String,Object> map = new HashMap<>();    PageHelper.startPage(pageNum,pageSize);    List<Student>  students = studentMapper.selectAllStudents();    PageInfo pageInfo = new PageInfo(students);    long total = pageInfo.getTotal();    map.put("result",pageInfo);    map.put("count",total);    return ResponseResultUtil.success(map);}

五、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<TravelItem> items = null;try {    items = travelRepository.getTravelItemsByTravelDateBetweenAndUserId(theStartDate, theEndDate, openId, pageable);} catch (Exception e) {    throw new DatabaseRelatedException("TravelRepository异常");}

这就是有关于在service层的代码,那么对于dao层的话,接口继承的是PagingAndSortingRepository接口,所以我们注意要加@Repository注解。

读到这里,这篇“Java中如何实现分页功能”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Oracle中Java分页功能有哪些
  2. java web如何实现分页功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:HTML5 <article>标签元素怎么用

下一篇:HTML页面编码charset选择的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》