当前位置: 代码迷 >> 综合 >> MyBatis框架 第5章:MyBatis 动态SQL
  详细解决方案

MyBatis框架 第5章:MyBatis 动态SQL

热度:37   发布时间:2024-01-22 00:55:13.0

第5章:MyBatis 动态SQL

5.1 MyBatis动态SQL简介

  1. 动态 SQL是MyBatis强大特性之一。极大的简化我们拼装SQL的操作
  2. 动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似
  3. MyBatis 采用功能强大的基于 OGNL 的表达式来简化操作

if

choose (when, otherwise)

trim (where, set)

foreach

      4.  OGNL( Object Graph Navigation Language )对象图导航语言,这是一种强大的

表达式语言,通过它可以非常方便的来操作对象属性。 类似于我们的EL,SpEL等

访问对象属性: person.name

调用方法:     person.getName()

调用静态属性/方法: @java.lang.Math@PI

        @java.util.UUID@randomUUID()

调用构造方法: new com.atguigu.bean.Person(‘admin’).name

运算符:      +,-*,/,%

逻辑运算符:  in,not in,>,>=,<,<=,==,!=

注意:xml中特殊符号如”,>,<等这些都需要使用转义字符

5.2 if  where

  1. If用于完成简单的判断.
  2. Where用于解决SQL语句中where关键字以及条件中第一个and或者or的问题

<select id="getEmpsByConditionIf" resultType="com.atguigu.mybatis.beans.Employee">

select id , last_name ,email  , gender  

from tbl_employee

<where>

<if test="id!=null">

and id = #{id}

</if>

<if test="lastName!=null && lastName!=""">

and last_name = #{lastName}

</if>

<if test="email!=null and email.trim()!=''">

and email = #{email}

  相关解决方案