if标签test为true时,才会执行语句,where标签自动判断是否为空,加减and、or
1.编写sql
<resultMap id="empMap" type="Employee">
<id property="id" column="id"/>
<result column="name" property="name"/>
</resultMap>
<select id="getEmpList" resultMap="empMap" parameterType="Employee">
SELECT id,name,birthday,dept,salary
FROM employee
<where>
<if test="name!=null">name=#{name}</if>
<if test="id!=null">and id=#{id}</if>
</where>
</select>
测试:



本文介绍MyBatis中IF与WHERE标签的使用技巧,展示如何通过IF标签实现条件判断,以及WHERE标签自动添加WHERE关键字并处理空值情况,避免SQL语法错误。
1万+

被折叠的 条评论
为什么被折叠?



