业务中经常会有判空处理,一次忘记判空就应该一次祭天。
如下代码被注释掉的时bug代码,由于初学λ表达式,才疏学浅,写的判空代码不敢用,却直接忽略判空处理。
.filter(allrrule -> //owner 判空处理 StringUtils.isNotEmpty(allrrule.getOwnerId()) ? StringUtils.equals(allrrule.getOwnerId(),fileBisRule.getOwnerId()) : true) // {return StringUtils.equals(allrrule.getOwnerId(),fileBisRule.getOwnerId());}) .collect(Collectors.toList());
加上对owner的判空修复了该bug,但是我还是不太放心,可以看下图:
也就是说StringUtils.isNotEmpty不会防住length(owner_id) = 0的数据,那用户一定会来和理论。
isNotEmpty的结果是7条数据;isNotBlank的结果是8条数据。
1、如何解决这个问题呢?
isNotBlank(str)
isNotBlank(str) 等价于 isNotEmpty && str.trim().length > 0 ( isNotEmpty 等价于 str!=null && str.length() > 0 )
参考:https://www.cnblogs.com/dixinyunpan/p/6088612.html
2、那为什么会存下来这种数据呢?
通过问询用户,其是用excel导入的,由此可见:Excel是对导入的数据做了处理,把为NULL的字段都转成了空串。微软为什么会这么做呢?
通过查询资料发现:
1:空值('')是不占用空间的
2: MySQL中的NULL其实是占用空间的。官方文档说明:
“NULL columns require additional space in the row to record whether their values are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.”
那这是如何实现把NULL ->''的呢?
Mysql是这样
Oracle是这样:
-----------------------------2020年3月10日09:48:15----------------------
需要注意的是在oracle 中null 和空串是同等的,至少在查询 is null 这个条件下是等同的。