<!-- 演示动态sql-if标签的使用情景 --><selectid="getUserByWhere"parameterType="user"resultType="com.xu.mybatis.pojo.User"><!-- SELECT * FROM USER WHERE username LIKE '%${username}%' and id = #{id} --><!-- 1=1 是为了保证sql语句在if判断为空时可以正常运行 -->
SELECT * FROM USER where 1 = 1
<!-- if标签的使用 --><iftest="id != null">
and id = #{id}
</if><iftest="username != null and username != ''">
and username LIKE '%${username}%'
</if></select>
02、where标签
<!-- 演示动态sql-where标签的使用情景 --><selectid="getUserByWhere2"parameterType="user"resultType="com.xu.mybatis.pojo.User"><!-- include:引入sql片段,refid引入片段id -->
SELECT
*
FROM USER
<!-- where会自动加上where同处理多余的and --><where><!-- if标签的使用 --><iftest="id != null">
and id = #{id}
</if><iftest="username != null and username != ''">
and username LIKE '%${username}%'
</if></where></select>