动态SQL where trim

<!--trim的用法-->
 <!-- trim 只要包含的内容没有任何条件成立,trim不会生效
         如果包含的内容有条件成立,查看内部的语句前缀有没有与prefixOverrides内容相同的
          相同就将其去掉,即此处的and,之后再在前面加上前缀prefix
     -->
    <select id="selectBySelective" parameterType="tendencyParam" resultType="tendency">
        select * from tendency
        <trim prefix="where" prefixOverrides="and">
            <choose>
                <when test="id!=null">
                    and id=#{id}
                </when>
                <when test="supCode != null">
                    and sup_code=#{supCode}
                </when>
                <when test="createDate != null">
                    and create_date=#{createDate}
                </when>
                <!--<otherwise>.....</otherwise>-->
            </choose>
        </trim>
    </select>

<!--foreach的用法-->
  // select sum(arv_rate) from tendency where id in ( ? , ? , ? , ? )
        // mybatis内部会进行:将传递的集合对象
        // 通过map.put("list", listInteger),将集合保存成键值对存储
        // 之后再xml中在访问listInteger的时候,要通过list去访问
        double sum = tendencyMapper.sumTendencyIds(listInteger);
         <!--collection="list" 代表集合
     item="val" 代表foreach每次循环遍历的值
     separator="," 值之间的分隔符
     open="("  开始符号
     close=")"  结束符号
     (1001,1005,1007)
     -->
    <select id="sumTendencyIds" resultType="double">
        select sum(arv_rate)
        from tendency
        where id in
        <foreach collection="list" item="val" separator="," close=")" open="(">
           #{val}
        </foreach>
<!--if的用法-->
    </select>
    <update id="updateTendencyById" parameterType="tendency">
        update tendency
        <trim prefix="set" suffixOverrides=",">
        <!--<set>-->
            <if test="supCode!=null">
                sup_code=#{supCode},
            </if>
            <if test="supName!=null">
                sup_name=#{supName},
            </if>
            <if test="arvRate!=null">
                arv_rate=#{arvRate},
            </if>
            <if test="passRate!=null">
                pass_rate=#{passRate},
            </if>
            <if test="createDate!=null">
                create_date=#{createDate},
            </if>

        </trim><!--</set>-->
        where id=#{id}
    </update>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值