Handle optional parameters in QueryDSL(处理 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 中的可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:处理 QueryDSL 中的可选参数
基础教程推荐
- 多个组件的复杂布局 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 大摇大摆的枚举 2022-01-01
