一、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. C++创建对象时什么时候用*,什么时候不用*

    用*, 表示创建的是一个指针对象,而指针的创建,必须初始化,C++中用new关键字开辟内存. 另外指针对象访问成员变量用-> , 非指针用. 就这么个原则 但是指针也可以不用-> 例如 ( ...

  2. python网络编程之验证客户端链接的合法性

    六.socket的更多方法介绍 服务端套接字函数s.bind() 绑定(主机,端口号)到套接字s.listen() 开始TCP监听s.accept() b被动接收TCP客户的连接,(阻塞式)等待连接的 ...

  3. 【CF1255A】Changing Volume【思维】

    题意:每次可以-5/-2/-1/+1/+2/+5,问是否存在方案使得A变成B 题解:首先我们可以设A<B,若A>B,则交换AB,因为A到B和B到A是互逆的过程,所以可以交换 其次将B-=A ...

  4. Zsh vs. Bash不完全对比解析,zsh是一种更强大的被成为“终极”的Shell

    https://www.zhihu.com/question/21418449 Mort | Zsh vs. Bash:不完全对比解析(1) 2014-10-07  bdpqlxz     Zsh和B ...

  5. 631D Messenger

    题目大意 给你串s和t 但是每个串都被表示为多个二元组(x,y)表示字符x连续出现y次 问t在s中出现了多少次 分析 我们先将s和t每个串中二元组合并 即相邻两个二元组如果字符相等则将它们变为一个 特 ...

  6. 理解JavaScript中的this

    在JavaScript中,this关键字是用来引用 调用该函数的 那个对象的.看几个栗子: var name="Window"; var obj={ name:"Obje ...

  7. C++ 测量程序执行时间的办法

    #include <time.h> clock_t start = clock(); //时间起始 /*待测试代码*/ clock_t end = clock(); //时间测试结束 co ...

  8. python web自动化测试框架搭建(功能&接口)——测试用例执行和结果收集

    由于unittest框架中结果收集在不同文件中,所以此处重写结果收集方法,加入执行时间,失败信息,失败截图等 TestRunner.py # coding=utf-8 import sys impor ...

  9. poj1258Agri-Net(最小生成树)

    题目链接:http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One ...

  10. MySQL 查询语句--------------进阶9:联合查询

    #进阶9:联合查询 /* union 联合 合并:将多条查询语句的结果合并成一个结果 语法: 查询语句1 union 查询语句2 union..... 应用场景:要查询的结果来自于多个表,且多个表没有 ...