Skip to main content

6.4 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 name is not empty: Generates ... where id > ? and name = ?
  • If name is 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.

👉 Click to view Rule System detailed documentation