1.mybatis中动态sql编写的场景
1.1 在实现多条件查询但是不确定其中的哪些条件不需要执行的情况
1.2 在根据传入的数组条件进行查询数据的时候
2.示例
2.1 在需要多条件查询时,可以使用mybatis的 if 标签自动判断是否添加该条件查询
<select id="queryByCondition" parameterType="com.zfj.entity.vo.StudentConditionVo" resultMap="map1">
select * from student
<where>
<if test="name!=null and name !=''">
and name=#{name}
</if>
<if test="gender!=null and gender!=''">
and gender=#{gender}
</if>
<if test="telephone!=null and telephone!=''">
and telephone=#{telephone}
</if>
<if test="minAge!=0 and maxAge==0">
and age>#{minAge}
</if>
<if test="minAge==0 and maxAge!=0">
and age<#{maxAge}
</if>
<if test="minAge!=0 and maxAge!=0">
and age between(#{minAge},#{maxAge})
</if>
</where>
</select>
ps: where标签可以取代sql语句中的where 1=1
if 标签中的test属性是用来判断输入的值是否为空
2.2 在根据传入的数组条件进行查询数据的时候
<select id="queryByIds" parameterType="com.zfj.entity.vo.SidesVo" resultType="Student">
select * from student
<foreach collection="ids" item="id" open="where id in (" close=")" separator=",">
#{id}
</foreach>
</select>