springboot整合jpa的分頁操作

2021-09-23 07:39:32 字數 2111 閱讀 6041

分頁的工具類

package com.pig.pigbase.tools;

import org.springframework.data.domain.pagerequest;

import org.springframework.data.domain.pageable;

import org.springframework.data.domain.sort;

/** * 封裝分頁和排序

*/public class pageabletools

/*** 獲取基礎分頁物件,每頁條數預設15條

* - 預設以id降序排序

* @param page 獲取第幾頁

* @return

*/public static pageable basicpage(integer page)

/*** 獲取基礎分頁物件,每頁條數預設15條

* @param page 獲取第幾頁

* @param dtos 排序物件陣列

* @return

*/public static pageable basicpage(integer page, sortdto... dtos)

/*** 獲取基礎分頁物件,排序方式預設降序

* @param page 獲取第幾頁

* @param size 每頁條數

* @param orderfield 排序字段

* @return

*/public static pageable basicpage(integer page, integer size, string orderfield)

/*** 獲取基礎分頁物件

* - 每頁條數預設15條

* - 排序方式預設降序

* @param page 獲取第幾頁

* @param orderfield 排序字段

* @return

*/public static pageable basicpage(integer page, string orderfield)

}

排序的工具類:

package com.pig.pigbase.tools;

import org.springframework.data.domain.sort;

/** * 封裝排序

*/public class sorttools

public static sort basicsort(string ordertype, string orderfield)

public static sort basicsort(sortdto... dtos) else

}return result;

}}

運用:(給使用者進行分頁查詢)

package com.pig.manage.controller.apicontroller;

import com.alibaba.fastjson.jsonobject;

import com.google.common.collect.maps;

import com.pig.manage.service.apiserviceimpl.*;

@restcontroller

public class apiusercontroller

list datalist = new arraylist();

mapresult = new hashmap<>();

result.put("data", apiuserpage.getcontent());

result.put("totalpage", apiuserpage.gettotalpages());

result.put("totalsize", apiuserpage.gettotalelements());

datalist.add(result);

return new dataresponse(constanterror.error_code_0, constanterror.error_msg_0, datalist);

}}

學習日誌 springboot整合JPA

1.編寫實體類 entity 告訴spring這是乙個實體類 table 之地你和哪個表對映。不寫則預設是user,即實體類名的小寫。指定 name public class user 3.配置檔案編寫 spring datasource driver class name com.mysql.jd...

springboot 整合JPA 主鍵生成方式常用

採用mysql主鍵自增屬性生成 generatedvalue strategy generationtype.identity 指明生成主鍵的方式為mysql的自增主鍵 id column name id generatedvalue strategy generationtype.identity...

springboot整合JPA的快速使用

這是專案結構 先導入依賴 org.springframework.boot spring boot starter data jpa 其次配置yml檔案 spring datasource driver class name com.mysql.jdbc.driver url jdbc mysql ...