zoukankan      html  css  js  c++  java
  • 【串线篇】Mybatis之动态sql

    一、if标签

    <select id="getTeacherByCondition" resultMap="teacherMap">
    
            select * from t_teacher where
    
                   
    
                <if test="id!=null">
    
                    id > #{id} and
    
                </if>
    
               
    
                <if test="name!=null &amp;&amp; !name.equals(&quot;&quot;)">
    
                    teacherName like #{name} and
    
                </if>
    
                <if test="birth!=null">
    
                    birth_date &lt; #{birth} and
    
                </if>
    
    <!--&quot;&quot;    “”的转义字符-- >
    
    <!--&amp;&amp;  &&的转义字符,或者直接写and妥了 -- >
    
    <!-- &lt;小于号的转义字符 -->
    
    </select>

    【注】

    test属性指定JavaBean里的属性名

    teacherName like #{name} and:teacherName是数据库表里的字段名

    二、while标签

    where可以帮我们去除掉前面的and;

    id > #{id},and teacherName like #{name},and birth_date &lt; #{birth}

    意思是万一有比如第一句不满足,来到了第二句但第二局前面多了个and这时候where标签就会自动去除

     

    三、trim标签

    <!-- trim:截取字符串
    
                prefix="":前缀;为我们下面的sql整体添加一个前缀
    
                prefixOverrides="": 去除出整体字符串前面可能多余的字符
    
                suffix="":为整体添加一个后缀
    
                suffixOverrides="":后面可能哪个多了可以去掉; -->
    
            <!-- 我们的查询条件就放在where标签中;每个and写在前面,
    
                where帮我们自动取出前面多余的and -->
    
            <trim prefix="where" prefixOverrides="and" suffixOverrides="and">
    
                <if test="id!=null">
    
                    id > #{id} and
    
                </if>
    
               
    
                <if test="name!=null &amp;&amp; !name.equals(&quot;&quot;)">
    
                    teacherName like #{name} and
    
                </if>
    
                <if test="birth!=null">
    
                    birth_date &lt; #{birth} and
    
                </if>
    
            </trim>
    
        </select>

     

    四、foreach标签

    <!-- public List<Teacher> getTeacherByIdIn(@Param("ids")List<Integer> ids);
    
        <select id="getTeacherByIdIn" resultMap="teacherMap">
    
           
    
            SELECT * FROM t_teacher WHERE id IN
    
            <!-- 帮我们遍历集合的;
    
    collection="":指定要遍历的集合的key
    
            close="":以什么结束
    
            index="i":索引;
    
                如果遍历的是一个list;
    
                    index:指定的变量保存了当前索引
    
                    item:保存当前遍历的元素的值
    
                如果遍历的是一个map:
    
                    index:指定的变量就是保存了当前遍历的元素的key
    
                    item:就是保存当前遍历的元素的值
    
            item="变量名":每次遍历出的元素起一个变量名方便引用
    
            open="":以什么开始
    
            separator="":每次遍历的元素的分隔符
    
                (#{id_item},#{id_item},#{id_item} -->
    
            <if test="ids.size >0">
    
                <foreach collection="ids" item="id_item" separator="," open="("
    
                    close=")">
    
                    #{id_item}
    
                </foreach>
    
            </if>
    
    </select>

    五、choose-when标签

    <!--public List<Teacher> getTeacherByConditionChoose(Teacher teacher); -->
    
        <select id="getTeacherByConditionChoose" resultMap="teacherMap">
    
            select * from t_teacher
    
            <where>
    
                <choose>
    
                    <when test="id!=null">
    
                       id=#{id}
    
                    </when>
    
                    <when test="name!=null and !name.equals(&quot;&quot;)">
    
                       teacherName=#{name}
    
                    </when>
    
                    <when test="birth!=null">
    
                       birth_date = #{birth}
    
                    </when>
    
                    <otherwise>
    
                       1=1
    
                    </otherwise>
    
                </choose>
    
            </where>
    
        </select>

    【注】

    1)、与if不同的是,当满足一个条件的时候when只会进一个,而if都会判断

    2)、<otherwise>1=1</otherwise>执行所有查询,有几条返回几条

    六、set标签完成mybatis动态更新

     

    !-- public int updateTeacher(Teacher teacher); -->
    
        <update id="updateTeacher">
    
            UPDATE t_teacher
    
            <set>
    
                <if test="name!=null and !name.equals(&quot;&quot;)">
    
                    teacherName=#{name},
    
                </if>
    
                <if test="course!=null and !course.equals(&quot;&quot;)">
    
                    class_name=#{course},
    
                </if>
    
                <if test="address!=null and !address.equals(&quot;&quot;)">
    
                    address=#{address},
    
                </if>
    
                <if test="birth!=null">
    
                    birth_date=#{birth}
    
                </if>
    
            </set>
    
            <where>
    
                id=#{id}
    
            </where>
    
             
    
    </update>

    【注】set就是sql语句中的set,他会帮我们自动去除可能多余的逗号

    七、sql标签与include

    1)、抽取sql到外面

    <sql id=”selectSql”>select * from t_teacher</sql>

    2)、内调用

    <select id="getTeacherById" resultMap="teacherMap">

            <include refid=”selectSql”></include>

            where id=#{id}

    </select>

  • 相关阅读:
    Javaweb 第4 天xml 课程
    Javaweb 第2天 JavaScript课程
    Javaweb 第1天 HTML和CSS课程
    第27天反射(解剖)技术
    Ip 讲解
    第26 天网络编程
    第25天多线程、网络编程
    【剑指offer】连续子数组的最大和,C++实现
    [剑指offer]数组中最小的K个数,C++实现
    【剑指offer】数组中出现次数超过数组长度一半的数字,C++实现
  • 原文地址:https://www.cnblogs.com/yanl55555/p/11936731.html
Copyright © 2011-2022 走看看