动态SQL

1.if标签:多条件查询。
 

    List<Car> selectByMultiCondition(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType);
    <select id="selectByMultiCondition" resultType="Car">
        select * from t_car where 1 = 1
        <!--
            1. if标签中test属性是必须的。
            2. if标签中test属性的值是false或者true。
            3. 如果test是true,则if标签中的sql语句就会拼接。反之,则不会拼接。
            4. test属性中可以使用的是:
                当使用了@Param注解,那么test中要出现的是@Param注解指定的参数名。@Param("brand"),那么这里只能使用brand
                当没有使用@Param注解,那么test中要出现的是:param1 param2 param3 arg0 arg1 arg2....
                当使用了POJO,那么test中出现的是POJO类的属性名。
            5. 在mybatis的动态SQL当中,不能使用&&,只能使用and。
            6. where 1 = 1 只是为了让条件判断中的语句为空的时候,代码也能够不受任何的影响,所添加的一个恒成立的条件
        -->
        <if test="brand != null and brand != ''">
            and brand like "%"#{brand}"%"
        </if>
        <if test="guidePrice != null and guidePrice != ''">
            and guide_price > #{guidePrice}
        </if>
        <if test="carType != null and carType != ''">
            and car_type = #{carType}
        </if>
    </select>

2.where标签:是专门负责where子句动态生成的 ,让where⼦句更加动态智能。
        所有条件都为空时,where标签保证不会⽣成where⼦句。
        ⾃动去除某些条件前⾯多余的and或or。但是后⾯多余的and是不会被去除的。

   <select id="selectByMultiConditionWithWhere" resultType="Car">
        select * from t_car
        <!--where标签是专门负责where子句动态生成的。-->
        <where>
            <if test="brand != null and brand != ''">
                and brand like "%"#{brand}"%"
            </if>
            <if test="guidePrice != null and guidePrice != ''">
                and guide_price > #{guidePrice}
            </if>
            <if test="carType != null and carType != ''">
                and car_type = #{carType}
            </if>
        </where>
    </select>

3.trim标签:

    <select id="selectByMultiConditionWithTrim" resultType="Car">
        select * from t_car
        <trim prefix="where" suffixOverrides="and|or">
            <if test="brand != null and brand != ''">
                brand like "%"#{brand}"%" or
            </if>
            <if test="guidePrice != null and guidePrice != ''">
                guide_price > #{guidePrice} and
            </if>
            <if test="carType != null and carType != ''">
                car_type = #{carType}
            </if>
        </trim>
        <!--trim标签的属性:
                  prefix:在trim标签中的语句前添加内容 ->加前缀 如果所有条件为空,不会被加上!
                  suffix:在trim标签中的语句后添加内容 ->加后缀
                  prefixOverrides:前缀覆盖掉(去掉) ->删除前缀
                  suffixOverrides:后缀覆盖掉(去掉) ->删除后缀
                  prefix="where" 是在trim标签所有内容的前面添加 where 
                  suffixOverrides="and|or" 把trim标签中内容的后缀and或or去掉-->
    </select>

4. set标签:
主要使⽤在update语句当中,⽤来⽣成set关键字,同时去掉最后多余的“,”
⽐如我们只更新提交的不为空的字段,如果提交的数据是空或者"",那么这个字段我们将不更新。

   <update id="updateBySet">
        update t_car
        <set>
            <if test="carNum != null and carNum != ''">car_num = #{carNum},</if>
            <if test="brand != null and brand != ''">brand = #{brand},</if>
            <if test="guidePrice != null and guidePrice != ''">guide_price = #{guidePrice},</if>
            <if test="produceTime != null and produceTime != ''">produce_time = #{produceTime},</if>
            <if test="carType != null and carType != ''">car_type = #{carType},</if>
        </set>
        where
            id = #{id}
    </update>

5. choose when otherwise标签:相当于if(){} else if(){} else{} 只有一个标签会被选择!

<!--需求:先根据品牌查询,如果没有提供品牌,再根据指导价格查询,如果没有提供指导价格,就根据⽣产⽇期查询。-->
    <select id="selectByChoose" resultType="Car">
        select * from t_car
        <where>
            <choose>
                <when test="brand != null and brand != ''">
                    brand like "%"#{brand}"%"
                </when>
                <when test="guidePrice != null and guidePrice != ''">
                    guide_price > #{guidePrice}
                </when>
                <otherwise>
                    car_type = #{carType}
                </otherwise>
            </choose>
        </where>
    </select>

6. foreach标签:
循环数组或集合,动态⽣成sql:一般用来进行批量删除和批量添加:

foreach标签的属性:
    collection:指定数组或者集合
    item:代表数组或集合中的元素
    separator:循环之间的分隔符
    open: foreach循环拼接的所有sql语句的最前面以什么开始。
    close: foreach循环拼接的所有sql语句的最后面以什么结束。
#比如这样的代码:
delete from t_car where id = 1 or id = 2 or id = 3;
insert into t_car values (null,'1001','凯美瑞',35.0,'2010-10-11','燃油⻋'), (null,'1002','⽐亚迪唐',31.0,'2020-11-11','新能源');
    * 批量删除。foreach标签 : 也可以使用 or 来进行批量删除separator="or"
    int deleteByIds(@Param("ids") Long[] ids);
    <delete id="deleteByIds">
        delete from t_car where id in
        <foreach collection="ids" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>
    * 批量插入,一次插入多条Car信息
    int insertBatch(@Param("cars") List<Car> cars);
    <insert id="insertBatch">
        insert into t_car values
        <foreach collection="cars" item="car" separator=",">
            (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#{car.carType})
        </foreach>
    </insert>

7 sql标签与include标签:
sql标签⽤来声明sql⽚段, include标签⽤来将声明的sql⽚段包含到某个sql语句当中
作⽤:代码复⽤。易维护。

    <!--声明一个SQL片段-->
    <sql id="carColumnNameSql">
        id, car_num as carNum, brand,  guide_price as guidePrice, produce_time as produceTime, car_type as carType
    </sql>

    <select id="selectById2" resultType="car">
        select
            <!--将声明的sql片段包含进来。-->
            <include refid="carColumnNameSql"/>
        from t_car where id = #{id}
    </select>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值