<!-- where 标签 -->
<!-- 只有在一个以上的if条件有值的情况下才去插入“WHERE”子句,若最后的内容是“AND”或“OR”开头,会将他们去除 -->
<select id="quaryByWhere" resultType="Student">
select * from student
<where>
<if test="name != '' and name != null">
and `name` like concat('%',#{name},'%')
</if>
</where>
</select>
<!-- where 1=1 -->
<!-- 有一个最大的弊端,就是导致数据表上的索引失效,如果有索引的话。而且还是一个垃圾条件 -->
<select id="quaryByWhere" resultType="Student">
select * from student
where 1=1
<if test="name != '' and name != null">
and `name` like concat('%',#{name},'%')
</if>
</select>
<!-- 等价where标签 -->
<!-- 通过自定义 trim 元素来定制我们想要的功能 -->
<trim prefix="WHERE" prefixOverrides="AND |OR "> ... </trim>
拼条件查询语句(where标签 , where 1=1 , 自定义 trim 元素)
于 2021-11-13 12:06:10 首次发布