MyBatis的XML文件支持动态SQL的方式有以下几种:
<select id="selectUsers" resultType="User">
SELECT * FROM users
<where>
<if test="username != null">
AND username = #{username}
</if>
</where>
</select>
<select id="selectUsers" resultType="User">
SELECT * FROM users
<where>
<choose>
<when test="username != null">
AND username = #{username}
</when>
<otherwise>
AND id = #{id}
</otherwise>
</choose>
</where>
</select>
<select id="selectUsers" resultType="User">
SELECT * FROM users
WHERE id IN
<foreach item="id" collection="ids" open="(" separator="," close=")">
#{id}
</foreach>
</select>
<select id="selectUsers" resultType="User">
SELECT * FROM users
<where>
<trim prefix="AND" prefixOverrides="AND" suffixOverrides="OR">
<if test="username != null">
AND username = #{username}
</if>
<if test="email != null">
OR email = #{email}
</if>
</trim>
</where>
</select>
通过以上方式,MyBatis的XML文件可以灵活地支持动态SQL,根据不同的条件生成不同的SQL语句,实现更加灵活和强大的SQL操作。