一、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>

【串线篇】Mybatis之动态sql的更多相关文章

  1. Mybatis解析动态sql原理分析

    前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...

  2. MyBatis 示例-动态 SQL

    MyBatis 的动态 SQL 包括以下几种元素: 详细的使用参考官网文档:http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html 本章内容简单描述这 ...

  3. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...

  4. mybatis 使用动态SQL

    RoleMapper.java public interface RoleMapper { public void add(Role role); public void update(Role ro ...

  5. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

  6. 使用Mybatis实现动态SQL(一)

    使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面:        *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...

  7. MyBatis探究-----动态SQL详解

    1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...

  8. mybatis中的.xml文件总结——mybatis的动态sql

    resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...

  9. mybatis.5.动态SQL

    1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式 if语句,在DeptMapper.xml增加如下语句; <select id="selectB ...

  10. MyBatis的动态SQL详解-各种标签使用

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...

随机推荐

  1. php strrpos()函数 语法

    php strrpos()函数 语法 作用:寻找某字符串中某字符最后出现的位置.大理石构件怎么选择 语法:strrpos(string,find,start) 参数: 参数 描述 string 必需. ...

  2. luoguP4721 【模板】分治 FFT (分治NTT)

    给定 $g[1....n-1]$,求 $f[0],f[1],...,f[n-1]$,其中   $f[i]=\sum_{j=1}^{i}f[i-j]g[j]$    变界为 $f[0]=1$ 答案模 9 ...

  3. 2019 上海网络赛 F Rhyme scheme (字典树DP)

    题目:https://nanti.jisuanke.com/t/41414 题意:求长度为n的第k个bell number  ,  就是第i位的选取范围在 1-(i-1)位的最大值 +1,第一位固定为 ...

  4. python使用中遇到的一些问题

    一./usr/bin/ld:cannot find -lxxx错误 例如出现了问题: /usr/bin/ld:cannot find -lssl 其中xxx表示函式库文件名称,如上面的libssl.s ...

  5. RHEL7(RedHat 7)本地yum源的配置

    配置yum 源 1.挂载DVD光盘到/mnt   因为配置时候路径名里面不能有空格,否则不能识别  [root@ mnt]# mount /dev/cdrom /mnt 2.在目录/etc/yum.r ...

  6. 126B Password[扩展kmp学习]

    题目大意 给你一个字符串,求它的一个子串使得这个子串即使前缀又是后缀又出现在不是前缀且不是后缀的地方 分析 扩展kmp就是定义z[i]表示i~n的子串与整个串的最长公共前缀的长度是z[i] 所以这个题 ...

  7. Gradle查看Project中的所有 Task

    查看Project中所有的Task:$ gradle tasks 查看Project中所有的properties:$ gradle properties 如: 参照了: https://www.jia ...

  8. Drone 持续集成实践 - 基于 Gogs,以 Golang 为例

    Drone 官方示例 - Example Go project 用 Docker 部署 Go 服务器 Golang 官方示例 - outyet 一个生产环境的例子 用 rsync 复制文件的方式进行部 ...

  9. 网络流强化-HDU 3338-上下界限制最大流

    题意是: 一种特殊的数独游戏,白色的方格给我们填1-9的数,有些带数字的黑色方格,右上角的数字代表从他开始往右一直到边界或者另外一个黑格子,中间经过的白格子的数字之和要等于这个数字:左下角的也是一样的 ...

  10. eclipse或者myeclipse的代码提示功能

    第一步:打开eclipse,在菜单栏选择window-->preferences 第二步:在左边tree菜单栏选择General-->keys 第三步:在右边的文本框中输入content, ...