亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot+JPA?分頁查詢指定列并返回指定實體方式

 更新時間:2021年12月07日 10:59:17   作者:斯沃樂。  
這篇文章主要介紹了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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論