<!-- 演示动态sql-if标签的使用情景 -->
<select id="getUserByWhere" parameterType="user" resultType="com.itheima.mybatis.pojo.User">
SELECT * FROM USER where 1 = 1
<!-- if标签的使用 -->
<if test="id != null">
and id = #{id}
</if>
<if test="username != null and username != ''">
and username LIKE '%${username}%'
</if>
</select>
<!--
如果id不为null,username不为空和null,sql如下
SELECT * FROM USER WHERE username LIKE '%${username}%' and id = #{id}
-->
2.where
<select id="getUserByif" parameterType="com.lin.pojo.User" resultType="com.lin.pojo.User">
selec * from user
<where><!-- where会自动加上where同处理多余的and -->
<if test="username!=null and username!=''"> and username like'%${username}%'</if>
<if test="sex!=null and sex!=''"> and sex =#{sex}</if>
</where>
</select>
3.foreach
<!-- 测试foreach标签 -->
<select id="getUserByForeach" parameterType="com.lin.pojo.UserBuffered" resultType="com.lin.pojo.User">
select * from user
<where>
<foreach collection="ids" open="id in (" item="id" separator="," close=")">
<!-- foreach循环标签
collection:要遍历的集合,来源入参 (ids是list类型)
open:循环开始前的sql
separator:分隔符
close:循环结束拼接的sql
-->
#{id}
</foreach>
</where>
</select>
<!--等效于sql语句:select * from user where id in (1,2,3)-->