我们都知道Mybatis的
- <select id="selectSelective" resultType="com.study.entity.User">
- select * from t_user
- <where>
- <if test="name != null and name != ''">
- and name = #{name}
- if>
- <if test="sex != null and sex != ''">
- and sex = #{sex}
- if>
- where>
- select>
如果没有
select * from t_user where and name = 'CLAY';
很明显看出多了一个and,那么加上
那我们明白了这个前提,我们再看下一个SQL:
- <select id="selectSelective" resultType="com.study.entity.User">
- select * from t_user
- <where>
- del_flag = '0'
- <if test="name != null and name != ''">
- and name = #{name}
- if>
- <if test="sex != null and sex != ''">
- and sex = #{sex}
- if>
- where>
- select>
那按我们刚才说的,
select * from t_user where del_flag = '0' name = 'CLAY';
但其实结果并没有去掉这个and,还是正常的:
select * from t_user where del_flag = '0' and name = 'CLAY';
这是因为
我们再看看官网怎么说: