一. sql片段
1.声明sql片段
<!--sql片段-->
<sql id="sql_select">
select * from t_user
</sql>
- 引用sql片段
<!--调用sql片段 include refid="..."-->
<include refid="sql_select"></include>
二. where标签
where标签的作用是可以自动处理掉第一个and
<select id="selectByIds" resultType="User">
<!--调用sql片段 include refid="..."-->
<include refid="sql_select"></include>
<where>
<!--foreach循环-->
<foreach collection="ids" open="and id in(" item="id" separator="," close=")">
#{id}
</foreach>
</where>
</select>
三. if标签
如果money值为空或者没有值,则执行sql语句,查询数据库对应表格的信息
<!--sql片段-->
<sql id="sql_select">
select * from t_user
</sql>
<select id="selectByMonry" resultType="User">
<!--调用sql片段 include refid="..."-->
<include refid="sql_select"></include>
<where>
<!--if条件语句-->
<if test="money != null and money != '' ">
and money > 1000
</if>
</where>
</select>
四. foreach标签
foreach标签的open属性表示循环开始;close属性表示循环结束;separator属性表示每次循环中间的分隔符
<select id="selectByIds" resultType="User">
<!--调用sql片段 include refid="..."-->
<include refid="sql_select"></include>
<where>
<!--foreach循环--> <!-- open:循环开始 close:循环结束 separator:中间的分隔符 -->
<foreach collection="ids" open="and id in(" item="id" separator="," close=")">
#{id}
</foreach>
</where>
</select>