MyBatis的動態SQL是一種通過條件判斷來動態生成SQL語句的方式,可以根據不同的條件生成不同的SQL語句,從而實現動態查詢。下面是使用MyBatis的動態SQL的一些常用方法:
<select id="getUserList" parameterType="map" resultType="User">
SELECT * FROM user
<where>
<if test="name != null">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
</select>
<select id="getUserList" parameterType="map" resultType="User">
SELECT * FROM user
<where>
<choose>
<when test="name != null">
AND name = #{name}
</when>
<when test="age != null">
AND age = #{age}
</when>
<otherwise>
AND gender = #{gender}
</otherwise>
</choose>
</where>
</select>
<select id="getUserList" parameterType="map" resultType="User">
SELECT * FROM user
<where>
<trim prefix="AND" prefixOverrides="AND">
<if test="name != null">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</trim>
</where>
</select>
<select id="getUserList" parameterType="map" resultType="User">
SELECT * FROM user
WHERE id IN
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
通過以上方法,可以靈活地根據條件生成不同的SQL語句,實現動態查詢功能。更多關于MyBatis動態SQL的用法和示例可以參考MyBatis官方文檔。