Master MyBatis Interview Questions: #{} vs ${}, XML Mapping, Plugins, and More

This article provides concise, interview‑style explanations of MyBatis core concepts—including the difference between #{} and ${}, additional XML tags, DAO interface mechanics, pagination, plugin development, batch operations, executors, lazy loading, enum mapping, and internal configuration mapping—helping developers ace backend Java ORM interviews.

ITFLY8 Architecture Home
ITFLY8 Architecture Home
ITFLY8 Architecture Home
Master MyBatis Interview Questions: #{} vs ${}, XML Mapping, Plugins, and More

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

${}

is a placeholder in properties files for static text replacement, such as com.mysql.jdbc.Driver. #{} is a SQL parameter placeholder; MyBatis replaces it with a ? and sets the value via PreparedStatement, e.g., #{item.name} maps to param.getItem().getName().

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

Additional tags include:

<resultMap>
<parameterMap>
<sql>
<include>
<selectKey>

Dynamic SQL provides nine tags: trim|where|set|foreach|if|choose|when|otherwise|bind. The <include> tag inserts SQL fragments, while <selectKey> handles primary‑key generation for databases without auto‑increment.

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

The DAO interface, often called a Mapper, has a fully‑qualified name that matches the XML namespace. Each method name corresponds to a

MappedStatement
id

. MyBatis creates a JDK dynamic‑proxy for the interface; when a method is invoked, the proxy looks up the corresponding MappedStatement and executes its SQL. Methods cannot be overloaded because the lookup key is the fully‑qualified interface name plus method name.

4. How does MyBatis perform pagination and what is the pagination plugin’s principle?

MyBatis can paginate using the RowBounds object, which performs in‑memory pagination on the result set, or by writing physical pagination SQL directly. A pagination 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 t._ from (select * from student) t limit 0,10).

5. What is the principle behind MyBatis plugins and how to write one?

Plugins can target four core interfaces: ParameterHandler, ResultSetHandler, StatementHandler, and Executor. MyBatis uses JDK dynamic proxies; the InvocationHandler ’s invoke() method intercepts calls to the selected methods. To create a plugin, implement the Interceptor interface, override intercept(), annotate the class with @Intercepts specifying the target interface and methods, and register the plugin in the MyBatis configuration.

6. Can MyBatis return generated primary keys for batch inserts?

Yes. Since JDBC supports retrieving generated keys, MyBatis can also return the list of primary keys after a batch insert.

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

Dynamic SQL lets developers build SQL statements conditionally within XML using tags such as trim|where|set|foreach|if|choose|when|otherwise|bind. MyBatis evaluates OGNL expressions against the parameter object, concatenates the appropriate fragments, and produces the final SQL.

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

Result mapping can be done via a <resultMap> that explicitly defines column‑to‑property relationships, or by using column aliases that match property names (e.g., T_NAME AS name). MyBatis creates object instances via reflection and assigns values based on the mapping; unmapped properties remain unset.

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

Yes. Associations can be fetched either by separate SQL statements (multiple queries) or by a single join query (nested query). When a join returns duplicate rows, MyBatis uses the <id> element(s) in the <resultMap> to deduplicate parent objects.

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

MyBatis supports lazy loading for association (one‑to‑one) and collection (one‑to‑many) relationships. When a lazy‑loaded property is accessed, a CGLIB proxy intercepts the call, detects a null value, executes the pre‑configured SQL to fetch the related data, sets the property, and returns the result.

11. Can id values be duplicated across different XML mapping files?

If a namespace is defined, id values may repeat because the key is namespace + id. Without a namespace, duplicate id s cause conflicts.

12. How does MyBatis execute batch operations?

Batch operations are 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?

SimpleExecutor

: creates a new Statement for each operation and closes it immediately. ReuseExecutor: reuses Statement objects by caching them in a map keyed by SQL. BatchExecutor: batches update statements, deferring execution until executeBatch() is called, similar to JDBC batch processing.

14. How to specify which Executor MyBatis should use?

The default ExecutorType can be set in the MyBatis configuration file, or passed as a parameter when obtaining a SqlSession from the DefaultSqlSessionFactory.

15. Can MyBatis map Enum types?

Yes. By implementing a custom TypeHandler that defines setParameter() and getResult(), MyBatis can convert between Java enums and JDBC column values.

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

Yes. MyBatis parses the file sequentially, marks the referencing tag as unresolved, continues parsing, and after all tags are read, it revisits unresolved tags, allowing forward references.

17. How does MyBatis map XML configuration to its internal data structures?

All XML configurations are loaded into a heavyweight Configuration object. <parameterMap> becomes a ParameterMap with ParameterMapping children; <resultMap> becomes a ResultMap with ResultMapping children; and <select>, <insert>, <update>, <delete> become MappedStatement objects whose SQL is stored as BoundSql.

18. Why is MyBatis considered a semi‑automatic ORM tool compared to fully automatic solutions?

Frameworks like Hibernate automatically generate the necessary SQL based on object relationships, providing full automation. MyBatis requires developers to write the SQL manually for associations and collections, offering more control but less automation, thus it is termed a semi‑automatic ORM.

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.

JavaSQLMyBatisORMinterview
ITFLY8 Architecture Home
Written by

ITFLY8 Architecture Home

ITFLY8 Architecture Home - focused on architecture knowledge sharing and exchange, covering project management and product design. Includes large-scale distributed website architecture (high performance, high availability, caching, message queues...), design patterns, architecture patterns, big data, project management (SCRUM, PMP, Prince2), product design, and more.

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.