在MyBatis的XML文件中,可以通过使用动态SQL语句来实现灵活配置。以下是一些常用的动态SQL语句:
<select id="selectUser" parameterType="map" resultType="User"> SELECT * FROM users <where> <if test="username != null"> AND username = #{username} </if> <if test="age != null"> AND age = #{age} </if> </where> </select> <select id="selectUser" parameterType="map" resultType="User"> SELECT * FROM users <where> <choose> <when test="username != null"> AND username = #{username} </when> <when test="age != null"> AND age = #{age} </when> <otherwise> AND id = #{id} </otherwise> </choose> </where> </select> <select id="selectUsersByIds" parameterType="map" resultType="User"> SELECT * FROM users WHERE id IN <foreach item="id" collection="ids" open="(" separator="," close=")"> #{id} </foreach> </select> 通过使用这些动态SQL语句,可以在MyBatis的XML文件中实现灵活配置,根据不同的条件动态生成不同的SQL语句,从而实现灵活的数据查询和操作。