mybatis之动态sql

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值