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根據客戶端輸入動態變化。
這是簡單的「準備好的聲明」和春季支持開箱即通過'@ Param'和'@ Query'的。你甚至檢查過Spring Data Docs嗎? – Antoniossss
我已經選中了spring data文檔,但是我無法爲不同的屬性編寫動態查詢。用戶可以插入多個屬性,如(第一次用戶只點擊屬性1,第二次屬性1和屬性2等)。如何爲這些類型的問題編寫查詢。提前致謝。 – Gopal
「JPA」不是數據庫。 JPA使用JPQL,並且在網絡上有充足的參考,所以請說出你所嘗試過的。 JPQL需要CLASSES,並且您沒有提供任何可以看到的課程 –