collection标签_MyBatis常用标签

38933976f8a7191e0ab4f321f9354ced.png

1.<resultMap> 通常用于做实体类和数据库表之间的映射,当查询的返回值为实体类的时候,可以直接返回相对应的<resultMap>。

2.<sql id="Base_Column_List">当需要查询的字段经常用到,就可以用sql标签来包装,当想要使用的时候使用<include refid="Base_Column_List"/>进行使用。

3.trim标签的使用:

<trim prefix="(" suffix=")" suffixOverrides=",">
prefix:前缀覆盖并添加其内容

suffix:后缀覆盖并添加其内容

prefixOverrides:前缀判断条件

suffixOverrides:后缀判断条件

<trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="guid != null">
                guid,
            </if>
            <if test="userMail != null">
                user_mail,
            </if>
            <if test="userGuid != null">
                user_guid,
            </if>
            <if test="userName != null">
                user_name,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="createUserEmail != null">
                create_user_email,
            </if>
            <if test="updateUserEmail != null">
                update_user_email,
            </if>
        </trim>

4.if 标签使用

<if test="userName != null">
user_name,
</if>

主要用于测试使用的字段是否为空。

5. foreach标签的使用

<foreach collection="users" item="p" open="" close=""
separator=",">

主要用于对于集合的操作,一般入参的数据类型都为Map。

collection为Map的key的类型

     <foreach collection="users" item="p" separator=",">
            (#{p.guid},
            #{p.userMail},
            #{p.userGuid},
            #{p.userName},
            1,
            #{p.createUserEmail},
            #{p.updateUserEmail},
            #{p.deptId},
            #{p.isOwer})
        </foreach>

6.choose标签的使用

<choose>
<when test="p.userName != null">#{p.userName},</when>
<otherwise>null,</otherwise>
</choose>

主要用于对字段的条件的判断,一般搭配otherwise来使用。

<foreach collection="users" item="p" open="" close=""
                 separator=",">
            <trim prefix="(" suffix=")">
                <choose>
                    <when test="p.userName != null">#{p.userName},</when>
                    <otherwise>null,</otherwise>
                </choose>
                <choose>
                    <when test="p.deptId != null">#{p.deptId},</when>
                    <otherwise>null,</otherwise>
                </choose>
                <choose>
                    <when test="p.isOwer != null">#{p.isOwer},</when>
                    <otherwise>0,</otherwise>
                </choose>
                <choose>
                    <when test="p.userMail != null">#{p.userMail}</when>
                    <otherwise>null</otherwise>
                </choose>
            </trim>
        </foreach>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值