一、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. 用三目运算,与if判断 函数调用 达到相同判定作用

    三目运算符:     操作数1 ? 操作数2 : 操作数3   (操作数1位bool类型,操作数2和操作数3为两个相同的任何类型) 返回结果:如果操作数1判定结果为真,则将操作数2作为返回结果如果操作 ...

  2. js 和jquery

    1. js 全称 javascript 是 web客户端 运行的 解释性语言.. 2. jquery 只不过是 js 封装 简化了 ajax 和 dhtml 的 一款js 框架而已. 简单来说 Jqu ...

  3. 电脑如何连接windows server服务器

    第一步:win+r 启动‘运行’,输入mstsc 第二步:连接远程桌面,输入服务器ip 第三步,输入账户密码,登陆即可. 注释:mstsc,全称 Microsoft Telnet Screen Con ...

  4. (转)原理到实现 | K8S 存储之 NFS

    转:https://mp.weixin.qq.com/s/Mrr1Rnl_594Gyyn9fHekjw 1NFS介绍 NFS是Network File System的简写,即网络文件系统,NFS是Fr ...

  5. (转)使用windows server2008 创建 Hyper-V虚拟机

    转:https://jingyan.baidu.com/article/7c6fb42833ad4980652c904f.html Hyper-v是微软提供的虚拟机,利用server 2008搭建hy ...

  6. 去掉注释index.html

    <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...

  7. Spring Boot 读取外部的配置文件

    Spring Boot 程序会按优先级从下面这些路径来加载application.properties 或者 application.yml 配置文件 jar包同级目录下的/config目录jar包同 ...

  8. centos6.5下apollo1.7.1的搭建

    前言:apollo MQ作为消息队列中间件,在需要消息列表的应用程序环境中,需要使用该服务器中间件 1.准备工作 2.搭建 3.测试 1.准备工作 第一步:linux系统中配置好java环境 A.卸载 ...

  9. SqlServer 字段类型详解

    bit    整型 bit数据类型是整型,其值只能是0.1或空值.这种数据类型用于存储只有两种可能值的数据,如Yes 或No.True 或False .On 或Off. 注意:很省空间的一种数据类型, ...

  10. Convolutional Neural Networks(3):Convolution and Channels

    在CNN(1)和CNN(2)两篇文章中,主要说明的是CNN的基本架构和权值共享(Weight Sharing),本文则重点介绍卷积的部分. 首先,在卷积之前,我们的数据是4D的tensor(width ...