1,where1=1
1)where 1=1 是为True的语句,语句语法正确,能够被正确执行,它的作用相当于:”select * from table”,即返回表中所有数据。
2)在多条件查询中,减少if判断
只是为了满足多条件查询页面中不确定的各种因素而采用的一种构造一条正确能运行的动态SQL语句的一种方法。
有where1=1
<select id="listTalentScreen" parameterType="Map" resultMap="BeanResult">
select a.* from bean a where 1=1
<if test="ccc!=null and ccc!='' ">
and a.ccc in (#{ccc})
</if>
<if test="bbb!=null and bbb!='' ">
and a.bbb in (#{bbb})
</if>
</select>
没有where 1==1
<select id="listTalentScreen" parameterType="Map" resultMap="BeanResult">
select a.* from bean a
<if test="ccc==null and ccc=='' " || bbb==null and bbb=='' ">
order by a.id desc
</if>
<if test="ccc!=null and ccc!='' " || bbb==null and bbb=='' ">
where a.ccc in (#{ccc})
order by a.id desc
</if>
<if test="ccc!=null and ccc!='' " || bbb!=null and bbb!='' ">
where a.ccc in (#{ccc}) and a.bbb in (#{bbb})
order by a.id desc
</if>
......
.......
......
</select>
</select>
2.where1=0
where 1=0; 条件始终为false,结果不会返回任何数据,只有表结构,可用于快速建表。
因为只读取表的结构而不考虑表中的数据,也就不用保存结果集,所以起到节省了内存的作用。
select a.* from bean a where 1=0;