Hibernate / MyBatis Interview questions
What is the difference between resultType and resultMap?
Both attributes tell MyBatis how to map a query's result set to Java objects, but they differ in how that mapping is expressed: resultType is a shorthand for simple, automatic mapping, while resultMap references an explicit, reusable mapping definition.
| resultType | resultMap |
| Inline attribute naming the target class directly. | References a separately defined <resultMap> by id. |
| Relies on automatic column-to-property name matching. | Explicit, hand-specified column-to-property mapping. |
| Cannot express nested associations or collections. | Supports nested association and collection mapping. |
| Best for simple, flat queries with matching names. | Best for complex, nested, or reused mapping logic. |
A statement can use exactly one of the two, never both at once, since they represent two different ways of specifying the same underlying concern; a common practical pattern is starting with resultType for simple queries and switching to a defined resultMap only once a query's result shape actually needs the extra structure or reuse that resultType alone can't express.
More Related questions...