Mybatis中 SQL 语句中的 where 1=1 冷不防 2024-04-17 15:45 11阅读 0赞 我们在使用 Mybatis 框架编写 SQL 语句时,会发现这个现象 <select id="findAll" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from t_people where 1=1 <if test="bookName != null"> and bookName = #{bookName} </if> <if test="author != null"> and author = #{author} </if> </select> 发现这个 select 查询语句中存在一条这样的语句 where 1=1 * select \* from t1 where 1=1; * \-- 实际等效于select \* from t1 where true; * \-- 语句将返回t1中所有的记录行 ### where 1=1 是为了避免 where 关键字后面的第一个词直接就是 “and”而导致语法错误。 ### 什么你还问我为什么不是 2=2,1+2=3? 只不过1=1的运算开销更小,故被应用的最普遍。
还没有评论,来说两句吧...