1.<if>标签
<if test="companyName != null and companyName != '' ">
and company_name like concat('%',#{companyName},'%')
</if>
test指条件,如果传过来的JavaBean的companyName属性不为空或者不为空字符串则将后面的语句拼接在后面
2.<where>标签
表示where关键字
<select id="selectByCondition" resultMap="brandResultMap">
select *
from tb_brand
<where>
<if test="status != null">
and status = #{status}
</if>
<if test="companyName != null and companyName != '' ">
and company_name like concat('%',#{companyName},'%')
</if>
<if test="brandName != null and brandName != '' ">
and brand_name like concat('%',#{brandName},'%')
</if>
</where>
</select>
3.<choose>和<when>标签
作用和switch一样,和<when>配合使用
<select id="selectByConditionSingle" resultMap="brandResultMap">
select *
from tb_brand
<where>
<choose><!--相当于switch-->
<when test="status != null"><!--相当于case-->
status = #{status}
</when>
<when test="companyName != null and companyName != '' "><!--相当于case-->
company_name like concat('%',#{companyName},'%')
</when>
<when test="brandName != null and brandName != ''"><!--相当于case-->
brand_name like concat('%',#{brandName},'%')
</when>
</choose>
</where>
</select>
4.<set>标签
用在更新语句中表示set关键字
<update id="updatePart">
update tb_brand
<set>
<if test="brandName != null and brandName != ''">
brand_name = #{brandName},
</if>
<if test="companyName != null and companyName != ''">
company_name = #{companyName},
</if>
<if test="ordered != null">
ordered = #{ordered},
</if>
<if test="description != null and description != ''">
description = #{description},
</if>
<if test="status != null">
status = #{status}
</if>
</set>
where id = #{id};
</update>
5.<foreach>标签
用来迭代任何可迭代的对象(如数组,集合)。有如下属性
-
collection 属性:
传入参数为list集合时 : collection = "list"
传入参数为array集合时 : collection = "array"
传入参数为其他集合时 : collection = "collection"
-
item 属性:遍历集合得到元素的元素名,用于标签中拿元素值。
-
separator 属性:集合项迭代之间的分隔符。
foreach
标签不会错误地添加多余的分隔符。也就是最后一次迭代不会加分隔符。 -
open 属性:该属性值是在拼接SQL语句之前拼接的语句,只会拼接一次
-
close 属性:该属性值是在拼接SQL语句拼接后拼接的语句,只会拼接一次
-
<delete id="deleteByIds"> delete from tb_brand where id in <foreach collection="array" item="id" separator="," open="(" close=")"> #{id} </foreach> </delete>