Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring data jpa 如何根据用户从HTML表单提交的条件动态生成select查询_Spring Data Jpa_Jpql - Fatal编程技术网

Spring data jpa 如何根据用户从HTML表单提交的条件动态生成select查询

Spring data jpa 如何根据用户从HTML表单提交的条件动态生成select查询,spring-data-jpa,jpql,Spring Data Jpa,Jpql,我想根据用户可以通过HTML表单指定的各种条件执行SELECT查询。查询中的每个条件都映射到HTML表单中的适当字段。如果没有为字段指定值,则可以在查询中跳过相应的条件 完整格式的查询如下所示: select d from TotalInventoryTransaction d where d.transactionDate between ?1 and ?2 and d.material=?3 and d.shipmentOrder between ?4 and ?5 and d.

我想根据用户可以通过HTML表单指定的各种条件执行
SELECT
查询。查询中的每个条件都映射到HTML表单中的适当字段。如果没有为字段指定值,则可以在查询中跳过相应的条件

完整格式的查询如下所示:

select
  d
from
  TotalInventoryTransaction d
where
  d.transactionDate between ?1 and ?2
and d.material=?3
and d.shipmentOrder between ?4 and ?5
and d.source = ?6
and d.destination= ?7
Spring数据JPA存储库方法如下:

@Query(FILTER_DATA_QUERY)
List<TotalInventoryTransaction> getFilteredData(Date dateFrom
                                                , Date dateTo
                                                , Long material
                                                , Long stoFrom
                                                , Long stoTo
                                                , String source
                                                , String destination);
@Query(筛选数据查询)
列表getFilteredData(日期从
,日期至
,长物料
,久违
,长斯托托
,字符串源
,字符串目的地);
我希望将
FILTER\u DATA\u QUERY
传递给此方法,如果未指定方法参数,则在执行的查询中跳过其相应的子句


请建议如何实现这一点。

您只需确保在提供参数值时,查询会考虑该值,如果未指定该值,则会考虑相应的列值

以下硬编码JPA查询将起作用:

select
  d
from
  TotalInventoryTransaction d
where
  d.transactionDate >= coalesce(?1, d.transactionDate)
and d.transactionDate <= coalesce(?2, d.transactionDate)
and d.material = coalesce(?3, material)
and d.shipmentOrder >= coalesce(?4, d.shipmentOrder)
and d.shipmentOrder <= coalesce(?5, d.shipmentOrder)
and d.source = coalesce(?6, d.source)
and d.destination = coalesce(?7, d.destination)
选择
D
从…起
总目录事务d
哪里
d、 transactionDate>=合并(?1,d.transactionDate)
d.transactionDate=合并(?4,d.Shipmenter)

和d.shipmenterder您只需确保在提供参数值时,查询会考虑该值,而在未指定该值时,会考虑相应的列值

以下硬编码JPA查询将起作用:

select
  d
from
  TotalInventoryTransaction d
where
  d.transactionDate >= coalesce(?1, d.transactionDate)
and d.transactionDate <= coalesce(?2, d.transactionDate)
and d.material = coalesce(?3, material)
and d.shipmentOrder >= coalesce(?4, d.shipmentOrder)
and d.shipmentOrder <= coalesce(?5, d.shipmentOrder)
and d.source = coalesce(?6, d.source)
and d.destination = coalesce(?7, d.destination)
选择
D
从…起
总目录事务d
哪里
d、 transactionDate>=合并(?1,d.transactionDate)
d.transactionDate=合并(?4,d.Shipmenter)

如果您使用的是SpringDataJPA,我建议您使用CriteriaAPI。查看可以使用谓词创建动态where子句的文档。我创建了一个示例来向您展示它的用法

表:-

@Entity
public class Test {
    @Id private Long id;    
    String attributeOne;
    String attributeTwo;
    ...
}
实际查询

//Params from the method
String param1 = "1";
String paramNull = null;

CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery cq = qb.createQuery();
Root<Test> variableName = cq.from(Test.class);

//Creating Predicates
List<Predicate> predicates = new ArrayList<Predicate>();

//Adding predicates when params are not null
if (param1 != null) {
    predicates.add(
            qb.equal(variableName.get("attributeOne"), param1));
}
if (paramNull != null) {
    predicates.add(
            qb.equal(variableName.get("attributeTwo"), paramNull));
}
//Actual query
cq.select(variableName)
        .where(predicates.toArray(new Predicate[]{}));
