Hibernate / MyBatis Interview questions
What is the difference between MyBatis annotations and XML mapping?
Both approaches define the same underlying concept — SQL statements tied to Mapper interface methods — but they differ in where that definition lives and how well each scales to more complex mapping needs.
| Annotations | XML Mapping |
| SQL written directly on the interface method, e.g. @Select. | SQL written in a separate Mapper XML file. |
| Keeps interface and SQL physically together. | Separates interface (Java) from SQL (XML) into different files. |
| Becomes unwieldy for complex dynamic SQL or long queries. | Well suited to dynamic SQL tags and long, complex statements. |
| Good for simple, short, one-line statements. | Good for anything non-trivial: joins, dynamic SQL, complex result maps. |
The two approaches can be mixed within the same application — and even the same mapper interface, in principle — though most teams settle on a consistent convention (annotations for simple lookups, XML for everything more complex) rather than switching styles arbitrarily per statement, since consistency makes a codebase easier to navigate for anyone unfamiliar with a specific mapper.
More Related questions...