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

最新文章

  1. java 开发业务逻辑的思考(1)- 通知短信发送
  2. C# 一个页面,多个Updatepannel,多个Timer
  3. 【集合框架】JDK1.8源码分析之ArrayList(六)
  4. 在 MVC4 中使用 Uploadify 3.2 - 1
  5. Abot爬虫和visjs
  6. 【LeetCode】5. Longest Palindromic Substring 最大回文子串
  7. Android读取Manifest文件下Application等节点下的metadata自定义数据
  8. KISSY对vm的抽取
  9. 服务器 Disk full
  10. 目标文件obj的各段 2
  11. 开窗函数over
  12. sql server 锁与事务拨云见日(上)
  13. nginx配置文件注释说明
  14. golang xorm框架的使用
  15. 【Atcoder ARC060F】最良表現 / Best Representation
  16. npm install xxx --save-dev 与npm install xxx --save 的区别
  17. LeetCode108.将有序数组转换为二叉搜索树
  18. String和StringBuffer和StringBuilder
  19. hibernate中3个重要的类 Configuration SessionFactory Session
  20. SharePoint Development - Custom List using Visual Studio 2010 based SharePoint 2010

热门文章

  1. MySQL基础篇——安装、管理
  2. JS中的继承(原型链、构造函数、组合式、class类)
  3. luogu P1307 数字反转 x
  4. 2019 上海网络赛 J stone name (01背包)
  5. ReactNative的学习笔记
  6. php面试专题---7、文件及目录处理考点
  7. 2017工业软件top100
  8. Eclipse总是自动跳到ThreadPoolExecutor
  9. 修改linux文件的mtime
  10. c++函数overload 的歧义匹配