日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

sqlserver实现分页的几种方式

發(fā)布時(shí)間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sqlserver实现分页的几种方式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

sqlserver實(shí)現(xiàn)分頁(yè)的幾種方式

第一種:使用org.springframework.data.domain.Page來進(jìn)行分頁(yè)

package com.cellstrain.icell.repository.repositoryImpl;

import com.cellstrain.icell.entity.V_Paper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

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

@Repository(value = "vPaperRepository")
public class VPaperRepositoryImpl {
@Autowired
private JdbcTemplate jdbcTemplate;

@PersistenceContext
private EntityManager entityManager;

/**
* 后臺(tái)分頁(yè)查詢
* @param condition
* @param pageable
* @return
*/
public Page findByCondition(String condition, Pageable pageable){
StringBuffer hql = new StringBuffer("from V_Paper p where ");
if(!StringUtils.isEmpty(condition)){
hql.append("p.title like '%"+condition+"%' or p.entryName like '%"+condition+"%' or p.pName like '%"+condition+"%' or p.auth like '%"+condition+"%' order by p.paperId desc");
}else{
hql.append("1=1 order by p.paperId desc");
}
Query query = entityManager.createQuery(hql.toString());
//得到符合記錄的總數(shù)
int count = query.getResultList().size();
Long total = (long)count;
//分頁(yè)查詢
query.setFirstResult(pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
List<V_Paper> proteinRanksList = query.getResultList();
//封裝Page
Page<V_Paper> page =new PageImpl<V_Paper>(proteinRanksList,pageable,total);
}

}

第二種:使用top關(guān)鍵字來進(jìn)行分頁(yè)
package com.cellstrain.icell.repository.repositoryImpl;

import com.cellstrain.icell.entity.V_Paper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

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

@Repository(value = "vPaperRepository")
public class VPaperRepositoryImpl {
@Autowired
private JdbcTemplate jdbcTemplate;

@PersistenceContext
private EntityManager entityManager;

/**
* 后臺(tái)分頁(yè)查詢
* @param condition
* @param pageable
* @return
*/
public Page findByCondition(String condition, Pageable pageable){
if(StringUtils.isEmpty(condition)){
condition="";
}
int pageSize=pageable.getPageSize();
int pageNow=pageable.getPageNumber();
String sql="select top "+pageSize+" vp.paperId,vp.title,vp.entryName,vp.source,vp.recordDate,vp.url,vp.auth,dbo.JoinStr(vp.paperId) as pNames" +
" from V_Paper vp where vp.paperId not in(select top "+pageSize*pageNow+" paperId from V_Paper order by paperId desc) and (title like '%"+condition+"%' or contents like '%"+condition+"%') " +
"group by vp.paperId,vp.title,vp.entryName,vp.source,vp.recordDate,vp.url,vp.auth order by vp.paperId desc";
List paperList=null;
Long total = jdbcTemplate.queryForObject("select count(*) from V_Paper",Long.class);
try{
paperList=jdbcTemplate.queryForList(sql);
}catch (Exception e){
e.printStackTrace();
}
Page page = new PageImpl(paperList, pageable, total);
return page;
}
}
?

轉(zhuǎn)載于:https://www.cnblogs.com/qianzf/p/6781618.html

總結(jié)

以上是生活随笔為你收集整理的sqlserver实现分页的几种方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。