2017-08-19 50 views
-1

多條查詢

package com.mobile.model; 
 

 
import javax.persistence.*; 
 

 

 
@Entity 
 
@Table(name = "mobile_table") 
 
public class Mobile { 
 

 
    @Id 
 
    @GeneratedValue(strategy = GenerationType.SEQUENCE) 
 
    public int mobileId; 
 

 
    @Column(name = "mobile_name",nullable = false,length = 20) 
 
    public String mobileName; 
 

 
    @Column(name = "mobile_brand",nullable = false,length = 15) 
 
    public String mobileBrand; 
 

 
    @Column(name="mobile_networkType") 
 
    public String mobileNetworkType; 
 

 
    @Column(name = "mobile_core") 
 
    public Integer mobileAvailableCore; 
 

 
    @Column(name = "mobile_ram") 
 
    public Integer mobileRAMSize; 
 

 
    @Column(name = "mobile_os") 
 
    public String mobileOS; 
 

 
    @Column(name = "mobile_wifif") 
 
    public Boolean mobileWifiAvailability; 
 

 
    @Column(name = "mobile_bluetooth") 
 
    public Boolean mobileBluethoothAvailablity; 
 

 
    public Mobile() { 
 
    } 
 

 
    public Mobile(String mobileName, String mobileBrand, String mobileNetworkType, Integer mobileAvailableCore, Integer mobileRAMSize, String mobileOS, Boolean mobileWifiAvailability, Boolean mobileBluethoothAvailablity) { 
 
     this.mobileName = mobileName; 
 
     this.mobileBrand = mobileBrand; 
 
     this.mobileNetworkType = mobileNetworkType; 
 
     this.mobileAvailableCore = mobileAvailableCore; 
 
     this.mobileRAMSize = mobileRAMSize; 
 
     this.mobileOS = mobileOS; 
 
     this.mobileWifiAvailability = mobileWifiAvailability; 
 
     this.mobileBluethoothAvailablity = mobileBluethoothAvailablity; 
 
    } 
 
}

//Query for multiple selection 
 
    @SuppressWarnings("unchecked") 
 
\t public List<Mobile> getMobileSearch(String mobileBrand, Integer mobileRAMSize, String mobileOS){ 
 
    \t return mobileDao.findAll(Specifications.where((Specification<Mobile>) getAllMobilesByBrand(mobileBrand)) 
 
    \t \t \t .and((Specification<Mobile>) getAllMobilesByOS(mobileOS)) 
 
    \t \t \t .and((Specification<Mobile>) getAllMobilesByRam(mobileRAMSize))); 
 
    }

// calling function for multiple attribute Search in controller class 
 
@RequestMapping(value="/searchMobile", method=RequestMethod.GET) 
 
    public ResponseEntity<List<Mobile>> searchMobile(@RequestParam(value="brand") String mobileBrand, @RequestParam(value="ramSize") Integer ramSize, @RequestParam(value="mobileOs") String MobileOs) 
 
    { 
 
    \t List<Mobile> mobileList=mobileService.getMobileSearch(mobileBrand,ramSize,MobileOs); 
 
    \t return new ResponseEntity<List<Mobile>>(mobileList,HttpStatus.OK); 
 
    }

// here is how my database is calling database query. 
 
@Repository 
 
public interface MobileDao extends CrudRepository<Mobile, Integer>, JpaSpecificationExecutor<Mobile> { 
 

 
    public List<Mobile> findByMobileBrand(String mobileBrand); 
 

 
    @Query("select distinct m.mobileBrand from Mobile m") 
 
    public List<String > getAllDistinctBrand(); 
 

 
    public List<Mobile> findByMobileRAMSize(Integer mobileRAMSize); 
 

 
    @Query("select distinct m.mobileRAMSize from Mobile m") 
 
    public List<Integer> getAllDistinctRam(); 
 

 
    public List<Mobile> findByMobileOS(String mobileOS); 
 

 

 
    @Query("select distinct m.mobileOS from Mobile m") 
 
    public List<String > getAllDistinctMobileOS();

我想實現對產品的搜索查詢就像在不同的電子商務網站使用(如Flipkart,亞馬遜),其中用戶插入的產品不同的屬性。根據產品屬性顯示結果。對於我使用Spring Boot和JPA作爲數據庫的實現。

如何動態地在單個查詢中搜索多個子句。例如,在SQL查詢的情況下,我們像這樣搜索多個子句。

我如何在JPA中實現多個子句。

Select * from table_name where attribute1="Attb" and attribute2="Attb2" and attribute3="Attb3"; 

Attb,Attb2和Attb3根據客戶端輸入動態變化。

+0

這是簡單的「準備好的聲明」和春季支持開箱即通過'@ Param'和'@ Query'的。你甚至檢查過Spring Data Docs嗎? – Antoniossss

+0

我已經選中了spring data文檔,但是我無法爲不同的屬性編寫動態查詢。用戶可以插入多個屬性,如(第一次用戶只點擊屬性1,第二次屬性1和屬性2等)。如何爲這些類型的問題編寫查詢。提前致謝。 – Gopal

+0

「JPA」不是數據庫。 JPA使用JPQL,並且在網絡上有充足的參考,所以請說出你所嘗試過的。 JPQL需要CLASSES,並且您沒有提供任何可以看到的課程 –

回答

0

你應該實現一個JPA存儲庫和使用方法查詢:

public interface MyRepository implements JpaRepository<MODEL_CLASS, Long> { 
    MODEL_CLASS findAllByAtttibute1AndAttribute2AndAttribute3(String Attribute1, String Attribute2, String Attribute3); 
} 
+0

使用Crietria API,我將不得不編寫Attribute的所有可能組合。就像用戶想要僅通過一個屬性或多個屬性進行搜索一樣,那麼我們如何編寫動態查詢。 – Gopal

+0

@Gopal使用'Criteria API' – Antoniossss

0

您的問題沒有明確的足夠多。如果您需要使用可變數量的條件,則必須使用Criteria API。它允許您有條件地創建謂詞列表並將其用於數據提取。 http://www.objectdb.com/java/jpa/query/criteria

它看起來是這樣的:

myQueryMethod(UserChoices choices){ 
     if(choices.someChoice1){ 
     predicates.add(somePredicate) 
     } 
    .....