Mehrere Klausel Abfrage im Frühjahr Boot
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();
Ich versuche, die Suchabfrage für Produkte wie in verschiedenen E-Commerce-Websites verwendet zu implementieren (wie Flipkart, Amazon), in dem Benutzer verschiedene Attribute für das Produkt einfügt. Basierend auf dem Produktattribut werden Ergebnisse angezeigt. Für die Implementierung verwende ich Spring Boot und JPA als Datenbank.
Wie kann ich mehrere Klausel in einer einzelnen Abfrage dynamisch suchen. Im Falle einer SQL-Abfrage suchen wir beispielsweise mehrere Klauseln wie diese.
Wie kann ich mehrere Klausel in JPA implementieren.
Select * from table_name where attribute1="Attb" and attribute2="Attb2" and attribute3="Attb3";
Attb, Attb2 und Attb3 ändern sich dynamisch basierend auf der Clienteingabe.
Die einfache "vorbereitete Anweisung" und Feder unterstützt, dass aus der Box über '@ Param' und' @ Query'. Hast du sogar die Spring Data Docs gelesen? – Antoniossss
Ich habe Spring Data Docs überprüft, aber ich bin nicht in der Lage, dynamische Abfrage für verschiedene Attribute zu schreiben. Der Benutzer kann mehrere Attribute einfügen, wie zum Beispiel (Erstbenutzer klickt nur Attribut1, Zweitmal Attribut1 und Attribut2 und so weiter). Wie schreibe ich eine Abfrage für diese Art von Problemen. Danke im Voraus. – Gopal
"JPA" ist keine Datenbank. JPA verwendet JPQL und es gibt reichlich Hinweise darauf im Internet, also bitte sagen Sie, was Sie versucht haben. JPQL erfordert KLASSEN, und Sie präsentieren keine Klassen –