Comprehensive MyBatis Interview Q&A: Placeholders, XML Tags, DAO Mechanics, Pagination, Plugins, Dynamic SQL, Result Mapping, Associations, Lazy Loading, Executors and More

This article provides detailed answers to 18 common MyBatis interview questions, covering the differences between #{ } and ${ }, XML mapping tags, DAO interface behavior, pagination techniques, plugin development, batch operations, dynamic SQL, result mapping, association handling, lazy loading, executor types, enum mapping, and the internal configuration structure.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Comprehensive MyBatis Interview Q&A: Placeholders, XML Tags, DAO Mechanics, Pagination, Plugins, Dynamic SQL, Result Mapping, Associations, Lazy Loading, Executors and More

1. What is the difference between #{} and ${}?

${}

is a variable placeholder used in Properties files for static text replacement, such as ${driver} being replaced with com.mysql.jdbc.Driver. #{} is an SQL parameter placeholder; MyBatis replaces it with a ? and sets the value via PreparedStatement (e.g., ps.setInt(0, parameterValue)), using reflection to obtain the property value.

2. Besides the common select , insert , update , and delete tags, what other tags exist in an XML mapping file?

Additional tags include <resultMap>, <parameterMap>, <sql>, <include>, <selectKey>, and the nine dynamic SQL tags: trim, where, set, foreach, if, choose, when, otherwise, bind. The <sql> tag defines reusable SQL fragments, while <selectKey> handles key generation for databases without auto‑increment.

3. How does a DAO (Mapper) interface work, and can its methods be overloaded?

The DAO interface’s fully‑qualified name matches the namespace in the XML file, and each method name corresponds to a MappedStatement id. The interface has no implementation class; MyBatis creates a JDK dynamic proxy that maps the method call to the appropriate MappedStatement. Methods cannot be overloaded because the lookup key is the combination of namespace and method name.

4. How does MyBatis handle pagination and what is the principle of pagination plugins?

MyBatis can paginate using the RowBounds object, which performs in‑memory pagination on the result set. Physical pagination is achieved by writing SQL with limit/offset directly or by using a pagination plugin. The plugin intercepts the SQL via MyBatis’s plugin interface, rewrites it according to the database dialect, and adds the appropriate limit clause (e.g., select * from student becomes select t.* from (select * from student) t limit 0,10).

5. Summarize the MyBatis plugin mechanism and how to write a plugin.

Plugins can target four MyBatis core interfaces: ParameterHandler, ResultSetHandler, StatementHandler, and Executor. By implementing the Interceptor interface and overriding intercept(), you can intercept method calls. Annotate the interceptor with @Intercepts to specify which interface and method to intercept, and register the plugin in the MyBatis configuration.

6. Can MyBatis batch inserts return generated primary keys?

Yes, batch inserts can return generated keys because JDBC supports this feature and MyBatis leverages it.

7. What is MyBatis dynamic SQL, which tags are available, and how does it work?

Dynamic SQL allows conditional and programmatic SQL construction within XML using tags such as trim, where, set, foreach, if, choose, when, otherwise, and bind. MyBatis evaluates OGNL expressions against the parameter object, then assembles the final SQL based on the evaluated results.

8. How does MyBatis map query results to objects and what mapping forms exist?

Mapping can be done via a <resultMap> that explicitly defines column‑to‑property relationships, or by using column aliases (e.g., T_NAME AS name) that match object property names. MyBatis creates object instances via reflection and sets properties accordingly.

9. Can MyBatis perform one‑to‑one and one‑to‑many association queries? How are they implemented?

Yes. Associations can be fetched either by issuing separate SQL statements for the related objects or by using a single nested JOIN query. The

<resultMap>
<id>

sub‑tag defines unique identifiers to de‑duplicate parent objects when a join returns multiple rows.

10. Does MyBatis support lazy loading and what is its principle?

MyBatis supports lazy loading for association (one‑to‑one) and collection (one‑to‑many) relationships. When a lazy property is accessed, a CGLIB proxy triggers an additional query to load the data, then sets the property on the original object.

11. Can IDs be duplicated across different XML mapping files?

If a namespace is defined, IDs may be duplicated because the full key is namespace+id. Without a namespace, IDs must be unique across all files.

12. How to execute batch processing in MyBatis?

Batch processing is performed using the BatchExecutor, which accumulates statements via addBatch() and executes them together with executeBatch().

13. What Executor types does MyBatis provide and how do they differ?

MyBatis offers three executors: SimpleExecutor creates a new Statement for each operation; ReuseExecutor reuses Statement objects based on the SQL key; BatchExecutor batches update statements for collective execution.

14. How to specify which Executor to use?

The default executor type can be set in the MyBatis configuration file, or passed as a parameter when creating a SqlSession via DefaultSqlSessionFactory.

15. Can MyBatis map Enum types?

Yes, by implementing a custom TypeHandler that defines setParameter() and getResult() to convert between Java enums and JDBC types.

16. In an XML mapping file, can a tag referenced by &lt;include&gt; be defined after the referencing tag?

Yes. MyBatis parses the entire file, marks unresolved references, and resolves them after the whole file has been read.

17. How do MyBatis XML files map to internal data structures?

All XML configurations are loaded into a Configuration object. <parameterMap> becomes a ParameterMap with ParameterMapping children; <resultMap> becomes a ResultMap with ResultMapping children; each CRUD tag becomes a MappedStatement containing a BoundSql object.

18. Why is MyBatis considered a semi‑automatic ORM tool?

Unlike fully automatic ORM frameworks such as Hibernate, MyBatis requires developers to write explicit SQL for associations and complex queries, providing more control but less automation.

t_id

t_name

s_id

1

teacher

38

1

teacher

39

1

teacher

40

1

teacher

41

1

teacher

42

1

teacher

43

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaPersistenceMyBatisORM
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.