在MyBatis中,可以使用 <if>
標(biāo)簽來(lái)進(jìn)行條件判斷,根據(jù)條件來(lái)動(dòng)態(tài)生成 SQL 語(yǔ)句。示例如下:
<select id="selectUserByCondition" parameterType="map" resultType="User">
SELECT * FROM user
<where>
<if test="id != null">
AND id = #{id}
</if>
<if test="username != null">
AND username = #{username}
</if>
<if test="status != null">
AND status = #{status}
</if>
</where>
</select>
在上面的示例中,根據(jù)傳入的參數(shù)動(dòng)態(tài)生成了查詢條件,如果傳入的參數(shù)不為空,就添加相應(yīng)的條件到 SQL 語(yǔ)句中。這樣可以根據(jù)不同的條件進(jìn)行靈活的查詢操作。