1.if标签

接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee);

XML中:where 1=1必不可少

<select id="getEmpsByEmpProperties" resultType="com.mybatis.entity.Employee">
select * from t_employee where 1=1
<if test="empId!=null">
and empId=#{empId}
</if>
<if test="empName!=null &amp;&amp; empName.trim()!=&quot;&quot;">
and empName like #{empName}
</if>
<if test="empSex==0 or empSex==1">
and empSex=#{empSex}
</if>
<if test="empAge!=null">
and empAge=#{empAge}
</if>
</select>

2.where标签

接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee);

XML中:

mybatis就会将where标签中拼装的sql,多出来的and或者or去掉,只会去掉第一个多出来的and或者or

<select id="getEmpsByEmpProperties" resultType="com.mybatis.entity.Employee">
select * from t_employee
<where>
<if test="empId!=null">
empId=#{empId}
</if>
<if test="empName!=null &amp;&amp; empName.trim()!=&quot;&quot;">
and empName like concat('%',#{empName},'%')
</if>
<if test="empSex==0 or empSex==1">
and empSex=#{empSex}
</if>
<if test="empAge!=null">
and empAge=#{empAge}
</if>
</where>
</select>

3.set标签

接口中方法:public Integer updateEmp(Employee emp);

XML中:

<update id="updateEmp">
<!-- Set标签的使用 推荐 -->
<!-- update t_employee
<set>
<if test="empName!=null">
empName=#{empName},
</if>
<if test="empSex==0 or empSex==1">
empSex=#{empSex},
</if>
<if test="empAge!=null">
empAge=#{empAge}
</if>
</set>
where empId=#{empId} --> <!-- Trim:更新拼串 -->
update t_employee
<trim prefix="set" suffixOverrides=",">
<if test="empName!=null">
empName=#{empName},
</if>
<if test="empSex==0 or empSex==1">
empSex=#{empSex},
</if>
<if test="empAge!=null">
empAge=#{empAge}
</if>
</trim>
where empId=#{empId}
</update>

4.trim标签

接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee);

XML中:

<select id="getEmpsByEmpProperties" resultType="com.mybatis.entity.Employee">
select * from t_employee
<!-- 后面多出的and或者or where标签不能解决
prefix="":前缀:trim标签体中是整个字符串拼串 后的结果。
prefix给拼串后的整个字符串加一个前缀
prefixOverrides="":
前缀覆盖: 去掉整个字符串前面多余的字符
suffix="":后缀
suffix给拼串后的整个字符串加一个后缀
suffixOverrides=""
后缀覆盖:去掉整个字符串后面多余的字符 -->
<!-- 自定义字符串的截取规则 -->
<trim prefix="where" suffixOverrides="and">
<if test="empId!=null">
empId=#{empId} and
</if>
<if test="empName!=null &amp;&amp; empName!=&quot;&quot;">
empName like #{empName} and
</if>
<if test="empSex==0 or empSex==1">
and empSex=#{empSex}
</if>
<if test="empAge!=null">
and empAge=#{empAge}
</if>
</trim>
</select>

5.foreach标签

5.1 批量查询

接口中方法:public List<Employee> getEmpsByempIdList(@Param("empIdList")List<String> empIdList);

XML中:

<select id="getEmpsByempIdList" resultType="com.mybatis.entity.Employee">
select * from t_employee
<!-- collection:指定要遍历的集合
item:将当前遍历出的元素赋值给指定的变量
separator:每个元素之间的分隔符
open:遍历出所有结果拼接一个开始的字符
close:遍历出所有结果拼接一个结束的字符
-->
<foreach collection="empIdList" item="empId" separator=","
open="where empId in(" close=")">
#{empId}
</foreach>
</select>

5.2 批量插入

接口中方法:public Integer addEmployees(@Param("emps")List<Employee> emps);

XML中:

