字符串判空问题

xiaoxiao2021-02-28  122

刚学到一招:

if (model.getTemplatePaperEntity().getId()==null || model.getTemplatePaperEntity().getId()==""){ templatePaperEntity.setId(UuidUtils.base58Uuid()); //添加模板表 result1=this.insert(templatePaperEntity); }else{ result1=this.updateById(templatePaperEntity); } 原来的代码是上面这样的,出现的问题就是给它传空值,也会走else语句,

修改后:

if ("".equals(model.getTemplatePaperEntity().getId())){ templatePaperEntity.setId(UuidUtils.base58Uuid()); //添加模板表 result1=this.insert(templatePaperEntity); }else{ result1=this.updateById(templatePaperEntity); } 原因:== 指的是地址,虽然是两个空字符串比较,但地址不一样;

           .equals比较的是值

plus:"".equals ()这样的写法可以避免空指针错误(感谢我钊指点)

小结:理论上肯定见过,用起来就忘啦,实践出真知,细节决定成败。

转载请注明原文地址: https://www.6miu.com/read-40332.html

最新回复(0)