【问题标题】:Multiple clause query in spring bootspring boot中的多子句查询
【发布时间】:2018-01-28 06:44:00
【问题描述】:

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")
	public List<Mobile> getMobileSearch(String mobileBrand, Integer mobileRAMSize, String mobileOS){
    	return mobileDao.findAll(Specifications.where((Specification<Mobile>) getAllMobilesByBrand(mobileBrand))
    			.and((Specification<Mobile>) getAllMobilesByOS(mobileOS))
    			.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)
    {
    	List<Mobile> mobileList=mobileService.getMobileSearch(mobileBrand,ramSize,MobileOs);
    	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 根据客户端输入动态变化。

【问题讨论】:

  • 这是简单的“准备好的声明”,spring 通过@Param@Query 支持开箱即用。你甚至检查过 Spring Data Docs 吗?
  • 我已经检查了 spring 数据文档,但我无法为不同的属性编写动态查询。用户可以插入多个属性,例如(第一次用户只点击属性 1,第二次点击属性 1 和属性 2 等等)。如何为这些类型的问题编写查询。提前致谢。
  • "JPA" 不是数据库。 JPA 使用 JPQL,并且在网络上有大量的参考,所以请说出您尝试过的内容。 JPQL 需要 CLASSES,而您不提供任何课程
  • link 您可以在网站用户点击复选框的侧边栏中看到,它会显示基于此的结果。我必须实现类似的。
  • “你试过的”是指什么代码。什么 JPQL。

标签: java mysql spring-boot spring-data-jpa jpql


【解决方案1】:

你的问题不够清楚。如果您需要使用可变数量的条件,则必须使用Criteria API。它允许您有条件地创建谓词列表并将其用于数据提取。 http://www.objectdb.com/java/jpa/query/criteria

看起来像这样:

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

【讨论】:

    【解决方案2】:

    您应该实现一个 JPA 存储库并使用方法查询:

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

    【讨论】:

    • 通过使用这种方法,我将不得不编写所有可能的属性组合。比如如果用户只想通过一个属性或多个属性进行搜索,那么我们如何编写动态查询。
    • @Gopal 使用 Criteria API
    猜你喜欢
    • 2018-10-29
    • 2020-03-29
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多