SpringBoot+JPA?分頁(yè)查詢指定列并返回指定實(shí)體方式
SpringBoot JPA分頁(yè)查詢指定列并返回指定實(shí)體
用習(xí)慣Mybatis,沒用過(guò)jpa 真是各種踩坑了
腦殼疼,一個(gè)分頁(yè)弄老半天,原來(lái)就一句話的事情,唉
先來(lái)說(shuō)說(shuō)正常的JPA如何操作
實(shí)體類對(duì)應(yīng)表來(lái)創(chuàng)建,舉個(gè)例子
@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; /** * 用戶所屬平臺(tái) */ @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方法省略 */
相對(duì)應(yīng)的建立操作接口
@Repository public interface TdUserRepository extends JpaRepository<TdUser, Long>, JpaSpecificationExecutor<TdUser> { }
分頁(yè)查詢的時(shí)候只要一句話
// Partner partner 外部傳入的分頁(yè)信息 Page<TdUser> allPage = TdUserRepository.findAll(pageable);
但是有時(shí)候可能不需要返回所有字段,只要返回一部分而已,經(jīng)過(guò)各種嘗試,有一種最簡(jiǎn)單的方法
就是把想要返回的字段再構(gòu)建成一個(gè)實(shí)體,實(shí)體的屬性需要和數(shù)據(jù)庫(kù)字段進(jìn)行映射,然后單獨(dú)寫一個(gè)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)用的時(shí)候
// Partner partner 外部傳入的分頁(yè)信息 Page<User> allPage = UserRepository.findAll(pageable);
我一開始也嘗試過(guò)寫sql,但是我發(fā)現(xiàn)返回?cái)?shù)據(jù)會(huì)變成Page<Object[]>,經(jīng)過(guò)json數(shù)列化以后數(shù)據(jù)會(huì)變成 [ ["1":"string"],...]這種樣子而不是key-value的形式
改了n遍后發(fā)現(xiàn)這樣是最簡(jiǎn)單的。。。
SpringBoot JPA實(shí)現(xiàn)自定義語(yǔ)句分頁(yè)查詢
例:
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.服務(wù)層
@Override public Map findInvoice(int companyID, String status, Integer page, Integer pageSize) { Double amount = companyFinanceRepository.findDCompanyFinance(companyID); //分頁(yè)查詢 Pageable pageable = PageRequest.of(page, pageSize, Sort.Direction.ASC, "id"); Page<Map> invoiceList = invoiceRepository.findInvoice(companyID, status, pageable); //重組返回結(jié)果 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());//當(dāng)前頁(yè)碼 return map; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決SpringBoot加載application.properties配置文件的坑
這篇文章主要介紹了SpringBoot加載application.properties配置文件的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08淺談Springboot下引入mybatis遇到的坑點(diǎn)
這篇文章主要介紹了Springboot下引入mybatis遇到的坑點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08SpringMVC實(shí)現(xiàn)文件上傳和下載功能
這篇文章主要為大家詳細(xì)介紹了SpringMVC實(shí)現(xiàn)文件上傳和下載功能 ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08關(guān)于使用Mybatisplus自帶的selectById和insert方法時(shí)的一些問(wèn)題
這篇文章主要介紹了關(guān)于使用Mybatisplus自帶的selectById和insert方法時(shí)的一些問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08SpringBoot中FailureAnalyzer的使用詳解
這篇文章主要介紹了SpringBoot中FailureAnalyzer的使用詳解,FailureAnalyzer攔截啟動(dòng)時(shí)異常,將異常轉(zhuǎn)換成更加易讀的信息并包裝成org.springframework.boot.diagnostics.FailureAnalysis對(duì)象,監(jiān)控應(yīng)用啟動(dòng)過(guò)程,需要的朋友可以參考下2023-12-12使用java實(shí)現(xiàn)BBS論壇發(fā)送郵件過(guò)程詳解
這篇文章主要介紹了使用java發(fā)送郵件過(guò)程詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04