<select id="selectUser" parameterType="map" resultType="User">
SELECT * FROM user
<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 user
<where>
<choose>
<when test="username != null">
AND username = #{username}
</when>
<when test="age != null">
AND age = #{age}
</when>
<otherwise>
AND gender = 'male'
</otherwise>
</choose>
</where>
</select>
<select id="selectUsers" parameterType="map" resultType="User">
SELECT * FROM user
WHERE id IN
<foreach item="id" collection="ids" open="(" separator="," close=")">
#{id}
</foreach>
</select>
以上是一些常用的mybatis dynamic配置技巧,可以根據具體需求靈活運用。通過合理使用動態SQL功能,可以提高SQL語句的復用性和效率。