Prev Next

Hibernate / MyBatis Interview questions

Explain the internal working of MyBatis's dynamic SQL parsing?

When MyBatis loads a Mapper XML file containing dynamic SQL elements, it doesn't treat the statement as a plain string; instead, it parses the XML structure into a tree of SqlNode objects — one node type per dynamic SQL element — that gets evaluated fresh for every statement execution, since the actual resulting SQL can differ depending on the parameters passed in.

flowchart TD A[Mapper XML with if/choose/foreach elements parsed at startup] --> B[Built into a tree of SqlNode objects] B --> C[MixedSqlNode holds child nodes: TextSqlNode, IfSqlNode, ForEachSqlNode, etc.] D[Statement executed with actual parameters] --> E[SqlNode tree traversed, each node's apply method called] E --> F[IfSqlNode evaluates its OGNL test expression against parameters] F --> G{Condition true?} G -- Yes --> H[Child SQL fragment appended to output] G -- No --> I[Fragment skipped] H --> J[Final assembled SQL string produced] I --> J J --> K[Parameter markers extracted, PreparedStatement built]

Each dynamic element type maps to a corresponding SqlNode implementation — IfSqlNode, ForEachSqlNode, ChooseSqlNode, and so on — and a parent MixedSqlNode holds the overall tree structure; when a statement executes, this tree is traversed, with elements like IfSqlNode evaluating their OGNL test expression against the actual runtime parameter object to decide whether to include their contained SQL fragment in the final assembled output.

This tree is built once when the mapper XML is parsed at startup (an expensive, one-time operation), but traversed fresh on every single statement execution (a comparatively cheap operation), which is the same general build-once/execute-many pattern used throughout MyBatis's architecture to keep per-query overhead low despite the flexibility dynamic SQL provides.

What structure does MyBatis parse dynamic SQL XML into?
When is the SqlNode tree built, versus when is it traversed?

More Related questions...

What is MyBatis? What is the purpose of MyBatis? What are the key features of MyBatis? What is the difference between MyBatis and Hibernate? What is a SqlSessionFactory in MyBatis? What is a SqlSession in MyBatis? What is a Mapper interface in MyBatis? What is the mybatis-config.xml file used for? What is a Mapper XML file? How do you write a basic SELECT statement in MyBatis? What is parameter binding in MyBatis? What is result mapping? What are the supported statement types in MyBatis? What is the difference between #{} and ${} in MyBatis? What is a resultMap? How do you configure a data source in MyBatis? What is MyBatis-Spring? How do you integrate MyBatis with Spring Boot? What is dynamic SQL in MyBatis? List the dynamic SQL elements available in MyBatis? What is the difference between MyBatis and JPA/Hibernate in terms of philosophy? Why is #{} preferred over ${} for parameter binding? How does MyBatis prevent SQL injection? What is the difference between resultType and resultMap? What is the @Param annotation used for in MyBatis? Explain how MyBatis handles one-to-many mappings? Explain how MyBatis handles many-to-one mappings? What is the difference between association and collection in resultMap? What is lazy loading in MyBatis, and how do you configure it? Explain the lifecycle of a SqlSession? What is the difference between SqlSessionFactory and SqlSessionFactoryBuilder? How does MyBatis's first-level cache work? How does MyBatis's second-level cache work? What is the difference between first-level and second-level cache? Explain how to implement a custom TypeHandler in MyBatis? What is the purpose of the if, choose, when, otherwise dynamic SQL elements? Explain the internal working of the foreach element for batch operations? How do you handle batch inserts/updates in MyBatis? What is the Mapper proxy pattern, and how does MyBatis use it internally? Explain the execution flow of a MyBatis query from mapper call to result? What is the difference between MyBatis annotations and XML mapping? How do you use MyBatis with multiple data sources? Explain the role of the Executor in MyBatis's internal architecture, including the SIMPLE, REUSE, and BATCH executor types? How does MyBatis handle transactions, and how does it integrate with Spring's transaction management? What is a plugin/interceptor in MyBatis, and how do you write one? Explain how pagination is typically implemented in MyBatis? What is the N+1 select problem, and how does it relate to MyBatis? How do you troubleshoot a MyBatis mapping exception? Explain the internal working of MyBatis's dynamic SQL parsing? Explain the execution flow of MyBatis-Spring-Boot-Starter's auto-configuration?
Show more question and Answers...


Comments & Discussions