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

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. spring security 配置xml 参考

    https://blog.csdn.net/zsq520520/article/details/77880491

  2. springboot - SqlSessionFactoryBean falls in circular dependencies by Spring Boot's DataSourceInitializer

    springboot mybatis配置多数据源的时候,报错:There is a circular dependency between 7 beans in the application con ...

  3. UI5-文档-2.3-使用SAPUI5工具为Eclipse开发应用程序

    用于为简单用例开发应用程序.用于Eclipse的SAPUI5应用程序开发工具提供向导来支持您以一种简单的方式创建应用程序.使用application project向导,将自动创建包含视图和控制器的必 ...

  4. 转载:MySQL和Redis 数据同步解决方案整理

    from: http://blog.csdn.net/langzi7758521/article/details/52611910 最近在做一个Redis箱格信息数据同步到数据库Mysql的功能. 自 ...

  5. 安卓上为什么不能用system.io.file读取streammingAssets目录下的文件

    首先,看文档: Streaming Assets   Most assets in Unity are combined into the project when it is built. Howe ...

  6. sparkSQL、dataframe

    http://www.aboutyun.com/forum.php?mod=viewthread&tid=12358&page=1 空值填充:http://spark.apache.o ...

  7. spark UDF函数

    Spark(Hive) SQL中UDF的使用(Python):http://www.tuicool.com/articles/3yMBNb7

  8. bug-ajax

    ajax 的get 方法参数及url的长度有限制 问题:url的参数只有一个url?id=101001000000000000000001 参数过长,ajax会报错. 解决方法:1,把get换成pos ...

  9. log4j每天,每小时产生一日志文件

    log4j每天,每小时产生一日志文件 2016年08月05日 14:14:33 阅读数:6254 一.之前的文章中有log4j的相关配置以及属性的介绍,下面我们先把配置列出来:   log4j.roo ...

  10. 9-centos定时任务-不起作用- 没指明路径!!!

    crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务 工具,并且会自动启动crond进程,cro ...