MyBatis中if - else if - else 的使用

http://www.leftso.com/blog/765.html

不过有他的替代 choose,写法如下:

        <choose>
            <when test="params!=null">
                right JOIN
            </when>
            <otherwise>
                LEFT JOIN
            </otherwise>
        </choose>

复制 <choose> <when test="params!=null"> right JOIN </when> <otherwise> LEFT JOIN </otherwise> </choose>

以上即可实现 if else 逻辑

https://www.cnblogs.com/buzheng/p/12485464.html

MyBatis中if - else if - else 的使用

有表user(id, name, state, sex, age)

1、单个 if - else 使用。

根据状态不同进行查询

复制代码

<select id="selectUserByState" resultType="com.bz.model.entity.User">
    SELECT
      *
    FROM
      user
    WHERE
      1=1
    <choose>
      <when test="state == 1">
        AND name = #{name1}
      </when>
      <otherwise>
        AND name = #{name2}
      </otherwise>
    </choose>
  </select>

复制代码

2、多个if -else if -else的使用。

复制代码

<select id="selectUserByState" resultType="com.bz.model.entity.User">
    SELECT
      *
    FROM
      user
    WHERE
      1=1
    <choose>
      <when test="state == 1">
        AND name = #{name1}
      </when>
     <when test="state == 2">
        AND name = #{name2}
      </when>
      <otherwise>
        AND name = #{name3}
      </otherwise>
    </choose>
  </select>

复制代码

如有不对之处希望大家指点。共同进步,谢谢!

https://www.cnblogs.com/a8457013/p/8033263.html

mybatis if-else(写法)

mybaits 中没有else要用chose when otherwise 代替

范例一

复制代码

<!--批量插入用户-->
<insert id="insertBusinessUserList" parameterType="java.util.List">
    insert into `business_user` (`id` , `user_type` , `user_login` )
    values
    <foreach collection="list" index="index" item="item" separator=",">
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <choose>
                <when test="item.id != null and item.id !=''">
                    #{item.id,jdbcType=CHAR},
                </when>
                <otherwise>
                    '',
                </otherwise>
            </choose>
            <choose>
                <when test="item.userType != null and item.userType !=''">
                    #{item.userType,jdbcType=VARCHAR},
                </when>
                <otherwise>
                    '',
                </otherwise>
            </choose>
        </trim>
    </foreach>
</insert>

复制代码

其中choose为一个整体
when是if
otherwise是else

范例二:

复制代码

<select id="selectSelective" resultMap="xxx" parameterType="xxx">
    select
    <include refid="Base_Column_List"/>
    from xxx
    where del_flag=0
    <choose>
        <when test="xxx !=null and xxx != ''">
            and xxx like concat(concat('%', #{xxx}), '%')
        </when>
        <otherwise>
            and xxx like '**%'
        </otherwise>
    </choose>
</select>

复制代码

下面就是MyBatis中的if…else…表示方法

复制代码

<choose>
    <when test="">
        //...
    </when>
    <otherwise>
        //...
    </otherwise>
</choose>

复制代码

分类:mybatis

标签:mybatis

MyBatis的if-else语句可以使用<choose>...<when>...<otherwise>...</choose>来表示。在<choose>标签,可以使用多个<when>标签来表示多个条件判断,最后使用<otherwise>标签来表示其他情况。 举个例子,如果我们在一个查询语句需要根据不同的条件来动态生成不同的SQL语句,可以使用if-else来实现。在<select>标签的SQL语句使用<choose>标签包裹条件判断的逻辑。每个条件判断使用<when>标签表示,例如当xxx不为空时,使用<when>语句来拼接SQL语句的条件;当xxx为空时,使用<otherwise>语句来拼接SQL语句的条件。 另外还有一个示例是在插入语句使用if-else语句。在<insert>标签使用<foreach>循环来遍历列表,并使用<trim>标签来拼接SQL语句的字段和对应的值。在<choose>标签使用<when>语句来判断字段的值是否为空,如果不为空,则使用字段的值,如果为空,则使用指定的默认值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [mybatis xml mapper 文件 if-else 写法](https://blog.csdn.net/aotun7642/article/details/102437909)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值