Mybatis_动态SQL

什么是动态SQL?

        官方文档mybatis – MyBatis 3 | 动态 SQLicon-default.png?t=M4ADhttps://mybatis.org/mybatis-3/zh/dynamic-sql.html

        简而言之:动态SQL可以根据实际情况自动生成不同的SQL语句

动态SQL

"1 = 1"与"1 <> 1"或"1 != 1"

where 1 = 1
where 1 <> 1 或 where 1 != 1

关于前者(恒成立条件):

select id,name,password...... from table
where 1 = 1
    and condition1
    and condition2
    and condition3
    ......

若不添加"1 = 1",则当后续条件均不符合时,SQL语句为"select id,name,password from table where",显然这是一条错误的SQL语句

若添加"1 = 1",则有"select id,name,password from table where 1 = 1",即使在后续条件均不符合时,SQL语句仍是正确的,将返回表中的所有数据

关于后者(恒不成立条件):

create table2 as select * from table1 where 1 <> 1;

由于在创建表的时候,优先创建表的结构,再添加表的数据,该语句可以实现只复制表结构而不复制表数据

IF标签

若test条件成立,则执行if标签内的SQL语句

        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
        ......

WHERE标签

where标签会自动去除多余的and与or后再进行sql语句的拼接

    <!--where-->
    <!--where标签会自动去除多余的and与or后再进行sql语句的拼接-->
    <select id="selectIF" parameterType="map" resultType="cn.alan.POJO.Blog">
        select * from mybatis.blog
        <where>
            <where>
            <if test="title != null">
                and title = #{title}
            </if>
            <if test="author != null">
                and author = #{author}
            </if>
            ......
        </where>
        </where>
    </select>

若第一个if条件不成立而第二个成立时,等效SQL语句为

select * from mybatis.blog where author = #{author}

SET标签

set标签会自动去除多余的逗号后再进行SQL语句的拼接

    <!--set-->
    <!--set标签会自动去除多余的逗号(,)后再进行sql语句的拼接-->
    <update id="updateBlog" parameterType="map">
        update mybatis.blog
        <set>
            <if test="title != null">title = #{title},</if>
            <if test="author != null">author = #{author},</if>
            ......
        </set>
        where id = #{id}
    </update>

若第一个if条件不成立而第二个成立时,等效SQL语句为

update mybatis.blog set author = #{author} where id = #{id}

TRIM标签

trim标签可自定义类似where标签与set标签的功能

    <!--trim-->
    <!--
    trim标签可自定义类似where标签与set标签的功能

    等价于where标签的trim标签(AND后与|符号之间的空格是必要的)
    <trim prefix="WHERE" prefixOverrides="AND |OR ">
        ...
    </trim>

    等价于set标签的trim标签
    <trim prefix="SET" suffixOverrides=",">
        ...
    </trim>
    -->

CHOOSE标签

choose标签仅拼接标签内第一个成立的when标签,若when标签均不成立则拼接otherwise标签

    <!--choose-->
    <!--choose标签内 仅拼接第一个成立的when标签 若when标签均不成立 则拼接otherwise标签-->
    <select id="selectChoose" parameterType="map" resultType="cn.alan.POJO.Blog">
        select * from mybatis.blog
        <where>
            <choose>
                <when test="title != null">
                    title = #{title};
                </when>
                <when test="author != null">
                    author = #{author};
                </when>
                ......
                <otherwise>
                    views = #{views};
                </otherwise>
            </choose>
        </where>
    </select>

FOREACH标签

关于foreach标签:
    collection为进行迭代的集合对象 根据传入参数不同 collection取值也不同
        链表 list
        数组 array
        对象 集合属性的属性名(若属性的属性为集合对象 则填xxx.xxx 以此类推)
        map 键
    item为集合中元素迭代时的别名 供选择器选取
    open为开头sql
    separator为分隔符
    close为结尾sql
    foreach标签内为被分隔内容

    <!--foreach-->
    <!--
    collection为进行迭代的集合对象 根据传入参数不同 collection取值也不同
        链表 list
        数组 array
        对象 集合属性的属性名(若属性的属性为集合对象 则填xxx.xxx 以此类推)
        map 键
    item为集合中元素迭代时的别名 供选择器选取
    open为开头sql
    separator为分隔符
    close为结尾sql
    foreach标签内为被分隔内容
    -->
    <select id="selectForEach" parameterType="list" resultType="cn.alan.POJO.Blog">
        select * from mybatis.blog where id in
        <foreach collection="list" item="ID" open="(" separator="," close=")">
            #{ID}
        </foreach>
    </select>

等效SQL语句

select * from mybatis.blog WHERE ( id = ? , id = ? , id = ? )

其中"?"代表循环取出的ID值

SQL标签

封装简单sql语句用于复用,使用include标签调用

    <sql id="if-title-author">
        <if test="title != null">
            and title = #{title}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
        ......
    </sql>

调用举例

        select * from mybatis.blog
        <where>
            <include refid="if-title-author"/>
        </where>

THE END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值