1、if用法

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

2、choose

  1. <select id="selectUser" resultType="com.forest.owl.entity.User">
  2. select * from user
  3. where 1=1 /*此处不可忽略*/
  4. <choose>
  5. <when test="id != null">
  6. and id=#{id}
  7. </when>
  8. <when test="userName != null and userName !='' ">
  9. and user_name=#{userName}
  10. </when>
  11. <otherwise>
  12. and 1=2 /*此处不可忽略*/
  13. </otherwise>
  14. </choose>
  15. </select>

 3、where

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

  1. <select id="selectUser" resultType="com.forest.owl.entity.User">
  2. select * from user
  3. <where>
  4. <if test="userName != null and userName != '' ">
  5. and user_name like concat('%', #{userName}, '%')
  6. </if>
  7. <if test="userEmail != null and userEmail != '' ">
  8. and user_email = #{userEmail}
  9. </if>
  10. </where>
  11. </select>

 4、set

  1. <update id="updateUserById">
  2. update user
  3. <set>
  4. <if test="userName != null and userName != '' ">
  5. user_name=#{userName},
  6. </if>
  7. id=#{id} /*此处不可忽略*/
  8. </set>
  9. where id=#{id}
  10. </update>

 5、foreach

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

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

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

参数为List的情况

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

参数为Map的情况

  1. <update id="updateByMap">
  2. update user
  3. set
  4. <foreach collection="_parameter" item="val" index="key" separator=",">
  5. ${key}=#{val}
  6. </foreach>
  7. where id=#{id}
  8. </update>

6、bind

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

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

可以通过bind改写成

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

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

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

 8、鉴别器映射(discriminator)

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

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

  1. <resultMap id="UserAndRole" extends="BaseResultMap" type="com.forest.owl.entity.User">
  2. <discriminator javaType="int" column="enabled">
  3. <" resultMap="resultMap1"/>
  4. <" resultMap="resultMap2"/>
  5. </discriminator>
  6. </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. 对C语言指针的理解

    一个小程序引发对于C语言指针的思考: #include <bits/stdc++.h> using namespace std; void my_swap (int* a,int* b) ...

  2. $(window).scroll()无法触发问题

    在微信端开发中遇到一个这种问题:明明用的公共文件(代码如下图),其他页面每次都能触发这个滚动条$(window).scroll事件,以显示右下角“回到顶部”这个按钮图标 但是,问题来了,最该需要使用“ ...

  3. B/S与C/S的优缺点

     B/S:Browser/Server,即浏览器/服务器架构,一般用于网站: 优点:无需安装,不需要更新客户端: 缺点:交互性差,安全性低. C/S:Client/Server,即客户端/服务器端架构 ...

  4. 编译原理作业(第一次)-完成retinf.c(阉割版)

    首先,作业要求概括如下: 根据前缀表达式文法,实现statements() 和expression() 两个函数. 并且要求使得语义分析在完成分析前缀表达式并输出中间代码的同时,也能够将前缀表达式翻译 ...

  5. 学习笔记DL001:数学符号、深度学习的概念

    数学符号. 数和数组.

  6. [NLP]非终结字符集&终结字符集

    参考:终结符和非终结符 终结字符集: 不能单独出现在推导式左边的符号, 不能够再继续推导. 非终结字符集: 不是终结字符集中的符号都为非终结字符集. 是可拆分元素. 例子: 文法如下: S->A ...

  7. [R] [Johns Hopkins] R Programming 作業 Week 2 - Air Pollution

    Introduction For this first programming assignment you will write three functions that are meant to ...

  8. pip安装报错 解决办法

    安装库时报错:Could not fetch URL https://pypi.python.org/simple/wheel/: 解决办法:  pip --trusted-host pypi.pyt ...

  9. AJAX实现登陆

    先添加点击事件 <input type="button" id="submitt" value="立即登录" /> 展示信息 & ...

  10. java中移位运算

    转自: https://blog.csdn.net/wk1134314305/article/details/74891419