Skip to main content
Hint

This article is generated by AI translation.

Macro Rules

These rules change the final SQL sent to the database, such as argument-driven sorting or computing sharded table names.

RuleDescription
@{macro, name}MACRO runs the SQL macro named name; macros can contain predefined SQL.
@{ifmacro, testExpr, name}IFMACRO is MACRO with a guard; when testExpr is true, the macro is executed.
@{iftext, testExpr, content}IFTEXT inserts content verbatim into SQL when testExpr is true.

MACRO / IFMACRO

MACRO inlines predefined SQL macros into the final SQL. Referencing a missing macro raises an error at execution.

1. Register a SQL macro
DynamicContext registry = jdbcTemplate.getRegistry();
registry.addMacro("includeSeq", "and seq = :seq");
2. Use macro in query
select * from users where
status = :status
@{macro, includeSeq}
3. Final executed SQL
select * from users where status = :status and seq = :seq

IFMACRO adds a guard expression.

Use IFMACRO
select * from users where
status = :status
@{ifmacro, status > 2, includeSeq}
  • The macro is applied only when status > 2.

IFTEXT

IFTEXT inserts content verbatim when testExpr is true.

1. Usage
select * from users where
status = :status
@{iftext, status > 2, and age = 36 }
2. When status > 2
select * from users where status = :status and age = 36
3. When status <= 2
select * from users where status = :status
info

content is inserted as raw text. If it contains arguments, consider other rules.