# 4.7 OGNL用法

在MyBatis的**动态SQL**&#x548C;**${}**&#x5F62;式的参数中都用到了OGNL表达式，MyBatis常用的OGNL表达式如下：

* <mark style="color:blue;">逻辑运算符</mark>：and（与）、or（或）、!或not（非）
* <mark style="color:blue;">关系运算符</mark>：==或eq、!=或neq、lt（小于）、lte（小于等于）、gt（大于）、gte（大于等于）
* <mark style="color:blue;">算术运算符</mark>：+（加）、-（减）、\*（乘）、/（除）、%（取余）
* <mark style="color:blue;">obj.method(args)</mark>：调用对象的实例方法
* <mark style="color:blue;">obj.property：</mark>调用对象的实例字段
* <mark style="color:blue;">@class\@method(args)</mark>：调用类的静态方法
* <mark style="color:blue;">@class\@field</mark>：调用类的静态字段
* <mark style="color:blue;">array\[index]</mark>：按索引取值，array可以是List或数组
* <mark style="color:blue;">map\[key]</mark>：按关键字取值，map是一个Map

假设存在一个名为map的Map类型的参数，我们可以通过**map\['userName']**&#x6216;**map.userName**来获取map中key为userName的值，这里一定要注意，<mark style="color:orange;">**不管userName的值是不是null，必须保证userName这个key存在，否则就会报错**</mark>。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bohans.gitbook.io/ji-chu/mybatis/ji-chu-zhi-shi/4.-dong-tai-sql/4.7-ognl-yong-fa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
