一、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. Factors and Multiples

    Factors and Multiples   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Y ...

  2. Jmeter中if 控制器的使用

    使用if控制器有两种方式:1.不勾选“interpret condition as variable expression”直接输入我们需要判断的表达式即可,判断表达式为真时,执行if控制器下的请求, ...

  3. HDU 4321 Arcane Numbers 2

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4321 ----------------------------------------------- ...

  4. ruby在类中访问@,类外访问调用方法

    class Box def initialize(w,h) @width,@height=w,h end def printWidth puts @width end def printHeight ...

  5. swiper在vue中的用法

    首先通过npm i vue-awesome-swiper --save 来在vue中下载插件 然后再main.js中引入 require('swiper/dist/css/swiper.css')im ...

  6. Vagrant 手册之 Provisioning - Shell 配置程序

    原文地址 Provisioner 命令:"shell" 示例: node.vm.provision "shell" do |s| s.inline = < ...

  7. python实现自动发送邮件

    Python发送邮件成功的前提,应是先开启授权码.目前使用广泛的邮箱有:163邮箱.qq邮箱等. 163邮箱开启授权码的方法如下图: qq邮箱开启授权码的方法如下图: 接下来代码的实现: import ...

  8. Bootstrap 学习笔记8 下拉菜单滚动监听

    代码部分: <nav class="navbar navbar-default"> <a href="#" class="navba ...

  9. Marriage Match II 【HDU - 3081】【并查集+二分答案+最大流】

    题目链接 一开始是想不断的把边插进去,然后再去考虑我们每次都加进去边权为1的边,直到跑到第几次就没法继续跑下去的这样的思路,果不其然的T了. 然后,就是想办法咯,就想到了二分答案. 首先,我们一开始处 ...

  10. Linux安装redis服务器和部署

    Linux安装redis和部署 第一步:下载安装包 wget http://download.redis.io/releases/redis-5.0.5.tar.gz 访问https://redis. ...