Mybatis从入门到精通——动态SQL标签(10)

一、动态sql标签作用

动态SQL标签是Mybatis的一大特色,通过使用动态SQL标签可以完成一些稍微复杂的操作和简化开发。

动态SQL标签主要包括:if、where、set、trim、choose-when-otherwise、foreach,下面将对每个标签进行说明。

 

二、where标签

说明:where标签一般和if标签搭配使用,用于sql中存在多余字符and或or的问题,当使用where标签时,如果第一行多出了and或or则会自动去除,并且where之后如果不存在任何条件,则会自动去除where字符。

where标签使用方式:

    <where>

          and 条件1

          and 条件2

    </where>

案例:

    <select id="selectByPerson" resultType="com.my.entity.Person" parameterType="com.my.entity.Person">
        select
        <include refid="Base_Column" />
        from person
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="name != null and name != ''">
                and name = #{name}
            </if>
            <if test="sex != null and sex != ''">
                and sex = #{sex}
            </if>
            <if test="telephone != null and telephone != ''">
                and telephone = #{telephone}
            </if>
            <if test="address != null and address != ''">
                and address = #{address}
            </if>
            <if test="age != null">
                and age = #{age}
            </if>
        </where>
    </select>

三、if标签

说明:if标签用于做简单的条件判断,如果返回true,则使用if标签体内的sql,如果不符合就不使用if标签体内的sql;如果要判断某个属性或参数是否符合直接写其参数名即可,不需要加#{}。

if标签使用方式:

    <if test="条件">

          sql内容

    </if>

注意:if标签进行与条件使用 and 字符,进行或条件使用 or 字符。

案例:

    <select id="selectByPerson" resultType="com.my.entity.Person" parameterType="com.my.entity.Person">
        select
        <include refid="Base_Column" />
        from person
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="name != null and name != ''">
                and name = #{name}
            </if>
            <if test="sex != null and sex != ''">
                and sex = #{sex}
            </if>
            <if test="telephone != null and telephone != ''">
                and telephone = #{telephone}
            </if>
            <if test="address != null and address != ''">
                and address = #{address}
            </if>
            <if test="age != null">
                and age = #{age}
            </if>
        </where>
    </select>

四、set标签

说明:set标签作用和where标签类似,用于处理update标签中set后多个赋值多出逗号(,)字符的问题,一般和if标签搭配使用,如果set后的条件都没有,则会把set字符删除。

set标签使用方式:

    <set>

        赋值1,

        赋值2,

    </set>

案例:

    <update id="updateById" parameterType="com.my.entity.Person">
        update person
        <set>
            <if test="name != null and name != ''">
                name = #{name},
            </if>
            <if test="sex != null and sex != ''">
                sex = #{sex},
            </if>
            <if test="telephone != null and telephone != ''">
                telephone = #{telephone},
            </if>
            <if test="address != null and address != ''">
                address = #{address},
            </if>
            <if test="age != null">
                age = #{age},
            </if>
        </set>
        where id = #{id}
    </update>

五、trim标签

说明:用于添加前缀和后缀,并且能去除头部字符和尾部字符,功能强大,任何地方都可以使用。

trim标签的使用方式:

    <trim prefix="添加的前缀字符" suffix="添加的后缀字符" suffixOverrides="去除的尾部字符" prefixOverrides="去除的头部字符">

        sql内容

    </trim>

注意:并不是所有的属性都需要用到,只使用需要的属性即可。

案例:

    <insert id="insert" parameterType="com.my.entity.Person" keyProperty="id" useGeneratedKeys="true">
        insert into person
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <if test="id != null">
                id,
            </if>
            <if test="name != null and name != ''">
                name,
            </if>
            <if test="sex != null and sex != ''">
                sex,
            </if>
            <if test="address != null and address != ''">
                address,
            </if>
            <if test="telephone != null and telephone != ''">
                telephone,
            </if>
            <if test="age != null">
                age,
            </if>
        </trim>
        <trim prefix="values(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id},
            </if>
            <if test="name != null and name != ''">
                #{name},
            </if>
            <if test="sex != null and sex != ''">
                #{sex},
            </if>
            <if test="address != null and address != ''">
                #{address},
            </if>
            <if test="telephone != null and telephone != ''">
                #{telephone},
            </if>
            <if test="age != null">
                #{age},
            </if>
        </trim>
    </insert>

五、foreach标签

说明:用于遍历操作,用于对List或Array或Set等集合和数组的遍历操作。

foreach标签的使用方式:

    <foreach collection="array|list|collection|属性名" open="前缀" close="后缀" item="每一个元素的变量名" separator="分隔符" index="下标的变量名">

       sql内容

    </foreach>

该遍历一般用于select in语句的遍历以及各种批量操作。

案例:

    <select id="selectListByIds" resultMap="BaseResult">
        select
        <include refid="Base_Column" />
        from person
        <where>
            id in
            <foreach collection="array" open="(" close=")" item="id" separator="," index="i">
                #{id}
            </foreach>
        </where>
    </select>

六、choose-when-otherwise标签

说明:该标签类似java的switch case的操作,只要满足其中一个when的条件,就只执行该when标签内的sql,其它when和otherwise的则不管,when相当于case,用于条件判断,otherwise则相当于when条件都不满足时执行的操作,类似switch的default。

choose-when-otherwise标签的使用方式:

    <choose>

        <when test="条件判断1">

            sql内容

        </when>

        <when test="条件判断2">

            sql内容

        </when>

        <otherwise> 

            默认的sql内容

        </otherwise>

    </choose>

注意:when标签中的test属性和if标签的test属性操作一致,可以引用参数进行判断。

案例:

    <select id="selectByNameOrSexOrAge" resultMap="BaseResult">
        select
        <include refid="Base_Column" />
        from person
        <where>
            <choose>
                <when test="name != null and name != ''">
                    and name = #{name}
                </when>
                <when test="sex != null and sex != ''">
                    and sex = #{sex}
                </when>
                <when test="age != null">
                    and age = #{age}
                </when>
                <otherwise>
                    <!-- 什么都不做 -->
                </otherwise>
            </choose>
        </where>
    </select>

七、补充说明

一般都是多个标签配合使用。

发布了61 篇原创文章 · 获赞 81 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/m0_37914588/article/details/104738213