Hibernate / MyBatis Interview questions
What is a Mapper interface in MyBatis?
A Mapper interface is a plain Java interface whose methods correspond to SQL statements defined either in a matching XML mapper file or directly via annotations, and MyBatis automatically generates a working implementation of that interface at runtime — no hand-written implementation class is needed.
public interface UserMapper { User selectUserById(int id); List<User> selectAllUsers(); int insertUser(User user); }
Retrieving a usable instance of a mapper is done through SqlSession.getMapper(UserMapper.class), which returns a dynamically generated proxy object; calling a method on that proxy triggers MyBatis to look up the matching SQL statement (by fully-qualified interface and method name, matched against a mapper's namespace and statement id), execute it, and map the result back to the method's declared return type.
This pattern is what lets application code interact with the data access layer using ordinary, statically-typed Java method calls — catching a mismatched method signature at compile time — while the actual SQL execution and result mapping happen transparently behind the interface, without a developer ever writing a class that implements UserMapper directly.
More Related questions...