Mybatis动态SQL

if 标签

If标签用于条件判断,当条件成立就附加之间的sql语句,如果条件不成立就不附加之间的sql语句。

<if test=”xxx”> ......</if>

where标签

标签在sql语句后面附加where关键字,当where 标签中有成条件成立时就会附加where关键字,如where标签中没有任何条件成立则不会附加where关键字,where会忽略掉离他最近的一个无关的and 或or

<select id="getPersonByCondtion" resultType="Person">
       select * from person
       <where>
           <if test="name!=null">
               and name=#{name}
           </if>
           <if test="gender!=null">
               and gender=#{gender}
           </if>
           <if test="age!=null">
               and age=#{age}
           </if>
           <if test="birthday!=null">
               and birthday=#{birthday}
           </if>
       </where>
    </select>

动态sql之choose、when、otherwises标签

当choose中有一个when成立就附加该条件,其它的when都不会被执行,如果所有的when都不成立就附加otherwise标签之间的sql语句

<select id="getPersonByCondtion" resultType="Person">
        select * from person
        <where>
            <choose>
                <when test="name!=null">
                    and name=#{name}
                </when>
                <when test="gender!=null">
                    and gender=#{gender}
                </when>
                <when test="age!=null">
                    and age=#{age}
                </when>
                <when test="birthday!=null">
                    and birthday=#{birthday}
                </when>
                <otherwise>
                     and 1=1
                </otherwise>
            </choose>
         </where>
    </select>

set标签

Set标签用于更新,当set标签中有if条成立成就会附加该列,更新指定的列。会自动忽略掉最后一个与sql语句无关的逗号。

<update id="updatePerson">
        update person
         <set>
             <if test="name!=null">
               name=#{name},
             </if>
             <if test="gender!=null">
                 gender=#{gender},
             </if>
             <if test="age!=null">
                 age=#{age},
             </if>
             <if test="birthday!=null">
                 birthday=#{birthday}
             </if>
         </set>
         where id=#{id}
    </update>

trim 标签

Trim标签要以附加sql关键字,trim应用于where和set关键字,
Prefix 前缀关键字,prefixOverrides=”and|or” 去掉条件前面无关的and或or
Suffix 后缀关键字, suffixOverrides=”and|or” 去掉条件后面无关的and或or

<select id="getPersonByCondtion" resultType="Person">
        select * from person
        <trim prefix="where" suffixOverrides="and|or">
            <if test="name!=null">
                 name=#{name} and
            </if>
            <if test="gender!=null">
                 gender=#{gender} and
            </if>
            <if test="age!=null">
                 age=#{age} and
            </if>
            <if test="birthday!=null">
                and birthday=#{birthday}
            </if>
        </trim>
    </select>

foreach标签

Collection: 指定集合的类型
Item: 定义一个变量用来接收集合中每个对象
Separator: 分隔符,当本次循环结束后,下次循环开始前附加分隔符
Open 附加字符 ,在循环开始前,附加字符串
Close 附加字符 ,在循环结束后,附加字符串
Index 集合的索引

可以使用在批量添加 删除 更新
注意:mysql本身不持批量更新,如果想要批量更新在url后面附加参数 allowMultiQueries=true

示例:

jdbc:mysql:///aaa?allowMultiQueries=true"

bind标签

<select id="getPersonByLikeName" resultType="Person">
        <bind name="keyword" value="'%' + _parameter.getName() + '%'" />
        select * from person where name like #{keyword}
    </select>

这里,参数名称必须是_parameter,否则会提示异常。
例如,将_parameter改为_parameter1,出现如下提示。
“nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘_parameter1’ in ‘class java.lang.String’”, “data”: null,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值