mybatis之动态SQL

根据不同的条件需要执行不同的 SQL 命令.称为动态 SQL
 
If 
<select id="selByAccinAccout" resultType="log">
        select * from log where 1=1   //如果下面语句都不满足,就查询全部,这样不会报错
<!-- OGNL 表达式,直接写 key 或对象的属性.不需要添加任何特字符号 -->
        <if test="accin!=null and accin!=''">
                and accin=#{accin}
        </if>
        <if test="accout!=null and accout!=''">
                and accout=#{accout}
        </if>
</select>

where

        当编写 where 标签时, 如果内容中第一个是 and 会去掉第一个and
        如果<where>中有内容会生成 where 关键字 (都是先加后去),如果没有内容不生成 where 关键
        比直接使用<if> 少写 where 1=1
<select id="selByAccinAccout" resultType="log">
        select * from log
<where>
        <if test="accin!=null and accin!=''">
                and accin=#{accin}
        </if>
        <if test="accout!=null and accout!=''">
                and accout=#{accout}
        </if>
</where>
</select>

<choose> <when> <otherwise>

只要有一个成立,其他都不执行. 

如果 accin 和 accout 都不是 null 或不是””;生成的 sql 中只有 瞒足第一个条件的,即where accin=?

<select id="selByAccinAccout" resultType="log">
        select * from log
<where>
    <choose>
            <when test="accin!=null and accin!=''">
                    and accin=#{accin}
            </when>
            <when test="accout!=null and accout!=''">
                    and accout=#{accout}
            </when>
    </choose>
</where>
</select>

<set>

用在修改 SQL 中 set 从句

        作用:加个set, 去掉最后一个逗号(都是先加后去)
                 如果<set>里面有内容生成 set 关键字,没有就不生成
        示例
            id=#{id} 目的 防止<set>中没有内容:mybatis 不生成 set 关键字,如果修改中没有 set 从句 SQL 语法错误. 
<update id="upd" parameterType="log" >
    update log
    <set>id=#{id},
        <if test="accIn!=null and accIn!=''">
            accin=#{accIn},
        </if>
        <if test="accOut!=null and accOut!=''">
            accout=#{accOut},
        </if>
    </set>
     where id=#{id}
</update>

<Trim>

    prefix 在前面添加内容
    prefixOverrides 去掉前面内容
    suffix 在后面添加内容
    suffixOverrieds 去掉后面内容
    执行顺序去掉内容后添加内容
    代码示例
<update id="upd" parameterType="log">
    update log
    <trim prefix="set" suffixOverrides=",">
    a=a,
    </trim>
    where id=100
</update>

<bind>

    作用:给参数重新赋值
    场景:
        模糊查询
        在原内容前或后添加内容
    示例
<select id="selLikeName" resultType="flower" >
     <bind name="name" value=" '%'+name+'%' "></bind>
     select * from flower where name like #{name}
 </select>

<foreach>标签

    适用场景:  in 查询中;批量新增,删除中
        1 collectino=”” 要遍历的集合或数组(只能是array/list)
        2 item 迭代变量, #{迭代变量名}获取内容
        3 open 循环后左侧添加的内容
        4 close 循环后右侧添加的内容
        5 separator 每次循环时,元素之间的分隔符
<!--批量删除-->
<delete id="batchDel" >
    DELETE FROM product WHERE id IN
    <foreach collection="array" open="(" item="id" separator="," close=")" >
        #{id}
    </foreach>
</delete>
<!--批量插入-->
<insert id="batchIns" parameterType="Product">
    INSERT INTO product(productName,brand) VALUES
    <foreach collection="list" item="product" separator="," >
        (#{product.productName},#{product.brand})
    </foreach>
</insert>

<sql> 和<include>

    某些 SQL 片段如果希望 复用,可以使用<sql>定义这个片段
<sql id="filed">id,accin,accout,money</sql>

<sql id="whereSql">
    <where>
        <if test="productName!=null and productName!='' ">
            AND productName LIKE CONCAT("%",#{productName},"%")
        </if>
        <if test="dirId!=null">
            <![CDATA[AND dir_id <#{dirId}]]>
        </if>
    </where>
</sql>

    在<select>或<delete>或<update>或<insert>中使用<include>引用  id与refid名字对应即可
<select id="">
    select <include refid="filed"></include> from log
</select>

<!--高级查询-->
<select id="selByQuery" parameterType="ProductQuery" resultMap="productResultMap" >
    SELECT <include refid="filed"></include> FROM product
    <include refid="whereSql"></include>
</select>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心之所向...

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值