本文介绍了处理 QueryDSL 中的可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
我正在使用带有 SpringData 的 QueryDSL.我有表说,Employee
,我创建了实体类说,EmployeeEntity
我写了以下 service 方法
I am using QueryDSL with SpringData.
I have Table say, Employee
and I have created entity class say, EmployeeEntity
I have written following service method
public EmployeeEntity getEmployees(String firstName, String lastName)
{
QEmployeeEntity employee = QEmployeeEntity.employeeEntity;
BooleanExpression query = null;
if(firstName != null)
{
query = employee.firstName.eq(firstName);
}
if(lastName != null)
{
query = query.and(employee.lastName.eq(lastName)); // NPException if firstName is null as query will be NULL
}
return empployeeDAO.findAll(query);
}
如上所述,我注释了 NPException
.如何使用 Spring Data 将 QueryDSL 用于 QueryDSL 中的可选参数?
As in above I commented the NPException
. How to use QueryDSL for optional Parameters in QueryDSL using Spring Data?
谢谢你:)
推荐答案
BooleanBuilder
可以用作布尔表达式的动态构建器:
BooleanBuilder
can be used as a dynamic builder for boolean expressions:
public EmployeeEntity getEmployees(String firstName, String lastName) {
QEmployeeEntity employee = QEmployeeEntity.employeeEntity;
BooleanBuilder where = new BooleanBuilder();
if (firstName != null) {
where.and(employee.firstName.eq(firstName));
}
if (lastName != null) {
where.and(employee.lastName.eq(lastName));
}
return empployeeDAO.findAll(where);
}
这篇关于处理 QueryDSL 中的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!