Mybatis框架-23:动态SQL——if标签、where标签、trim标签、choose标签

if标签

从下面这个查询语句我们可以很容易的看出,if标签其实是做判断使用的,当满足条件的时候执行对应的语句

<select id="getCustomerByNameAndPossition" resultType="com.ctbu.domain.Customer">
        select * from `customer`
        <where>
            <if test="cust_name !=null and cust_name !=''">
                cust_name=#{cust_name}
            </if>
            <if test="cust_profession !=null and cust_profession !=''">
                and cust_profession=#{cust_profession}
            </if>
        </where>
    </select>

where标签

可以自动生成sql语句中的where以及删除where之后的第一个and

trim标签

prefixOverrides="and" :前缀覆盖,覆盖第一个条件之前的相应内容

suffixOverrides="and" :后缀覆盖,覆盖最后一个条件之后的相应内容

prefix="" :前缀,在第一个条件之前添加的内容

​​​​​​​​​​​​​​suffix="":后缀,在最后一个条件之后添加的内容
<trim prefixOverrides="and" suffixOverrides="and">
            <if test="cust_name !=null and cust_name !=''">
                cust_name=#{cust_name}
            </if>
            <if test="cust_profession !=null and cust_profession !=''">
                and cust_profession=#{cust_profession}
            </if>
        </trim>

choose标签

判断当前条件如果成立,则不会判断后面的是否成立,如果前面的都不成立,则执行otherwise中的内容

<select id="getCustomerByNameAndPossition" resultType="com.ctbu.domain.Customer">
        select * from `customer`
        <where>
            <choose>
                <when test="cust_name != null">
                    cust_name=#{cust_name}
                </when>
                <when test="cust_profession != null">
                    cust_profession=#{cust_profession}
                </when>
                <otherwise>
                    cust_name='李白'
                </otherwise>
            </choose>
        </where>
    </select>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值