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标签的用法可以参考官方文档。