mybatis xml mapper 文件中 if-else 写法

摘要:
我的诱饵里没有别的了。选择其他人而不是示例1#{item.userType,jdbcType=VARCHAR},'',其中choice是一个整体when是if otherwise是else示例2:selectfrom xxx其中del_flag=0和xxxlike concat和xxxike'**%'以下是MyBatis中的if…else…表示方法//…˃//…

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

范例一

<!--批量插入用户-->
<insert   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   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 xml mapper 文件中 if-else 写法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇命令行方式完成本地新建项目代码上传至git远程服务器仓库JavaWeb项目启动时,tomcat会启动两次的原因(之一)和解决方案下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

关于 mybatis 中 in 写法,&amp;lt;foreach collection="xxx" item="xxx" index="index" open="(" separator="," close=")"&amp;gt; 参数详解

使用 mybatis 时,如果要使用到 in 写法,要使用  foreach ,里面几个参数,看了很多地方,都说的不清不楚,自己最后各种测试,这里详细说下: (1)collection = “” ,这个参数是 dao 层(mapper)接口方法里面传过来的集合参数,如果dao 层传的参数只有一个,这里写关键字 list(如果是数组,写 array) 例子:...

mybatis中的#和$的区别?

1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by "111", 如果传入的值是id,则解析成的sql为order by "id".  2. $将传入的数据直接显示生成在sql中。如:order by $user_id$,如果传入的值...

springBoot集成Mybatis generator

一、引入jar包 <!--boot整合mybatis的包 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-s...

JAVA MyBatis配置文件用properties引入外部配置文件

方式一:通过properties 元素的子元素来传递数据 例如: 1 <properties> 2 <property name="driver" value="com.mysql.jdbc.Driver" /> <!-- 驱动类型 --> 3 <property nam...

mybatis的一级缓存

1、mybatis缓存使用场景 2、mybatis一级缓存命中场景  当mybatis执行相关右边的操作时,均会执行clearCache()方法,清空对应缓存。 一级缓存的作用域默认是sqlsession,也就是同一个会话。但是也可以设置为statement级别缓存,此时级别变小了。此时只有同一个statement的子查询才会命中缓存。 只有满足左边条...

mybatis中使用selectKey,返回结果一直是1

mybatis中使用selectKey,返回结果一直是1,结合这个问题,笔记一下selectKey标签以及问题的原因 先说需求,向数据库插入一条记录,表的id是自增的,插入以后返回插入记录的id 下面是xml文件中的插入的sql 1 <insert id="insertCompete" parameterType="CompetesWithBLOBs...