SpringBoot+JPA?分頁查詢指定列并返回指定實體方式
SpringBoot JPA分頁查詢指定列并返回指定實體
用習慣Mybatis,沒用過jpa 真是各種踩坑了
腦殼疼,一個分頁弄老半天,原來就一句話的事情,唉
先來說說正常的JPA如何操作
實體類對應表來創(chuàng)建,舉個例子
@Entity @Table(name = "td_user") public class TdUser extends BaseModel { private static final long serialVersionUID = 8659266017517096998L; /** * id */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(nullable = false, name = "id", length = 10) private Long id; /** * 用戶所屬平臺 */ @Column(nullable = false, name = "partner_id", length = 11) private Integer partnerId; /** * 用戶名 */ @Column(nullable = false, name = "username", length = 32, unique = true) private String username; /** * 用戶昵稱 */ @Column(name = "nickname", length = 64) private String nickname; /** * 密碼 */ @JsonIgnore @Column(nullable = false, name = "password", length = 16) private String password; /** * id * * getter setter方法省略 */
相對應的建立操作接口
@Repository public interface TdUserRepository extends JpaRepository<TdUser, Long>, JpaSpecificationExecutor<TdUser> { }
分頁查詢的時候只要一句話
// Partner partner 外部傳入的分頁信息 Page<TdUser> allPage = TdUserRepository.findAll(pageable);
但是有時候可能不需要返回所有字段,只要返回一部分而已,經(jīng)過各種嘗試,有一種最簡單的方法
就是把想要返回的字段再構建成一個實體,實體的屬性需要和數(shù)據(jù)庫字段進行映射,然后單獨寫一個Repository就可以了
比如
@Entity @Table(name = "td_user") public class UserVO extends BaseModel { private static final long serialVersionUID = 8659266017517096998L; /** * id */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(nullable = false, name = "id", length = 10) private Long id; /** * 用戶名 */ @Column(nullable = false, name = "username", length = 32, unique = true) private String username; /** * id * * getter setter方法省略 */ @Repository public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> { }
調(diào)用的時候
// Partner partner 外部傳入的分頁信息 Page<User> allPage = UserRepository.findAll(pageable);
我一開始也嘗試過寫sql,但是我發(fā)現(xiàn)返回數(shù)據(jù)會變成Page<Object[]>,經(jīng)過json數(shù)列化以后數(shù)據(jù)會變成 [ ["1":"string"],...]這種樣子而不是key-value的形式
改了n遍后發(fā)現(xiàn)這樣是最簡單的。。。
SpringBoot JPA實現(xiàn)自定義語句分頁查詢
例:
1.JPA持久層 InvoiceRepository.java
@Repository public interface InvoiceRepository extends JpaRepository<Invoice, Integer> { @Query( value = "SELECT * from invoice_apply where company_id=?1 and IF (?2 is null ,1=1,status = ?2)", countQuery = "select count(*) from invoice_apply where company_id=?1 and IF (?2 is null ,1=1,status = ?2)", nativeQuery = true) Page<Map> findInvoice(int companyID, String status, Pageable pageable); }
2.服務層
@Override public Map findInvoice(int companyID, String status, Integer page, Integer pageSize) { Double amount = companyFinanceRepository.findDCompanyFinance(companyID); //分頁查詢 Pageable pageable = PageRequest.of(page, pageSize, Sort.Direction.ASC, "id"); Page<Map> invoiceList = invoiceRepository.findInvoice(companyID, status, pageable); //重組返回結果 Map map = new HashMap(); map.put("invoice_amount", amount); map.put("list", invoiceList.getContent());//數(shù)據(jù)列表 map.put("total", invoiceList.getTotalElements());//記錄總條數(shù) map.put("current_page", invoiceList.getNumber());//當前頁碼 return map; }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
解決SpringBoot加載application.properties配置文件的坑
這篇文章主要介紹了SpringBoot加載application.properties配置文件的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08關于使用Mybatisplus自帶的selectById和insert方法時的一些問題
這篇文章主要介紹了關于使用Mybatisplus自帶的selectById和insert方法時的一些問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08SpringBoot中FailureAnalyzer的使用詳解
這篇文章主要介紹了SpringBoot中FailureAnalyzer的使用詳解,FailureAnalyzer攔截啟動時異常,將異常轉換成更加易讀的信息并包裝成org.springframework.boot.diagnostics.FailureAnalysis對象,監(jiān)控應用啟動過程,需要的朋友可以參考下2023-12-12使用java實現(xiàn)BBS論壇發(fā)送郵件過程詳解
這篇文章主要介紹了使用java發(fā)送郵件過程詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04