文章目录
要求: 构建sql wrapper
java
根据要求构建sql wrapper:
dto.getProjectId() == null,则 where project_id is null
dto.getProjectId() != null,则 where project_id ='58b1fad6-adaa-4834-8337-8d438346783a'
错误写法
java
PropertyEntity property = propertyManager.getOne(
Wrappers.<PropertyEntity>lambdaQuery()
.eq(Objects.nonNull(dto.getProjectId()), PropertyEntity::getProjectId, dto.getProjectId())
.eq(StringUtils.isNotEmpty(dto.getCode()), PropertyEntity::getCode, dto.getCode())
.eq(StringUtils.isNotEmpty(dto.getName()), PropertyEntity::getName, dto.getName()));
原因: dto.getProjectId() == null,构建的sql是:where project_id = 'null'
解决方案
java
LambdaQueryWrapper<PropertyEntity> wrapper = Wrappers.lambdaQuery(PropertyEntity.class);
if (dto.getProjectId() == null) {
wrapper.isNull(PropertyEntity::getProjectId);
} else {
wrapper.eq(PropertyEntity::getProjectId, dto.getProjectId());
}
wrapper.eq(StringUtils.isNotEmpty(dto.getCode()), PropertyEntity::getCode, dto.getCode())
.eq(StringUtils.isNotEmpty(dto.getName()), PropertyEntity::getName, dto.getName());
PropertyEntity property = propertyManager.getOne(wrapper);