<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语句的复用性和效率。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>