MyBatis的動態SQL標簽可以幫助我們在SQL語句中根據條件來動態生成不同的SQL片段,從而實現更靈活的查詢。
下面是一些MyBatis動態SQL標簽的使用示例:
<select id="selectUsers" resultType="User">
SELECT id, username, email
FROM users
<where>
<if test="username != null">
AND username = #{username}
</if>
<if test="email != null">
AND email = #{email}
</if>
</where>
</select>
<select id="selectUsers" resultType="User">
SELECT id, username, email
FROM users
<where>
<choose>
<when test="username != null">
AND username = #{username}
</when>
<when test="email != null">
AND email = #{email}
</when>
<otherwise>
AND id > 0
</otherwise>
</choose>
</where>
</select>
<select id="selectUsersByIds" resultType="User">
SELECT id, username, email
FROM users
WHERE id IN
<foreach collection="ids" item="id" index="index" open="(" separator="," close=")">
#{id}
</foreach>
</select>
這些是MyBatis中常用的動態SQL標簽,通過它們可以輕松實現靈活的SQL查詢。更多關于MyBatis動態SQL標簽的用法可以參考官方文檔。