1. 程式人生 > >Spring Data JPA 二:實現多表關聯分頁查詢

Spring Data JPA 二:實現多表關聯分頁查詢

最近在對JPA的使用過程中發現對於單表的操作很是方便,但是當設計到多表聯查的時候就需要有一些特殊的操作了。

專案中有一個場景是後臺需要做一個分頁的列表查詢,所需要的資料分散在兩張表中,如果是用mybatis的話直接定義resultMap,然後手寫SQL就可以了。而在JPA中就需要用到JPQL了。

首先定義一下各個物件之間的關係

實體 GxOrderDO :訂單。

實體 GxOrderDetailDO:訂單詳情。

實體 GxOrderInfoRsp:分裝的分頁返回物件。

其中的GxOrderDO和GxOrderDetailDO通過orderId欄位做兩張表的關聯。

建立一個物件用來封裝返回的結果集:

GxOrderInfoRsp.java:

public class GxOrderInfoRsp implements Serializable{
    /**
     * 訂單ID
     */
    private String orderId;
    /**
     * 訂單狀態
     */
    private Integer orderStatus;
    /**
     * 訂單型別
     */
    private Integer orderType;
    /**
     * 訂單支付金額
     */
    private BigDecimal orderAmount;
    /**
     * 訂單渠道(裝置)型別 
     */
    private Integer orderChannel;

    public GxOrderInfoRsp(String orderId, Integer orderStatus, Integer orderType,         
                          BigDecimal orderAmount, Integer orderChannel) {
        this.orderId = orderId;
        this.orderStatus = orderStatus;
        this.orderType = orderType;
        this.orderAmount = orderAmount;
        this.orderChannel = orderChannel;
        
    }

Repository:

@Query("SELECT new com.app.domain.GxOrderInfoRsp(r.orderId, r.orderStatus, r.orderType, rd.orderAmount, rd.orderChannel)" +
        " FROM GxOrderDO r, GxOrderDetailDO rd WHERE r.orderId = rd.orderId and ( r.orderId=:#{#customer.orderId} or :#{#customer.orderId} is null)   ")
Page<GxOrderInfoRsp>  findOrderPage(@Param("customer") GxOrderDO customer,Pageable pageable);

注意:@Query中new的物件的引數順序要和構造方法的引數順序一致

r.orderId=:#{#customer.orderId} or :#{#customer.orderId} is null) 
這種是做個非空判斷,如果是空,就跳過這個條件,不執行。
這裡面用到了Spring Data JPA @Query定義中的SpEL特性

Service:

Pageable pageable = new PageRequest(0,3, Sort.Direction.DESC,"createTime");

 

文章參考:

https://spring.io/blog/2014/07/15/spel-support-in-spring-data-jpa-query-definitions

https://blog.csdn.net/moshowgame/article/details/80672617