Hint
This article is generated by AI translation.
Rule Parameters
Pass parameters and invoke the built-in rule engine in SQL using the @{...} syntax. This allows SQL statements to change dynamically based on parameter values without writing complex if/else logic or XML tags.
Why Use Rules
- Simple and Intuitive: Get rid of complex XML tags (like MyBatis's
<if>,<foreach>), and let SQL return to SQL. - Smart Processing: Automatically handle null checks, prefix/suffix processing (such as automatically removing extra AND/OR or commas).
- Powerful: Built-in rich rules such as
and,or,in,set,case, etc., meet most dynamic SQL needs.
Basic Example
Take the most common AND rule as an example. It can intelligently decide whether to append query conditions based on whether the parameter (:name) is empty:
-- Use @{and, ...} rule
select * from users where id > :id @{and, name = :name}
- If
nameis not empty: Generates... where id > ? and name = ? - If
nameis empty: The rule is automatically ignored, generating... where id > ?
More Rules
dbVisitor provides a wealth of built-in rules (such as in collection query, set dynamic update, case branch judgment, etc.), and even supports custom rules.
- Dynamic SQL Rules - Includes
@{and}@{or}@{in}@{set}@{case}, etc. - Result Processing Rules - Rules for post-processing result sets.
- Nested Rules - Introduces how to implement complex logic through nested rules.