mysql实现分页的几种方式:

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

  1. package com.cellstrain.icell.repository.repositoryImpl;
  2.  
  3. import com.cellstrain.icell.entity.LunBoTu;
  4.  
  5. import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    import javax.persistence.Query;
    import java.util.List;
  6.  
  7. public class LunBoTuRepositoryImpl {
  8.  
  9. @PersistenceContext
    private EntityManager entityManager;
  10.  
  11. 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关键字来进行分页

  1. 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;
    }

mysql实现分页的几种方式的更多相关文章

  1. Mysql查看版本号的五种方式介绍

    Mysql查看版本号的五种方式介绍 作者: 字体:[增加 减小] 类型:转载 时间:2013-05-03   一.使用命令行模式进入mysql会看到最开始的提示符;二.命令行中使用status可以看到 ...

  2. mysql级联更新的两种方式:触发器更新和外键

    1.mysql级联更新有两种方式:触发器更新和外键更新. 2.触发器更新和外键更新的目的都是为了保证数据完整性. 我们通常有这样的需求:删除表Table 1中记录,需要同时删除其它表中与Table 1 ...

  3. [Mysql]查看版本号的五种方式

    [Mysql]查看版本号的五种方式   目录(?)[+]   查看版本信息 #1 使用命令行模式进入mysql会看到最开始的提示符 Your MySQL connection id is 3Serve ...

  4. sqlserver实现分页的几种方式

    sqlserver实现分页的几种方式 第一种:使用org.springframework.data.domain.Page来进行分页 package com.cellstrain.icell.repo ...

  5. MySQL查看版本号的五种方式介绍1111111

    MySQL查看版本号的五种方式介绍 1 命令行模式登录MySQL [root@localhost ~]# mysql -uroot -p Enter password: Welcome to the ...

  6. mysql复制表的两种方式

    mysql复制表的两种方式. 第一.只复制表结构到新表 create table 新表 select * from 旧表 where 1=2 或者 create table 新表 like 旧表 第二 ...

  7. springmvc+jpa实现分页的两种方式

    1.工具类 public final class QueryTool { public static PageRequest buildPageRequest(int pageNumber, int ...

  8. linux中mysql密码找回的两种方式

    方法一:修改my.cnf配置文件 1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的  ...

  9. mysql批量更新的两种方式效率试验<二>

    Mysql两种批量更新的对比 简介: mysql搭载mybits框架批量更新有两种方式,一种是在xml中循环整个update语句,中间以‘:’隔开,还有一种是使用case when 变相实现批量更新, ...

随机推荐

  1. 将 MyBatis3 的支持添加到 Spring

    http://www.mybatis.org/spring/zh/index.html What is MyBatis-Spring? MyBatis-Spring 会帮助你将 MyBatis 代码无 ...

  2. WDA-FPM-4-用OVP做查询跳转到明细

    转载:https://www.cnblogs.com/sapSB/p/10100697.html   FPM四:用OVP做查询跳转到明细 前面做了查询的UIBB配置,在这边可以直接复用,查询的feed ...

  3. VirtualBox如何扩展虚拟机Ubuntu的硬盘容量?

    一.问题描述 刚刚在VirtualBox中使用Ubuntu虚拟机中,出现了虚拟硬盘不够用的情况.  乖乖,查了一下磁盘空间,如下所示: df -H 原来是上午安装Ubuntu虚拟机的时候,选择了动态分 ...

  4. iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 规范与部署

    沪江CCtalk视频地址:https://www.cctalk.com/v/15114923889450 规范与部署 懒人推动社会进步. 本篇中,我们会讲述三个知识点 定制书写规范 开发环境运行 如何 ...

  5. DataType 数据类型

    基本类型:四类八种:数值 : 整数:byte,short,int,long.默认是 int 小数:float,double                  默认是 double 布尔:boolean ...

  6. C#四舍五入说明

    string.Format("{0:N2}", d) 与 Math.Round(d, 2).ToString() 不总是相等 string.Format("{0:N2}& ...

  7. conductor 事件处理程序

    Introduction conductor中的事件提供工作流之间的松散耦合,并支持从外部系统生成和消耗事件. 包括: 1. 能够在外部系统像SQS或Conductor内部生成一个事件(消息). 2. ...

  8. python selenium点滴

    from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Ch ...

  9. 常用数据库ID格式

    转自:http://www.biotrainee.com/thread-411-1-1.html 常用数据库 ID ID 示例 ID 来源 ENSG00000116717 Ensemble ID GA ...

  10. MySQL8 重置改root密码及开放远程访问

    1. 修改配置文件 先修改配置文件:vim /etc/my.conf 在 [mysqld] 下加上下面这行 skip-grant-tables 重启 mysql 服务: service mysqld ...