处理QueryDSL中的可选参数 [英] Handle optional parameters in QueryDSL
本文介绍了处理QueryDSL中的可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在SpringData中使用QueryDSL。
我有表说,员工
我已经创建了实体类说, EmployeeEntity
I写了以下服务方法
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数据在 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中的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文