一洲赵、limit關(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);
? ? }
}
sql語句
select * from student where province = #{province} limit #{offset},#{limit}
二、hibernate分頁
service層
@Override
? public List getStudents(Integer? pageNo璃岳,Integer? pageSize) throws Exception {
? // 分頁數(shù)據(jù)
? int[] startIdAndCount = new int[2];
? startIdAndCount[0] = pageNo * pageSize;
? startIdAndCount[1] = pageSize;
? return studentDao.selectStudentsByPage(startIdAndCount);
}
dao層
@Override
public List findByHqlPage(int[] startIdAndCount) throws Exception {
String hql = "...";
try {
Query query = getSession().createQuery(hql);
// 設(shè)置分頁
if (startIdAndCount != null && startIdAndCount.length > 0) {
int rowStartIdx = Math.max(0, startIdAndCount[0]);
if (rowStartIdx > 0) {
query.setFirstResult(rowStartIdx);// 設(shè)置開始取值的索引
}
if (startIdAndCount.length > 1) {
int rowCount = Math.max(0, startIdAndCount[1]);
if (rowCount > 0) {
query.setMaxResults(rowCount);// 設(shè)置結(jié)束取值的索引
}
}
}
return query.list();
} catch (RuntimeException re) {
log.error("分頁查詢失敻恕住涉!", re);
throw re;
}
}
三裆站、截取List查詢結(jié)果分頁(簡單粗暴)
...
List<StudentEnroll> students = studentlDao.getAllStudents();
int count = 0;
if(studentEnrolls != null && studentEnrolls.size() > 0) {
count = studentEnrolls.size();
int fromIndex = pageNo * pageSize;
int toIndex = (pageNo + 1) * pageSize;
if(toIndex > count) {
toIndex = count;
}
List<StudentEnroll> pageList = studentEnrolls.subList(fromIndex, toIndex);
...
————————————————
四、mybatis框架pageHelper插件分頁
Spring整合:
導(dǎo)入pom.xml
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
? ? ? <groupId>com.github.pagehelper</groupId>
? ? ? <artifactId>pagehelper</artifactId>
? ? ? <version>5.1.2</version>
</dependency>
配置項目配置文件(我在spring和mybatis整合的配置文件中配置的精盅,如果在mybatis核心配置文件中配置,百度一下)
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ? <!-- 依賴數(shù)據(jù)源 -->
? ? ? ? <property name="dataSource" ref="dataSource"/>
? ? ? ? <!-- 注冊加載myBatis映射文件 -->
? ? ? ? <property name="mapperLocations">
? ? ? ? ? ? <array>
? ? ? ? ? ? ? ? <value>classpath*:com/yyz/mapper/*Mapper.xml</value>
? ? ? ? ? ? </array>
? ? ? ? </property>
? ? ? ? <!-- PageHelper分頁配置 -->
? ? ? ? <property name="plugins">
? ? ? ? ? ? <array>
? ? ? ? ? ? ? ? <bean class="com.github.pagehelper.PageInterceptor">
? ? ? ? ? ? ? ? ? ? <property name="properties">
? ? ? ? ? ? ? ? ? ? ? ? <!--使用下面的方式配置參數(shù)谜酒,一行配置一個叹俏,后面會有所有的參數(shù)介紹 -->
? ? ? ? ? ? ? ? ? ? ? ? <value>
? ? ? ? ? ? ? ? ? ? <!--helperDialect屬性來指定分頁插件使用哪種方言。-->
? ? ? ? ? ? ? ? ? ? ? ? ? ? helperDialect=mysql
? ? ? ? ? ? ? ? ? ? <!--分頁合理化參數(shù)僻族,設(shè)置為true時粘驰,pageNum<=0時會查詢第一頁,pageNum>pages(超過總數(shù)時),會查詢最后一頁。-->
? ? ? ? ? ? ? ? ? ? ? ? ? ? reasonable=true
? ? ? ? ? ? ? ? ? ? <!--為了支持startPage(Object params)方法述么,增加了該參數(shù)來配置參數(shù)映射蝌数,用于從對象中根據(jù)屬性名取值,
? ? ? ? ? ? ? ? ? ? ? ? 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable-->
? ? ? ? ? ? ? ? ? ? ? ? ? ? params=count=countSql
? ? ? ? ? ? ? ? ? ? <!--支持通過Mapper接口參數(shù)來傳遞分頁參數(shù)碉输,默認(rèn)值false籽前,分頁插件會從查詢方法的參數(shù)值中,自動根據(jù)上面 params 配
? ? ? ? ? ? ? ? ? ? 置的字段中取值敷钾,查找到合適的值時就會自動分頁枝哄。-->
? ? ? ? ? ? ? ? ? ? ? ? ? ? supportMethodsArguments=true
? ? ? ? ? ? ? ? ? ? <!--默認(rèn)值為 false。設(shè)置為 true 時阻荒,允許在運行時根據(jù)多數(shù)據(jù)源自動識別對應(yīng)方言的分頁-->
? ? ? ? ? ? ? ? ? ? ? ? ? ? autoRuntimeDialect=true
? ? ? ? ? ? ? ? ? ? ? ? </value>
? ? ? ? ? ? ? ? ? ? </property>
? ? ? ? ? ? ? ? </bean>
? ? ? ? ? ? </array>
? ? ? ? </property>
? ? ? ? <!-- 給數(shù)據(jù)庫實體起別名 -->
? ? ? ? <property name="typeAliasesPackage" value="com.yyz.entity;"/>
</bean>
————————————————
SpringBoot整合:
<!--分頁插件-->
<dependency>
? ? <groupId>com.github.pagehelper</groupId>
? ? <artifactId>pagehelper-spring-boot-starter</artifactId>
? ? <version>最新版本</version>
</dependency>
配置項目application.yml文件
#bybatis分頁插件配置
pagehelper:?
????????helper-dialect: mysql #數(shù)據(jù)庫?
????????reasonable: true?
????????support-methods-arguments: true?
????????params: count=countSql
標(biāo)題分頁插件參數(shù):分頁插件提供了多個可選參數(shù)挠锥,這些參數(shù)使用時,按照上面配置方式中的示例配置即可侨赡。分頁插件可選參數(shù)如下:
dialect:默認(rèn)情況下會使用 PageHelper 方式進行分頁蓖租,如果想要實現(xiàn)自己的分頁邏輯,可以實現(xiàn)Dialect(com.github.pagehelper.Dialect) 接口羊壹,然后配置該屬性為實現(xiàn)類的全限定名稱蓖宦。 使用自定義dialect 實現(xiàn)時,下面的參數(shù)沒有任何作用油猫。
helperDialect:分頁插件會自動檢測當(dāng)前的數(shù)據(jù)庫鏈接稠茂,自動選擇合適的分頁方式。oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby特別注意:使用 SqlServer2012 數(shù)據(jù)庫時情妖,需要手動指定為 sqlserver2012睬关,否則會使用 SqlServer2005的方式進行分頁。
offsetAsPageNum:默認(rèn)值為 false毡证,該參數(shù)對使用 RowBounds 作為分頁參數(shù)時有效电爹。 當(dāng)該參數(shù)設(shè)置為 true時,會將 RowBounds 中的 offset 參數(shù)當(dāng)成 pageNum 使用料睛,可以用頁碼和頁面大小兩個參數(shù)進行分頁丐箩。
rowBoundsWithCount:默認(rèn)值為false摇邦,該參數(shù)對使用 RowBounds 作為分頁參數(shù)時有效。當(dāng)該參數(shù)設(shè)置為true時雏蛮,使用 RowBounds 分頁會進行 count 查詢涎嚼。
pageSizeZero:默認(rèn)值為 false,當(dāng)該參數(shù)設(shè)置為 true 時挑秉,如果 pageSize=0 或者RowBounds.limit = 0 就會查詢出全部的結(jié)果(相當(dāng)于沒有執(zhí)行分頁查詢法梯,但是返回結(jié)果仍然是 Page 類型)。
reasonable:分頁合理化參數(shù)犀概,默認(rèn)值為false立哑。當(dāng)該參數(shù)設(shè)置為 true 時,pageNum<=0時會查詢第一頁姻灶,pageNum>pages(超過總數(shù)時)铛绰,會查詢最后一頁。默認(rèn)false 時产喉,直接根據(jù)參數(shù)進行查詢捂掰。
params:為了支持startPage(Object params)方法,增加了該參數(shù)來配置參數(shù)映射曾沈,用于從對象中根據(jù)屬性名取值这嚣,可以配置 pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默認(rèn)值塞俱,默認(rèn)值為pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero姐帚。
supportMethodsArguments:支持通過 Mapper接口參數(shù)來傳遞分頁參數(shù),默認(rèn)值false障涯,分頁插件會從查詢方法的參數(shù)值中罐旗,自動根據(jù)上面 params配置的字段中取值,查找到合適的值時就會自動分頁唯蝶。
aggregateFunctions:默認(rèn)為所有常見數(shù)據(jù)庫的聚合函數(shù)九秀,允許手動添加聚合函數(shù)(影響行數(shù)),所有以聚合函數(shù)開頭的函數(shù)粘我,在進行count 轉(zhuǎn)換時鼓蜒,會套一層。其他函數(shù)和列會被替換為 count(0)涂滴,其中count列可以自己配置。
重要提示:
當(dāng) offsetAsPageNum=false 的時候晴音,由于 PageNum 問題柔纵,RowBounds查詢的時候 reasonable 會強制為 false。使用 PageHelper.startPage 方法不受影響锤躁。
service層
@Override
public 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);
}
詳細請看?SpringBoot集成MyBatis的分頁插件PageHelper
五搁料、springData分頁
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異常");
}
...
dao層:接口繼承的是PagingAndSortingRepository接口,注意要加@Repository注解