1、判断一个字段是否为空,并作为条件进行查询时
一开始我是写成了
LambdaQueryWrapper<ProductCatalog> lambda = new LambdaQueryWrapper<>();
if(StringUtils.isBlank(productCatalog.getParentBIds)){
lambda.in(ProductCatalog::getParentBIds, "", null);
}
但是对数据库为NULL的值,判断不出来
然后看来SQL是有点问题
SELECT id,b_id,catalog_code,en_catalog_name,cn_catalog_name,parent_b_id,parent_b_ids,catalog_level,catalog_sequence,catalog_status,catalog_img_url,del_flag,create_user_id,create_time,update_user_id,update_time FROM product_catalog WHERE del_flag=0 AND parent_b_id IN (?,?) ORDER BY catalog_sequence ASC LIMIT 10
Parameters: (String), null
最终采用这种方法
// 临时作为判断条件进行是否需要这个参数
if (StringUtils.isBlank(productCatalog.getParentBId())) {
lambda.and(w ->
w.isNull(ProductCatalog::getParentBId).or().eq(ProductCatalog::getParentBId, ""));
}
打印的SQL如下
SELECT id,b_id,catalog_code,en_catalog_name,cn_catalog_name,parent_b_id,parent_b_ids,catalog_level,catalog_sequence,catalog_status,catalog_img_url,del_flag,create_user_id,create_time,update_user_id,update_time FROM product_catalog WHERE del_flag=0 AND ( parent_b_id IS NULL OR parent_b_id = ? ) ORDER BY catalog_sequence ASC LIMIT 10
Parameters: (String)