您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“Java中如何實(shí)現(xiàn)分頁(yè)功能”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Java中如何實(shí)現(xiàn)分頁(yè)功能”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
一、limit關(guān)鍵字
通過(guò)使用這個(gè)方法我們來(lái)看下相關(guān)的service層的代碼:
@Service
@Transactional
public class ImplStudentService implements StudentService {
@Resource
private StudentDao studentDao;
@Override
public List<Student> selectAllStudent(String province, Integer offset, Integer limit) {
return studentDao.selectAll(province,offset,limit);
}
}
對(duì)應(yīng)的相關(guān) sql 語(yǔ)句的代碼如下所示:
select * from student where province = #{province} limit #{offset},#{limit}
二、hibernate分頁(yè)
首先我們來(lái)看下service層的代碼:
@Override
public List getStudents(Integer pageNo,Integer pageSize) throws Exception {
// 分頁(yè)數(shù)據(jù)
int[] startIdAndCount = new int[2];
startIdAndCount[0] = pageNo * pageSize;
startIdAndCount[1] = pageSize;
return studentDao.selectStudentsByPage(startIdAndCount);
}
跟 limit 關(guān)鍵字方法不同的是,在 hibernate 方法中使用的是 dao層,代碼如下所示:
@Override
public List getStudents(Integer pageNo,Integer pageSize) throws Exception {
// 分頁(yè)數(shù)據(jù)
int[] startIdAndCount = new int[2];
startIdAndCount[0] = pageNo * pageSize;
startIdAndCount[1] = pageSize;
return studentDao.selectStudentsByPage(startIdAndCount);
}
三、截取List查詢(xún)結(jié)果分頁(yè)
對(duì)于這個(gè)方法會(huì)顯得比較的簡(jiǎn)單我們直接來(lái)看代碼:
@Override
public List getStudents(Integer pageNo,Integer pageSize) throws Exception {
// 分頁(yè)數(shù)據(jù)
int[] startIdAndCount = new int[2];
startIdAndCount[0] = pageNo * pageSize;
startIdAndCount[1] = pageSize;
return studentDao.selectStudentsByPage(startIdAndCount);
}
四、mybatis框架pageHelper插件分頁(yè)
首先我們來(lái)看 Spring整合部分:
導(dǎo)入pom.xml 代碼如下所示:
@Override
public List getStudents(Integer pageNo,Integer pageSize) throws Exception {
// 分頁(yè)數(shù)據(jù)
int[] startIdAndCount = new int[2];
startIdAndCount[0] = pageNo * pageSize;
startIdAndCount[1] = pageSize;
return studentDao.selectStudentsByPage(startIdAndCount);
}
我們通過(guò)配置項(xiàng)目的配置文件代碼如下所示:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 依賴(lài)數(shù)據(jù)源 --> <property name="dataSource" ref="dataSource"/> <!-- 注冊(cè)加載myBatis映射文件 --> <property name="mapperLocations"> <array> <value>classpath*:com/yyz/mapper/*Mapper.xml</value> </array> </property> <!-- PageHelper分頁(yè)配置 --> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageInterceptor"> <property name="properties"> <!--使用下面的方式配置參數(shù),一行配置一個(gè),后面會(huì)有所有的參數(shù)介紹 --> <value> <!--helperDialect屬性來(lái)指定分頁(yè)插件使用哪種方言。--> helperDialect=mysql <!--分頁(yè)合理化參數(shù),設(shè)置為true時(shí),pageNum<=0時(shí)會(huì)查詢(xún)第一頁(yè),pageNum>pages(超過(guò)總數(shù)時(shí)),會(huì)查詢(xún)最后一頁(yè)。--> reasonable=true <!--為了支持startPage(Object params)方法,增加了該參數(shù)來(lái)配置參數(shù)映射,用于從對(duì)象中根據(jù)屬性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable--> params=count=countSql <!--支持通過(guò)Mapper接口參數(shù)來(lái)傳遞分頁(yè)參數(shù),默認(rèn)值false,分頁(yè)插件會(huì)從查詢(xún)方法的參數(shù)值中,自動(dòng)根據(jù)上面 params 配 置的字段中取值,查找到合適的值時(shí)就會(huì)自動(dòng)分頁(yè)。--> supportMethodsArguments=true <!--默認(rèn)值為 false。設(shè)置為 true 時(shí),允許在運(yùn)行時(shí)根據(jù)多數(shù)據(jù)源自動(dòng)識(shí)別對(duì)應(yīng)方言的分頁(yè)--> autoRuntimeDialect=true </value> </property> </bean> </array> </property> <!-- 給數(shù)據(jù)庫(kù)實(shí)體起別名 --> <property name="typeAliasesPackage" value="com.yyz.entity;"/> </bean>
SpringBoot整合:
@Override
public List getStudents(Integer pageNo,Integer pageSize) throws Exception {
// 分頁(yè)數(shù)據(jù)
int[] startIdAndCount = new int[2];
startIdAndCount[0] = pageNo * pageSize;
startIdAndCount[1] = pageSize;
return studentDao.selectStudentsByPage(startIdAndCount);
}
配置項(xiàng)目 application.xml
文件,代碼如下所示:
@Override
public List getStudents(Integer pageNo,Integer pageSize) throws Exception {
// 分頁(yè)數(shù)據(jù)
int[] startIdAndCount = new int[2];
startIdAndCount[0] = pageNo * pageSize;
startIdAndCount[1] = pageSize;
return studentDao.selectStudentsByPage(startIdAndCount);
}
對(duì)于在分頁(yè)插件中為我們也提供了下面這些參數(shù):
dialect:
默認(rèn)情況下會(huì)使用 PageHelper
方式進(jìn)行分頁(yè),如果想要實(shí)現(xiàn)自己的分頁(yè)邏輯,可以實(shí)現(xiàn) 。
Dialect
(com.github.pagehelper.Dialect) 接口,然后配置該屬性為實(shí)現(xiàn)類(lèi)的全限定名稱(chēng)。 使用自定義 dialect
實(shí)現(xiàn)時(shí),下面的參數(shù)沒(méi)有任何作用。
helperDialect
:分頁(yè)插件會(huì)自動(dòng)檢測(cè)當(dāng)前的數(shù)據(jù)庫(kù)鏈接,自動(dòng)選擇合適的分頁(yè)方式oracle
,mysql
,mariadb
,sqlite
,hsqldb
,postgresql
,db2
,sqlserver
,informix
,h3
,sqlserver2012
,derby
。特別注意:使用 SqlServer2012 數(shù)據(jù)庫(kù)時(shí),需要手動(dòng)指定為 sqlserver2012,否則會(huì)使用 SqlServer2005
的方式進(jìn)行分頁(yè)。 offsetAsPageNum
:默認(rèn)值為 false
,該參數(shù)對(duì)使用 RowBounds
作為分頁(yè)參數(shù)時(shí)有效。 當(dāng)該參數(shù)設(shè)置為 true
時(shí),會(huì)將 RowBounds
中的 offset
參數(shù)當(dāng)成 pageNum
使用,可以用頁(yè)碼和頁(yè)面大小兩個(gè)參數(shù)進(jìn)行分頁(yè)。
rowBoundsWithCount
:默認(rèn)值為false
,該參數(shù)對(duì)使用 RowBounds
作為分頁(yè)參數(shù)時(shí)有效。 當(dāng)該參數(shù)設(shè)置為true
時(shí),使用 RowBounds
分頁(yè)會(huì)進(jìn)行 count
查詢(xún)。
pageSizeZero
:默認(rèn)值為 false,當(dāng)該參數(shù)設(shè)置為 true 時(shí),如果 pageSize=0 或者 RowBounds.limit = 0 就會(huì)查詢(xún)出全部的結(jié)果(相當(dāng)于沒(méi)有執(zhí)行分頁(yè)查詢(xún),但是返回結(jié)果仍然是 Page 類(lèi)型)。
reasonable
:分頁(yè)合理化參數(shù),默認(rèn)值為false
。當(dāng)該參數(shù)設(shè)置為 true
時(shí),pageNum<=0 時(shí)會(huì)查詢(xún)第一頁(yè),pageNum>pages(超過(guò)總數(shù)時(shí)),會(huì)查詢(xún)最后一頁(yè)。默認(rèn)false
時(shí),直接根據(jù)參數(shù)進(jìn)行查詢(xún)。
params
:為了支持startPage(Object params)
方法,增加了該參數(shù)來(lái)配置參數(shù)映射,用于從對(duì)象中根據(jù)屬性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable
,不配置映射的用默認(rèn)值, 默認(rèn)值為pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero
。
supportMethodsArguments
:支持通過(guò) Mapper
接口參數(shù)來(lái)傳遞分頁(yè)參數(shù),默認(rèn)值false
,分頁(yè)插件會(huì)從查詢(xún)方法的參數(shù)值中,自動(dòng)根據(jù)上面 params
配置的字段中取值,查找到合適的值時(shí)就會(huì)自動(dòng)分頁(yè)。
aggregateFunctions
:默認(rèn)為所有常見(jiàn)數(shù)據(jù)庫(kù)的聚合函數(shù),允許手動(dòng)添加聚合函數(shù)(影響行數(shù)),所有以聚合函數(shù)開(kāi)頭的函數(shù),在進(jìn)行 count
轉(zhuǎn)換時(shí),會(huì)套一層。其他函數(shù)和列會(huì)被替換為 count(0)
,其中count
列可以自己配置。
當(dāng)然在這些方法參數(shù)中,當(dāng) offsetAsPageNum=false
的時(shí)候,由于代碼中 PageNum
的問(wèn)題 RowBound
在查詢(xún)的時(shí)候 reasonable
會(huì)強(qiáng)制為 false
,但是使用PageHelper.startPage
方法不受影響。我們來(lái)看下相關(guān)內(nèi)容的 service層的代碼:
@Overridepublic ResponseResult selectAllStudent(Integer pageNum, Integer pageSize) { Map<String,Object> map = new HashMap<>(); PageHelper.startPage(pageNum,pageSize); List<Student> students = studentMapper.selectAllStudents(); PageInfo pageInfo = new PageInfo(students); long total = pageInfo.getTotal(); map.put("result",pageInfo); map.put("count",total); return ResponseResultUtil.success(map);}
五、springData分頁(yè)
對(duì)于在service層的代碼如下所示:
Sort.Order travelDate = new Sort.Order(Sort.Direction.DESC, "travelDate");Sort.Order createdTime = new Sort.Order(Sort.Direction.DESC, "createdTime");Sort sort = new Sort(travelDate, createdTime);Pageable pageable = new PageRequest(page, pageSize, sort);List<TravelItem> items = null;try { items = travelRepository.getTravelItemsByTravelDateBetweenAndUserId(theStartDate, theEndDate, openId, pageable);} catch (Exception e) { throw new DatabaseRelatedException("TravelRepository異常");}
這就是有關(guān)于在service層的代碼,那么對(duì)于dao層的話(huà),接口繼承的是PagingAndSortingRepository
接口,所以我們注意要加@Repository
注解。
讀到這里,這篇“Java中如何實(shí)現(xiàn)分頁(yè)功能”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。