MyBatis动态SQL

勤奋不是嘴上说说而已,而是实际的行动,在勤奋的苦度中持之以恒,永不退却。业精于勤,荒于嬉;行成于思,毁于随。在人生的仕途上,我们毫不迟疑地选择勤奋,她是几乎于世界上一切成就的催产婆。只要我们拥着勤奋去思考,拥着勤奋的手去耕耘,用抱勤奋的心去对待工作,浪迹红尘而坚韧不拔,那么,我们的生命就会绽放火花,让人生的时光更加的闪亮而精彩。

导读:本篇文章讲解 MyBatis动态SQL,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文


什么是动态SQL?

Mybatis框架的动态SQL技术是一种根据特定条件动态拼装SQL语句的功能,它存在的意义是为了解决拼装SQL语句字符串时的痛点问题
假如现在有个多条件查询:
在这里插入图片描述
在这里插入图片描述
那假如现在我只想根据id来查询数据或者年龄来查询数据难道还要去专门再写一个接口和方法吗?
在这里插入图片描述

那么现在开始动态SQL就 来帮我们解决这个难题了

if标签

根据标签中test属性所对应的表达式决定标签中的内容是否需要拼接

    <select id="getEmpByConditionOne" resultType="emp">
        select * from t_emp where 1=1
        <if test="empName != null and empName !=''">
            and emp_name = #{empName}
        </if>
        <if test="age !=null and age != ''">
            and age = #{age}
        </if>
        <if test="sex !=null and sex != ''">
            and sex = #{sex}
        </if>
        <if test="email !=null and email != ''">
            and email = #{email}
        </if>
    </select>

if标签中的test属性是我们的判断属性当满足判断条件就会粘贴它里面的sql语句,但有一个弊端,就是当你如是这样子
在这里插入图片描述
在这里插入图片描述
就会报这样的错误
在这里插入图片描述
这也就是上面我们写成这样的原因
在这里插入图片描述

where标签

where:当where标签中有内容时,会自动生成where关键字,并且将内容前多余的or和and去掉
* 当where标签中没有内容时,此时where标签没有任何效果
* 注意:where标签中不能将其中内容后面多余的and或or去掉

示例代码

    <select id="getEmpByConditionTwo" resultType="emp">
        select * from t_emp
        <where>
        <if test="empName != null and empName !=''">
            emp_name = #{empName}
        </if>
        <if test="age !=null and age != ''">
             and age = #{age}
        </if>
        <if test="sex !=null and sex != ''">
             and sex = #{sex} and
        </if>
        <if test="email !=null and email != ''">
             and email = #{email}
        </if>
        </where>
    </select>

trim标签

trim:
* prefix|suffix:将trim标签中内容前面或后面添加指定内容
* prefixOverides|suffixOverides:将trim标签中内容前面或后面去掉指定内容
* 若标签中没有内容时,trim标签也没有任何效果

示例代码:

    <!--List<Emp> getEmpByCondition(Emp emp);-->
    <select id="getEmpByCondition" resultType="emp">
        select <include refid="empColums"></include> from t_emp

            <trim prefix="where" suffixOverrides="and|or">
            <if test="empName != null and empName !=''">
                emp_name = #{empName} and
            </if>
            <if test="age !=null and age != ''">
                age = #{age} and
            </if>
            <if test="sex !=null and sex != ''">
                sex = #{sex} and
            </if>
            <if test="email !=null and email != ''">
                email = #{email}
            </if>
            </trim>
    </select>

choose,when,otherwise标签

choose,when,otherwise,相当于if…else if…else
* when至少有一个,otherwise最多只能有一个

示例代码:

    <select id="getEmpByChoose" resultType="emp">
        select * from t_emp
        <where>
            <choose>
                <when test="empName !=null and empName !=''">
                    emp_name = #{empName}
                </when>
                <when test="age !=null and age !=''">
                    age = #{age}
                </when>
                <when test="sex !=null and sex !=''">
                    sex = #{sex}
                </when>
                <when test="email !=null and email !=''">
                    email = #{email}
                </when>
                <otherwise>
                    eid = 1
                </otherwise>
            </choose>
        </where>
    </select>

foreach标签

* collection:设置需要循环的数组或集合
* item:表示数组或集合中的每一个数据
* separator:循环体之间的分隔符
* open:foreach标签所循环的所有内容的开始符
* close:foreach标签所循环的所有内容的结束符

不知道是否你们还记得我们之前学的批量删除,当时我们传给的参数是一个字符串,那现在如果我们传的参数是一个数组该怎么实现批量删除呢,还有,怎么样来实现批量增加呢?

批量删除:

<!--int deleteMoreByArray(Integer[] eids);-->
<delete id="deleteMoreByArray">

        delete from t_emp where
        <foreach collection="eids" item="eid" separator="or">
            eid = #{eid}
        </foreach>

       <!-- delete from t_emp where eid in
        <foreach collection="eids" item="eid" separator="," open="(" close=")
">
            #{eid}
        </foreach> -->
    </delete>

批量增加:

    <!--int insertMoreBylist(@Param("emps") List<Emp> emps);-->
    <insert id="insertMoreBylist">
        insert into t_emp values
        <foreach collection="emps" item="emp" separator=",">
            (null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
        </foreach>
    </insert>

sql标签

* 设置SQL片段:<sql id="empColums">eid,emp_name,age,sex,email</sql>
* 引用SQL片段:<include refid="empColums"></include>*/

代码示例:

 <sql id="empColums">eid,emp_name,age,sex,email</sql>

    <!--List<Emp> getEmpByCondition(Emp emp);-->
    <select id="getEmpByCondition" resultType="emp">
        select <include refid="empColums"></include> from t_emp

            <trim prefix="where" suffixOverrides="and|or">
            <if test="empName != null and empName !=''">
                emp_name = #{empName} and
            </if>
            <if test="age !=null and age != ''">
                age = #{age} and
            </if>
            <if test="sex !=null and sex != ''">
                sex = #{sex} and
            </if>
            <if test="email !=null and email != ''">
                email = #{email}
            </if>
            </trim>
    </select>

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/141559.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!