Hibernate / MyBatis Interview questions
List the dynamic SQL elements available in MyBatis?
MyBatis provides a specific set of XML tags for building dynamic SQL, each addressing a different conditional or structural need common in real-world queries.
| Element | Purpose |
| <if> | Conditionally includes a SQL fragment based on a test expression |
| <choose>/<when>/<otherwise> | Switch-like conditional logic, picking exactly one matching branch |
| <where> | Intelligently inserts WHERE and strips a leading AND/OR if needed |
| <set> | Intelligently inserts SET and strips a trailing comma for UPDATE statements |
| <trim> | General-purpose prefix/suffix trimming, the basis <where>/<set> build on |
| <foreach> | Iterates over a collection, useful for IN clauses and batch operations |
| <bind> | Defines a variable from an OGNL expression for reuse within the statement |
The <where> and <set> elements exist specifically to solve a common annoyance with hand-rolled conditional SQL: without them, a query where the first <if> condition happens to be false would produce invalid SQL starting with a stray AND right after WHERE; these elements automatically detect and strip that kind of leading/trailing boilerplate, which is why they're preferred over manually writing WHERE 1=1 workarounds.
More Related questions...