mysql实现分页的几种方式
2017-04-28 15:04
337 查看
mysql实现分页的几种方式:
第一种:使用框架自带的pageable来进行分页
第二种:使用limit关键字来进行分页
第一种:使用框架自带的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; }
相关文章推荐
- 常用的几种 SQLServer 分页查询方式实现
- MySql查询分页的几种方式例句
- [置顶] SSH hibernate 分页的几种实现方式
- 几种常见的分页查询实现方式
- MyBatis+Mysql 实现分页(最简单通用的分页方式)
- mysql和oracle分页实现方式
- sqlserver实现分页的几种方式
- 数据查询分页的几种实现方式
- MySQL和Oracle分页查询实现方式
- js实现页面跳转的几种方式
- JSF中分页的实现方式
- js实现页面跳转的几种方式
- js实现页面跳转的几种方式
- 实现等待窗体的几种方式
- js实现页面跳转的几种方式
- js实现页面跳转的几种方式
- js实现页面跳转的几种方式
- js实现页面跳转的几种方式
- js实现页面跳转的几种方式
- 默默小谈PHP&MYSQL分页原理及实现