1、if用法

<select id="selectUser" resultType="com.forest.owl.entity.User">
    select * from user
    where 1=1
    <if test="userName != null and userName != '' ">
        and user_name like concat('%', #{userName}, '%')
    </if>
    <if test="userPhone != null and userPhone !='' ">
        and user_phone=#{userPhone}
    </if>
</select>

2、choose

<select id="selectUser" resultType="com.forest.owl.entity.User">
    select * from user
    where 1=1 /*此处不可忽略*/
    <choose>
        <when test="id != null">
            and id=#{id}
        </when>
        <when test="userName != null and userName !='' ">
            and user_name=#{userName}
        </when>
        <otherwise>
            and 1=2 /*此处不可忽略*/
        </otherwise>
    </choose>
</select>

 3、where

where标签内如果没有符合条件的选项,则最后生成的sql语句不含where;如果有符合条件的,则生成的sql语句会自动去除两端的and

<select id="selectUser" resultType="com.forest.owl.entity.User">
    select * from user
    <where>
        <if test="userName != null and userName != '' ">
        and user_name like concat('%', #{userName}, '%')
        </if>
        <if test="userEmail != null and userEmail != '' ">
        and user_email = #{userEmail}
        </if>
    </where>
</select>

 4、set

<update id="updateUserById">
    update user
    <set>
        <if test="userName != null and userName != '' ">
        user_name=#{userName},
        </if>
        id=#{id}  /*此处不可忽略*/
    </set>
    where id=#{id}
</update>

 5、foreach

collection:必填,值为要迭代循环的属性名。

item:变量名,值为要从迭代对象中取出的每一个值

index:索引的属性名。在集合数组下为索引值,在Map对象下为Map的key值。

参数为List的情况

<select id="selectByIdList" resultType="com.forest.owl.entity.User">
    select * from user
    where id in
    <foreach collection="list" open="(" close=")" separator="," item="id" index="i">
    #{id}
    </foreach>
</select>

参数为Map的情况

    <update id="updateByMap">
        update user
        set
        <foreach collection="_parameter" item="val" index="key" separator=",">
            ${key}=#{val}
        </foreach>
        where id=#{id}
    </update>

6、bind

bind标签可以使用OGML表达式创建一个变量病绑定到上下文中。

<if test="userName != null and userName != '' ">
and user_name like concat('%', #{userName}, '%')
</if>

可以通过bind改写成

<if test="userName != null and userName != '' ">
    <bind name="userNameLike" value=" '%' + userName + '%' "/>
    and user_name #{userNameLike}
</if>

7、如果测试的时候想知道映射XML中方法执行的参数 可以这么做:

public class StringUtil{
    public static void print(Object parameter){
        System.out.println(parameter);
    }
}
<bind name="print" value="@com.forest.owl.util.StringUtil@print(_parameter)" />

 8、鉴别器映射(discriminator)

有时单独一个映射会需要返回不同数据类型的结果集,discriminator就是为了用来处理这种情况。

因为感觉这个标签不会很常用,所以不做进一步了解,暂时给出简单的代码,后续有需要再回来翻阅:

  <resultMap id="UserAndRole" extends="BaseResultMap" type="com.forest.owl.entity.User">
    <discriminator javaType="int" column="enabled">
      <" resultMap="resultMap1"/>
      <" resultMap="resultMap2"/>
    </discriminator>
  </resultMap>

mybatis入门篇:mybatis动态SQL的更多相关文章

  1. Spring Boot MyBatis升级篇-注解-动态SQL(if test)-方案二:@Provider(8)

    1)动态语言注解(2)@Provider使用思路(3)@SelectProvider小试牛刀(4)@SelectProvider初露锋芒(5)@SelectProvider过关斩将(6)@Insert ...

  2. 持久层之 MyBatis: 第二篇 :动态SQL And多表查询

    MyBatis入门到精通 完整CRUD UserDaoImpl 编写UserDao对应的UserDaoMapper.xml 添加UserDao的测试用例 编写UserDao的测试用例 解决数据库字段名 ...

  3. mybatis入门基础(五)----动态SQL

    一:动态SQL 1.1.定义 mybatis核心对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 1.2.案例需求 用户信息综合查询列表这个statement的定义使用动态s ...

  4. Mybatis第三篇【动态SQL】

    动态SQL 何为动态SQL??回顾一下我们之前写的SSH项目中,有多条件查询的情况,如下图 我们当时刚开始做的时候,是需要在Controller中判断SQL是否已经有条件了,因为SQL语句需要拼接起来 ...

  5. MyBatis学习总结_11_MyBatis动态Sql语句

    MyBatis中对数据库的操作,有时要带一些条件,因此动态SQL语句非常有必要,下面就主要来讲讲几个常用的动态SQL语句的语法 MyBatis中用于实现动态SQL的元素主要有: if choose(w ...

  6. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL

  7. SSM框架之Mybatis(6)动态SQL

    Mybatis(6)动态SQL 1.动态SQL 出现原因:有些时候业务逻辑复杂时,我们的 SQL 是动态变化的,此时在前面的学习中我们的 SQL 就不能满足要求了 1.1.if标签 我们根据实体类的不 ...

  8. Spring mybatis源码篇章-动态SQL节点源码深入

    通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-动态SQL基础语法以及原理 前话 前文描述到通过mybatis默认的解析驱动类org.apache.ibat ...

  9. SSM框架开发web项目系列(四) MyBatis之快速掌握动态SQL

    前言 通过前面的MyBatis部分学习,已经可以使用MyBatis独立构建一个数据库程序,基本的增删查改/关联查询等等都可以实现了.简单的单表操作和关联查询在实际开的业务流程中一定会有,但是可能只会占 ...

  10. Mybatis分页查询与动态SQL

    一.Mybatis的分页查询 由于第一二节较为详细讲述了Mybatis的环境搭建,文件配置,SQL编写和Java代码实现,所以接下来的讲述都将只抽取关键代码和mapper文件中的关键sql,详细的流程 ...

随机推荐

  1. CSS学习笔记_day7

    目录 1.径向渐变 2. 线性渐变 3. 背景图片裁剪 4. 背景图片大小 5. 边框图片 6. 边框圆角 7. 边框阴影 8.位移 9. 倾斜 10. 旋转 11. 缩放 12. 文本换行 13. ...

  2. SSM面试题

    一.Spring面试题 1.Spring 在ssm中起什么作用? Spring:轻量级框架 作用:Bean工厂,用来管理Bean的生命周期和框架集成. 两大核心: 1.IOC/DI(控制反转/依赖注入 ...

  3. PAT乙级考前总结(三)

    特殊题型 1027 打印沙漏 (20 分) 题略,感觉有点像大学里考试的题.找规律即可. #include <stdio.h>#include <iostream>using ...

  4. hdu 4506 快速幂

    小明自从告别了ACM/ICPC之后,就开始潜心研究数学问题了,一则可以为接下来的考研做准备,再者可以借此机会帮助一些同学,尤其是漂亮的师妹.这不,班里唯一的女生又拿一道数学题来请教小明,小明当然很高兴 ...

  5. MySQL-01-基础

    大纲 1)mysql存储结构: 数据库 -> 表 -> 数据   sql语句 2)管理数据库: 增加: create database 数据库 default character utf8 ...

  6. JavaScript DOM&BOM

    1.DOM含义 D: Document 文档 一份文档就是一棵节点树,每个节点都是一个对象O:Object 对象 JavaScript语言里对象可以分为三种类型: (1)用户定义的对象(user-de ...

  7. 密码疑云 (2)——RSA加密机制需要的数学知识

    在公钥密码体制提出不久,人们就找到其中的三种,其中最著名的当属RSA体制.RSA是一种非对称加密体制,在公开密钥加密和电子商业中被广泛使用.RSA是1977年由罗纳德·李维斯特(Ron Rivest) ...

  8. flutter Row 垂直或水平放置多个widget

    使用行(Row)水平排列widget,使用列(Column)垂直排列widget.在行或列中嵌套行或列实现复杂的布局.如下图所示: 此布局按行排列.该行包含两个子布局,左侧一列和右侧的图片 对于行(R ...

  9. Ubuntu下重新安装软件 配置文件不重新生成得问题解决

    apt-get remove nfs dpkg -P nfs apt-get install nfs 按照先remove然后dpkg -P再重新install的顺序.

  10. 使用命令查看 Laravel 的版本

    进入项目根目录文件夹后,进入命令行,输入命令: php artisan --version 或者输入: php artisan 会出现 artisan 的帮助文档,最上面就是 laravel 的版本号