MyBatis是一个优秀的持久层框架,其动态SQL操作功能非常强大。以下是一些MyBatis集合动态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集合动态SQL操作的技巧,可以根据具体业务需求灵活运用。