MyBatis是一個優(yōu)秀的持久層框架,其動態(tài)SQL操作功能非常強大。以下是一些MyBatis集合動態(tài)SQL操作的技巧:
<select id="selectByCondition" parameterType="map" resultType="com.example.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="selectByCondition" parameterType="map" resultType="com.example.User">
SELECT * FROM user
<where>
<choose>
<when test="username != null">
AND username = #{username}
</when>
<when test="age != null">
AND age = #{age}
</when>
<otherwise>
AND 1 = 1
</otherwise>
</choose>
</where>
</select>
<select id="selectByList" parameterType="map" resultType="com.example.User">
SELECT * FROM user
WHERE id IN
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>
<select id="selectByCondition" parameterType="map" resultType="com.example.User">
<bind name="limit" value="'10'"/>
SELECT * FROM user
LIMIT ${limit}
</select>
<update id="updateUser" parameterType="map">
UPDATE user
<set>
<if test="username != null">
username = #{username},
</if>
<if test="age != null">
age = #{age},
</if>
</set>
WHERE id = #{id}
</update>
以上是一些MyBatis集合動態(tài)SQL操作的技巧,可以根據(jù)具體業(yè)務(wù)需求靈活運用。