Introduction to MyBatis: Architecture, Core Components, and Execution Process
This article introduces MyBatis, a Java persistence framework that separates SQL from code, outlines its key features, explains its layered architecture, and details the core components such as SqlSessionFactory, SqlSession, MapperProxy, Executor, StatementHandler, ParameterHandler, and ResultSetHandler with code examples.
MyBatis is a class‑persistence framework that separates SQL statements from Java code, reduces JDBC boilerplate, and supports XML and annotation configurations.
Key features include decoupling SQL from business logic, lightweight design, support for native SQL and dynamic statements, and comprehensive ORM mapping.
The overall architecture consists of an interface layer (Mapper/DAO), a data‑processing layer (configuration parsing, SQL parsing, execution), and a foundational support layer (reflection, type conversion, logging, resource loading, cache, plugins, environments, etc.).
Core components: SqlSessionFactory builds SqlSession instances and loads mybatis‑config.xml via Resources.getResourceAsStream. SqlSession provides CRUD methods and transaction control. MapperProxy uses dynamic proxies to bind mapper interfaces to SQL statements. Executor (SimpleExecutor, ReuseExecutor, BatchExecutor, CachingExecutor) executes statements and manages caching. StatementHandler (SimpleStatementHandler, PreparedStatementHandler, CallableStatementHandler) creates and configures JDBC Statements. ParameterHandler (DefaultParameterHandler) sets parameters on PreparedStatement. ResultSetHandler (DefaultResultSetHandler) maps ResultSet rows to result objects.
Example code for creating a session:
InputStream is = Resources.getResourceAsStream("myBatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(is);
SqlSession sqlSession = factory.openSession();Custom object factory example:
public class ExampleObjectFactory extends DefaultObjectFactory {
public Object create(Class type) { return super.create(type); }
public Object create(Class type, List<Class> constructorArgTypes, List<Object> constructorArgs) {
return super.create(type, constructorArgTypes, constructorArgs);
}
public void setProperties(Properties properties) { super.setProperties(properties); }
public <T> boolean isCollection(Class<T> type) { return Collection.class.isAssignableFrom(type); }
}Configuration elements such as <properties>, <settings>, <typeAliases>, <plugins>, and <environments> control behavior, type handling, and environment selection.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Full-Stack Internet Architecture
Introducing full-stack Internet architecture technologies centered on Java
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
