Hibernate / MyBatis Interview questions
What is a Mapper XML file?
A Mapper XML file defines the actual SQL statements for a given mapper namespace, associating each statement with an id that corresponds to a method on the matching Mapper interface, along with the parameter and result type information MyBatis needs to bind inputs and map outputs correctly.
<mapper namespace="com.example.mapper.UserMapper"> <select id="selectUserById" parameterType="int" resultType="com.example.model.User"> SELECT id, name, email FROM users WHERE id = #{id} </select> </mapper>
The namespace attribute must match the fully-qualified name of the corresponding Mapper interface, and each statement's id must match a method name on that interface exactly — this naming correspondence is how MyBatis links a Java method call to the specific SQL statement that should execute when that method is invoked.
Beyond simple statements like the example above, Mapper XML files also support dynamic SQL elements (<if>, <foreach>, <choose>) and complex result mappings (<resultMap>) for nested objects and collections, which is where much of MyBatis's flexibility for handling non-trivial queries and mappings actually lives.
More Related questions...