//executing the query
em.createQuery(cq).getResultList();
//方法中的参数
字符串param1=“1”;
字符串paramNull=null;
CriteriaBuilder qb=em.getCriteriaBuilder();
CriteriaQuery cq=qb.createQuery();
根变量名=cq.from(Test.class);
//创建谓词
列表谓词=新的ArrayList();
//在参数不为null时添加谓词
if(param1!=null){
谓词.add(
qb.equal(variableName.get(“attributeOne”),param1));
}
if(paramNull!=null){
谓词.add(
相等(variableName.get(“attributewo”),paramNull);
}
//实际查询
cq.select(变量名称)
.where(谓词.toArray(新谓词[]{}));
//执行查询
em.createQuery(cq.getResultList();

希望这有帮助。

如果您使用的是Spring data Jpa,我建议您使用criteria API。查看可以使用谓词创建动态where子句的文档。我创建了一个示例来向您展示它的用法

I am now building a query statement by using string buffer according to condition it is working.. if value is not null then I am appending it into the query else I am not appending..
one section is like this..

StringBuffer queryBuff = new StringBuffer();
        queryBuff
                .append("select * from table_name where");
try {
            if ((dateFromStr != null && dateFromStr != "")
                    && (dateToStr != null && dateToStr != "")) {
                dateFrom = df.parse(dateFromStr);
                dateTo = df.parse(dateToStr);
                queryBuff.append(" transaction_date between " + "\'" + dateFrom
                        + "\'" + "and" + "\'" + dateTo + "\'");
            } else {
                queryBuff.append("");
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
if (materialName != null && materialName != "") {

            if ((dateFromStr == null || dateFromStr == "")
                    && (dateToStr == null || dateToStr == ""))

            {
                queryBuff.append(" material=" + "\'" + materialName + "\'");

            } else {
                queryBuff.append(" and ");
                queryBuff.append(" material=" + "\'" + materialName + "\'");
            }

        }
表:-

@Entity
public class Test {
    @Id private Long id;    
    String attributeOne;
    String attributeTwo;
    ...
}
实际查询

//Params from the method
String param1 = "1";
String paramNull = null;

CriteriaBuilder qb = em.getCriteriaBuilder();
CriteriaQuery cq = qb.createQuery();
Root<Test> variableName = cq.from(Test.class);

//Creating Predicates
List<Predicate> predicates = new ArrayList<Predicate>();

//Adding predicates when params are not null
if (param1 != null) {
    predicates.add(
            qb.equal(variableName.get("attributeOne"), param1));
}
if (paramNull != null) {
    predicates.add(
            qb.equal(variableName.get("attributeTwo"), paramNull));
}
//Actual query
cq.select(variableName)
        .where(predicates.toArray(new Predicate[]{}));
//executing the query
em.createQuery(cq).getResultList();
//方法中的参数
字符串param1=“1”;
字符串paramNull=null;
CriteriaBuilder qb=em.getCriteriaBuilder();
CriteriaQuery cq=qb.createQuery();
根变量名=cq.from(Test.class);
//创建谓词
列表谓词=新的ArrayList();
//在参数不为null时添加谓词
if(param1!=null){
谓词.add(
qb.equal(variableName.get(“attributeOne”),param1));
}
if(paramNull!=null){
谓词.add(
相等(variableName.get(“attributewo”),paramNull);
}
//实际查询
cq.select(变量名称)
.where(谓词.toArray(新谓词[]{}));
//执行查询
em.createQuery(cq.getResultList();

希望这能有所帮助。

您是说如果
材料
参数的值为
null
,则应跳过查询子句
和d.material=-3
(因为未指定任何材料),依此类推?是的………您是说如果
材料
参数的值为
null
,应跳过查询子句
和d.material=?3
(因为未指定任何物料),依此类推?是。。。。。。。。。。。。
I am now building a query statement by using string buffer according to condition it is working.. if value is not null then I am appending it into the query else I am not appending..
one section is like this..

StringBuffer queryBuff = new StringBuffer();
        queryBuff
                .append("select * from table_name where");
try {
            if ((dateFromStr != null && dateFromStr != "")
                    && (dateToStr != null && dateToStr != "")) {
                dateFrom = df.parse(dateFromStr);
                dateTo = df.parse(dateToStr);
                queryBuff.append(" transaction_date between " + "\'" + dateFrom
                        + "\'" + "and" + "\'" + dateTo + "\'");
            } else {
                queryBuff.append("");
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
if (materialName != null && materialName != "") {

            if ((dateFromStr == null || dateFromStr == "")
                    && (dateToStr == null || dateToStr == ""))

            {
                queryBuff.append(" material=" + "\'" + materialName + "\'");

            } else {
                queryBuff.append(" and ");
                queryBuff.append(" material=" + "\'" + materialName + "\'");
            }

        }