看似不重要的一句话其实可以省略好多代码
例如String sql=" select * from student where 1=1";
if (name!=null) sql+="and name like "%%"";
if (age!=null) sql+="and age like "%%"";
如果没有使用where 1=1;
String sql=" select * from student";
if (name!=null) sql+="where name like "%%"";
if(name==null and age!=null) sql+="where age like "%%"";
if (age!=null) sql+="and age like "%%"";
两个字段的非空可能都要判断的,逻辑思维比较杂乱,所以用上where 1=1 就可以解决很多问题的。

本文介绍了一种简化SQL查询构造的方法,通过使用'where 1=1'作为条件的基础部分,可以有效地减少代码量并简化逻辑判断过程。这种方法尤其适用于需要根据多个参数动态构建查询条件的场景。
2028

被折叠的 条评论
为什么被折叠?



