Java persistence framework
- Semi-ORM / SQL-mapping framework.
- No auto generate SQL
- Query MUST write manually in xml
- Full control SQL.
- Must write SQL manually in XML or annotations.
- Maps SQL results to Java objects.
- Supports first-level cache (SqlSession scoped) and second-level cache (mapper scoped).
- Easy integration: Works via
mybatis-spring-boot-starter.
// Unlike **JPA/Hibernate (**fully ORM) that tries to auto map entire db to objects.
Components
SqlSessionFactory
- Creates
SqlSession objects.
- Configured via XML or Java configuration.
- Reads
mybatis-config.xml for settings, type aliases, mappers, etc.
SqlSession
- Represents a single database session.
- Used to execute queries, commits, rollbacks.
- Always needs to be closed (can use try-with-resources).