Hibernate / MyBatis Interview questions
What is the Mapper proxy pattern, and how does MyBatis use it internally?
MyBatis generates a working implementation of a Mapper interface at runtime using Java's dynamic proxy mechanism, rather than requiring a hand-written implementation class — when application code calls session.getMapper(UserMapper.class), what's actually returned is a proxy object implementing that interface, backed by an InvocationHandler that intercepts every method call.
When a method is called on the proxy, the InvocationHandler (MyBatis's internal MapperProxy class) intercepts the call, looks up the MappedStatement matching that interface's fully-qualified name plus the called method's name, and delegates the actual work to the underlying SqlSession's selectOne, selectList, insert, update, or delete methods as appropriate for the statement type.
This is what lets MyBatis provide a fully statically-typed data access API — the compiler checks method signatures against the interface just like any other Java code — while the actual implementation is generated and wired up entirely at runtime, without a developer ever writing (or needing to keep in sync) a concrete class implementing the Mapper interface by hand.
More Related questions...