zoukankan      html  css  js  c++  java
  • mysql实现分页的几种方式

    mysql实现分页的几种方式:

    第一种:使用框架自带的pageable来进行分页

    package com.cellstrain.icell.repository.repositoryImpl;

    import com.cellstrain.icell.entity.LunBoTu;

    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.Query;
    import java.util.List;

    public class LunBoTuRepositoryImpl {

    @PersistenceContext
    private EntityManager entityManager;

    public List<LunBoTu> findTopThree(){
    StringBuffer sql = new StringBuffer("from LunBoTu lbt where 1=1 order by lbt.id desc");
    Query query = entityManager.createQuery(sql.toString());
    query.setFirstResult(0);
    query.setMaxResults(6);
    List<LunBoTu> lunbotuList = query.getResultList();
    return lunbotuList;
    }
    }

    第二种:使用limit关键字来进行分页

    public Page<News> FileDownloadByConditions(String condition, Pageable pageable) {
    StringBuffer sql = new StringBuffer("from news n left join newscategory c on n.newsCategoryId=c.id where c.id = 12 and ");
    if(!StringUtils.isEmpty(condition)){
    sql.append(" n.newsAuthor like '%"+condition+"%' or n.newsTitle like '%"+condition+"%' or c.categoryName like '%"+condition+"%' or n.roundup like '%"+condition+"%' and 1=1 ");
    }else{
    sql.append(" 1=1");
    }
    String fen_ye_sql = sql.toString() + " limit " + pageable.getOffset() + "," + pageable.getPageSize();
    Long total = jdbcTemplate.queryForObject("select count(*) " + sql.toString(), Long.class);
    List<News> newsList = null;
    try {
    newsList = jdbcTemplate.query("select n.*,c.categoryName " + fen_ye_sql.toString(), new BeanPropertyRowMapper(News.class));
    } catch (Exception e) {
    e.printStackTrace();
    }
    Page<News> page = new PageImpl(newsList, pageable, total);
    return page;
    }
  • 相关阅读:
    查看 FormData 中已存在的值
    dedecms 后台可以上传mp4,但无法选择
    dedecms 文章根据 权重排序
    js 单行注释
    dedecms给图片加水印覆盖整张图片
    Nginx服务器 配置 https
    dedecms 后台 菜单点击后打开的慢
    用 PHP文件引入css样式
    TFT、LCD、OLED、LPTS、CRT等显示屏的区别
    ORCAD中的一些操作小技巧
  • 原文地址:https://www.cnblogs.com/qianzf/p/6781670.html
Copyright © 2011-2022 走看看