Mybatis动态 sql用法

本文介绍了Mybatis的动态SQL用法,包括if、choose、where、set、trim、foreach和bind元素。详细阐述了它们的功能、语法和应用场景,帮助理解如何根据条件动态拼接SQL语句,实现对数据库的精准操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义:根据不同条件拼接 SQL 语句,实现对数据库更准确的操作;

实现:映射器配置文件或者注解。

常用的动态SQL元素:

        if元素:判断语句,单条件分支判断

        choose元素(when,otherwise):多条件分支判断,等 同于 java 的 switch

        trim(where,set):辅助元素,用于处理一些 SQL 拼接的问题

        foreach 元素:循环语句,在 in 语句等列举条件常用

        bind 元素:自定义上下文变量, 传递参数


1.if元素:通常用于where语句中,通过判断参数值来决定是否使用某个查询条件条件,它也经常用于update语句中判断是否更新某一个字段,还可以在insert语句中用来判断是否插入某个字段的值

        语法:< if test =”条件”> 满足条件的语句 </if>

        注意:拼接 SQL 语句的时候注意 AND 和逗号。

        if表达式 一般使用是放在where条件后面,判断参数是否传递使用if test属性(必填)为true或者false test使用OGNL表达式处理,返回true则进入到if标签的SQL,返回为false则不会进入if标签。

 <select id="selectStudent" parameterType="student"   resultType="Student">
        select * from Student where 1=1
               <if test="sid != 0">
                    and sid = #{sid}
                </if>
                <if test="sname != null">
                    and sname = #{sname}
                </if>
 </select>

2.choose元素:使用场景1.当新闻编号不为空,则只用新闻编号作为查询条件; 2.当新闻编号为空,而新闻标题不为空,则用新闻 标题作为条件进行模糊查询;3.当新闻编号和新闻标题都为空,则要求新闻作者不能为空。

        语法:<choose>

                        <when test="条件">满足条件的语句</when>

                        <otherwise>满足其他条件的语句</otherwise>

                </choose>

         注意:拼接 SQL 语句的时候注意 AND 和逗号。

3.where标签:如果该标签包含的元素有返回值,就插入一个where,如果where后面的字符串是以and或者or开头的且无参数传递,则将他们去除。

         语法:<where>

                                <if test="条件">满足条件的语句</if>

                     </where>

where表达式一般和if表达式一块使用,如果条件一个都不满足,则不拼接where条件;如果有一个或者多个if表达式,where会直接拼接在SQL上,并且紧随where的表达式的and或者or会被忽略掉。      

<select id="selectStudent" parameterType="student" resultType="Student">
    select * from Student
    <where>
        <if test="sid != 0">
            and sid = #{sid}
        </if>
        <if test="sname != null">
            and sname = #{sname}
        </if>
    </where>
</select>

4.set标签:set 标签元素主要是用在更新操作的时候, 它的主要功能和 where 标签元素其实是差不多的,主要是在包含的语句前输出一个 set, 然后如果包含的语句是以逗号结束的话将会把该逗号忽略,如果 set 包含的内容为空的话则会出错。有了 set 元素就可以动态的更新那些修改了的字段。

        语法:

                     <set>

                                <if test="条件">满足条件的语句</if>

                     </set>

<update id="updateStudent" parameterType="Student">
UPDATE student
    <set>
        <if test="sname != null and sname !=''">
            sname =#{sname},
        </if>
        <if test="ssex!= null and ssex!=''">
        ssex =#{ssex},
        </if>
        <if test="birthday != null ">
        birthday =#{birthday},
        </if>
    </set>
    Where sid =#{sid};
</update>

5.trim元素:

        trim标签有4个属性:

                prefix:如果trim标签内不为空,则增加某内容作为前缀(需要拼接的子句,可以是                 where,or 或者 set);
                suffix:如果trim标签内不为空,则增加某内容作为后缀
                prefixOverrides:删除trim标签包裹的第一个某内容(忽略通过管道分隔的文本序列前缀。一般 不与 suffixOvrrides 同时使用)
                suffixOverrides:删除trim标签包裹的最后一个某内容(忽略通过管道分隔的文本序列后缀。一般 不与 prefixOvrrides 同时使用)

        语法:

                <trim prefix="" suffixOverrides="" prefixOverrides="" suffix="" ></trim>

        例: <trim prefix="where" suffixOverrides="and" prefixOverrides="or"  ></trim>

<insert id="addstduent" parameterType="Student" useGeneratedKeys="true" keyProperty="sid">
insert into student
<trim prefix="(" suffix=")" suffixoverrides=",">
    <if test="sname != null and sname !=''">
        sname,
    </if>
    <if test="ssex != null and ssex !=''">
        ssex,
    </if>
    <if test="birthday != null ">
        birthday,
    </if>
</trim>
<trim prefix="values(" suffix=")" suffixOverrides=",">
    <if test="sname != null and sname !=''"S
        #{sname},
    </if>
    <if test="ssex != null and ssex l=''">
        #{ssex},
    </if>
    <if test="birthday != null ">
        #{birthday},
    </if>
</trim>
</insert>

6.foreach:

(1)foreach表达式 collection属性,必填,指定的入参的类型(方法传递的参数,一个数组或者集合;)
        列表:list
        数组:array
        hashmap:map

(2)item属性:起名字,给集合中单个元素起名称(循环中的当前元素;)

(3)open属性:开始的字符串(以什么符号开始将这些集合元素包装起来;)

(4)close属性:结束的字符串(以什么符号结束将这些集合元素包装起来;)

(5)separator属性:数据之间的分隔符

(6)index:当前循环元素的位置下标;

        参数是数组:

<select id="findstudentofArray" resultType="Student">
    select * from student
    <where>
    <foreach item="sid" collection="array" open="sid in ("close=")" separator=",">
        #{sid}
    </foreach>
    </where>
</select>

        参数是ArrayList:

<select id="findStudentofArray" resultType="Student">
    select * from student
    <where>
    <foreach item="sid" collection="list" open="sid in (" close=")" separator=",">
        #{sid}
    </foreach>
    </where>
</select>

7.bind标签:适合模糊查询,bind表达式中参数必须是具有getter方法,底层是通过OGNL表达式来进行解析,该表达式通过属性的getter进行获取值。

        语法:<bind name="" value="_parameter"></bind>

                name:自定义变量的变量名

                value:自定义变量的变量值

                _parameter:传递进来的参数

<select id="findStudentLikeSname" parameterType="String" resultType="Student">
    <bind name="snameValue" value="'%'+_parameter+'%'"/>
        select * from student where sname like #{snameValue}
</select>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值