<insert id="addEmployees">
<!-- 第一种方式:推荐 -->
insert into
t_employee(empId,empName,empSex,empAge)
values
<foreach collection="emps" item="emp" separator=",">
(#{emp.empId},#{emp.empName},#{emp.empSex},#{emp.empAge})
</foreach> <!-- 第二种方式:不推荐 需要数据库连接属性allowMultiQueries=true -->
<!--
<foreach collection="emps" item="emp" separator=";">
insert into t_employee(empId,empName,empSex,empAge)
values(#{emp.empId},#{emp.empName},#{emp.empSex},#{emp.empAge})
</foreach>
-->
</insert>

5.3 批量删除

接口中方法:public Integer deleteEmpsByempIdList(@Param("empIdList")List<String> empIdList);

XML中:

<delete id="deleteEmpsByempIdList">
delete from t_employee
<foreach collection="empIdList" item="empId" separator=","
open="where empId in(" close=")">
#{empId}
</foreach>
</delete>

   6.choose标签

     接口中方法:public List<Employee> getEmpsByChooseCondition(Employee emp);

XML中:

<select id="getEmpsByChooseCondition" resultType="com.mybatis.entity.Employee">
select * from t_employee
<where>
<!-- 如果带了empId就用empId查,如果带了empName就用empName查;只会进入其中一个 -->
<choose>
<when test="empId!=null">
empId=#{empId}
</when>
<when test="empName!=null">
empName like #{empName}
</when>
<otherwise>
empSex = 0
</otherwise>
</choose>
</where>
</select>

总结:mybatis 的动态sql语句是基于OGNL表达式的,主要有以下几类(可以随机组合)

  •   if  简单的条件判断
  •   choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似.
  •   trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)
  •   where (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法错误)
  •   set (主要用于更新时)
  •   foreach (在实现 mybatis in 语句查询时特别有用)

MyBatis探究-----动态SQL详解的更多相关文章

  1. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...

  2. MyBatis的动态SQL详解-各种标签使用

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...

  3. Oracle中动态SQL详解(EXECUTE IMMEDIATE)

    Oracle中动态SQL详解(EXECUTE IMMEDIATE) 2017年05月02日 18:35:48 悠悠倾我心 阅读数:744 标签: oracle动态sqloracle 更多 个人分类:  ...

  4. MyBatis动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...

  5. Java数据持久层框架 MyBatis之API学习七(动态 SQL详解)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  6. 超全MyBatis动态SQL详解!( 看完SQL爽多了)

    MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...

  7. oracle中动态SQL详解

    部分内容参考网上资料 1.静态SQLSQL与动态SQL Oracle编译PL/SQL程序块分为两个种:其一为前期联编(early binding),即SQL语句在程序编译期间就已经确定,大多数的编译情 ...

  8. 动态SQL详解

    动态SQL 在之前用户所编写的PL/SQL程序时有一个最大的特点:就是所操作的数据库对象(例如:表)必须存在,否则创建的子程序就会出问题,而这样的操作在开发之中被称为静态SQL操作,而动态SQL操作可 ...

  9. (转)Oracle中动态SQL详解

    本文转载自:http://www.cnblogs.com/gaolonglong/archive/2011/05/31/2064790.html 1.静态SQLSQL与动态SQL Oracle编译PL ...

随机推荐

  1. Class.forName()+Class.forName().newlnstance()和new语句和初始化块+static初始化块+构造方法之间的关系

    先上代码 class A{     int a;     static {System.out.println("载入类时执行");}     public A() {       ...

  2. windows上react-native run-android时Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED报错

    报错如图 解决 在C:\Users\{用户名}\.gradle\wrapper\dists路径下,删除所有文件夹,重新run-android ps:网上搜了说是说是java解压缩编码格式问题什么的,感 ...

  3. windbg源码驱动调试 + 无源码驱动调试

    windbg源码驱动调试   环境信息 虚拟机:win7 32位 windbg:6.12(版本不存在太大影响) 设置过程 windbg与虚拟机连接:链接 配置windbg 配置好双机调试后,点击win ...

  4. DEV_TreeList使用经验小结

    1. 点击叶子节点是希望Open键显示,点击非叶子节点时希望隐藏.实践中发现点击到了非叶子节点图标,Open没有隐藏,如何解决? 增加一个判断: if (_hitInfo.HitInfoType != ...

  5. webpack打包 基础

    001.什么是webpack? 作用有哪些? WebPack可以看做是模块打包机:它做的事情是,分析你的项目结构,找到JavaScript模块以及其它的一些浏览器不能直接运行的拓展语言(Scss,Ty ...

  6. Java项目中启动Tomcat报错invalid LOC header

    原因: 可能是jar包有问题. 解决方法: 1.找到加载不了的类对应的jar包. 2.在tomcat中webapps/INF/lib中找到对应的jar包,然后删除. 3.重新下载其它版本的jar包. ...

  7. Lecture5_1&5_2.随机变量的数字特征(数学期望、方差、协方差)

    一.数学期望 1.离散型随机变量的数学期望 设X为离散随机变量,其概率分布为:P(X=xk)=pk 若无穷级数$\sum_{k=1}^{+\infty}x_kp_k$绝对收敛 (即满足$\sum_{k ...

  8. 107个JS常用方法(持续更新中)

    1.输出语句:document.write(""); 2.JS中的注释为//3.传统的HTML文档顺序是:document->html->(head,body)4.一个 ...

  9. photoshop cc 2017安装

    https://jingyan.baidu.com/article/8ebacdf0700c0c49f65cd500.html

  10. 为什么不能用 JS 获取剪贴板上的内容?

    为什么不能用 JS 获取剪贴板上的内容? 为什么不能用 JS 获取剪贴板上的内容? 发一串口令给朋友朋友复制这串口令,然后访问你的网站你在网站上用 JS 读取朋友剪贴板上的口令根据不同的口令,显示不同 ...