# 自定义查询字段

## 使用场景

当遇到现有的字段映射方法无法支持的SQL语句时，可以使用注解`@QueryField`自定义SQL语句，作为临时方案。

## 注解定义

{% code title="QueryField.java" %}

```java
@Target(FIELD)
@Retention(RUNTIME)
public @interface QueryField {
    String and();
}
```

{% endcode %}

{% hint style="info" %}
当被注解字段的值满足过滤条件时，and变量里定义的条件语句将会被拼接到SQL中。
{% endhint %}

## 代码示例

**业务代码：**

```java
public class TestQuery extends PageQuery {
    @QueryField(and = "(username = ? OR email = ? OR mobile = ?)")
    private String account;
}
```

**单元测试：**

```java
@Test
void testQueryField() {
    TestQuery testQuery = TestQuery.builder().account("test").build();
    ArrayList<Object> argList = new ArrayList<>();

    String sql = BuildHelper.buildWhere(testQuery, argList);

    assertThat(sql).isEqualTo(" WHERE (username = ? OR email = ? OR mobile = ?)");
    assertThat(argList).containsExactly("test", "test", "test");
}
```

`TestQuery`的account字段通过`@QueryField`注解定义的查询条件被原样拼接到WHERE语句，并且因为查询条件里有3个占位符，account的值"test"也被三次添加到argList。


---

# 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://query.docs.doyto.win/zh/object-concepts/query-object/custom-condition-field.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.
