处理QueryDSL中的可选参数
我在SpringData中使用QueryDSL。我说表,Employee
我创建了实体类,EmployeeEntity
我写了以下 服务 方法
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
。如何使用 QueryDSL 在可选参数 QueryDSL 使用Spring的数据?
谢谢 :)
回答:
BooleanBuilder
可以用作布尔表达式的动态生成器:
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中的可选参数 的全部内容, 来源链接: utcz.com/qa/398931.html