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

 

转载于:https://www.cnblogs.com/qianzf/p/6781670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值