Hibernate / MyBatis Interview questions
What is result mapping?
Result mapping is the process of translating columns in a SQL query's result set into fields or properties of a Java object, which MyBatis can do automatically for simple cases (matching column names to bean property names) or explicitly via a <resultMap> for more complex scenarios.
<resultMap id="userResultMap" type="com.example.model.User"> <id property="id" column="user_id"/> <result property="fullName" column="full_name"/> <result property="email" column="email_address"/> </resultMap> <select id="selectUserById" resultMap="userResultMap"> SELECT user_id, full_name, email_address FROM users WHERE user_id = #{id} </select>
The simple, automatic case (using resultType alone) works well when column names already align with Java bean property names, or when MyBatis's automatic camelCase mapping setting is enabled to bridge snake_case database columns to camelCase Java properties; a resultMap becomes necessary when that automatic alignment isn't sufficient — mismatched names, nested objects, or collections that need explicit structure.
Explicit result maps also support more advanced mapping scenarios like constructor-based mapping (via <constructor>), discriminators for polymorphic result types, and the nested association/collection elements used for mapping related objects, covered in more depth as separate, more advanced topics.
More Related questions...