or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cache-system.mdindex.mdmapping-annotations.mdplugin-system.mdsession-management.mdtransaction-management.mdtype-handling.md
tile.json

tessl/maven-org-mybatis--mybatis

SQL mapping framework that eliminates JDBC boilerplate and couples objects with stored procedures or SQL statements using XML descriptors or annotations.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.mybatis/mybatis@3.6.x

To install, run

npx @tessl/cli install tessl/maven-org-mybatis--mybatis@3.6.0

index.mddocs/

MyBatis

MyBatis is a Java SQL mapping framework that eliminates almost all of the JDBC code and manual setting of parameters and retrieval of results. It uses XML descriptors or annotations to configure and map primitives, Map interfaces, and Java POJOs to database records.

Package Information

  • Package Name: org.mybatis:mybatis
  • Package Type: maven
  • Language: Java
  • Installation: Add to pom.xml:
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.6.0</version>
    </dependency>

Core Imports

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.io.Resources;

For annotation-based mappers:

import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;

Basic Usage

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.io.Resources;

// 1. Create SqlSessionFactory from XML configuration
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

// 2. Open a session and execute SQL
try (SqlSession session = sqlSessionFactory.openSession()) {
    // Using statement ID with parameter
    User user = session.selectOne("selectUser", 1);
    
    // Using mapper interface (type-safe)
    UserMapper mapper = session.getMapper(UserMapper.class);
    List<User> users = mapper.findAll();
    
    // Commit changes
    session.commit();
}

Architecture

MyBatis is built around several key components:

  • SqlSessionFactory: Factory for creating database sessions, typically configured once per application
  • SqlSession: Primary interface for executing SQL statements and managing transactions
  • Mapper Interfaces: Type-safe interfaces that MyBatis implements dynamically using proxies
  • Configuration: Central configuration object containing all settings, type handlers, and statement mappings
  • Type Handlers: Convert between Java types and JDBC types during parameter setting and result retrieval
  • Cache System: Two-level caching with session-level (L1) and global (L2) cache support
  • Dynamic SQL: XML-based templating system for building conditional SQL statements

Capabilities

Session Management

Core session and transaction management functionality. SqlSession is the primary interface for all database operations.

// Factory for creating sessions
interface SqlSessionFactory {
    SqlSession openSession();
    SqlSession openSession(boolean autoCommit);
    SqlSession openSession(ExecutorType execType);
    Configuration getConfiguration();
}

// Primary database session interface  
interface SqlSession extends Closeable {
    <T> T selectOne(String statement, Object parameter);
    <T> List<T> selectList(String statement, Object parameter);
    int insert(String statement, Object parameter);
    int update(String statement, Object parameter);
    int delete(String statement, Object parameter);
    void commit();
    void rollback();
    <T> T getMapper(Class<T> type);
    void close();
}

Session Management

Mapping Annotations

Annotation-based SQL mapping for declarative database operations. Provides type-safe alternatives to XML configuration.

// Core SQL operation annotations
@interface Select {
    String[] value();
}

@interface Insert {
    String[] value();
}

@interface Update {
    String[] value();
}

@interface Delete {
    String[] value();
}

// Parameter and result mapping
@interface Param {
    String value();
}

@interface Results {
    String id() default "";
    Result[] value();
}

@interface Result {
    String column();
    String property();
    Class<?> javaType() default void.class;
    JdbcType jdbcType() default JdbcType.UNDEFINED;
}

Mapping Annotations

Type Handling

Comprehensive type conversion system between Java types and JDBC types, with extensive built-in support and custom type handler capabilities.

// Core type handler interface
interface TypeHandler<T> {
    void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;
    T getResult(ResultSet rs, String columnName) throws SQLException;
    T getResult(ResultSet rs, int columnIndex) throws SQLException;
    T getResult(CallableStatement cs, int columnIndex) throws SQLException;
}

// Base implementation for custom handlers
abstract class BaseTypeHandler<T> implements TypeHandler<T> {
    public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;
    public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException;
    public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException;
    public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException;
}

Type Handling

Cache System

Multi-level caching system with configurable eviction policies and decorators for performance optimization.

// Core cache interface
interface Cache {
    String getId();
    void putObject(Object key, Object value);
    Object getObject(Object key);
    Object removeObject(Object key);
    void clear();
    int getSize();
}

// Cache configuration annotation
@interface CacheNamespace {
    Class<? extends Cache> implementation() default PerpetualCache.class;
    Class<? extends Cache> eviction() default LruCache.class;
    long flushInterval() default 0;
    int size() default 1024;
    boolean readWrite() default true;
    boolean blocking() default false;
}

Cache System

Transaction Management

Clean abstraction over JDBC and managed transactions with support for different transaction factories.

// Core transaction interface
interface Transaction {
    Connection getConnection() throws SQLException;
    void commit() throws SQLException;
    void rollback() throws SQLException;
    void close() throws SQLException;
}

// Transaction factory interface
interface TransactionFactory {
    void setProperties(Properties props);
    Transaction newTransaction(Connection conn);
    Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit);
}

enum TransactionIsolationLevel {
    NONE, READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
}

Transaction Management

Plugin System

Interceptor-based plugin system for AOP-style cross-cutting concerns like logging, performance monitoring, and custom behavior injection.

// Core plugin interface
interface Interceptor {
    Object intercept(Invocation invocation) throws Throwable;
    default Object plugin(Object target) { return Plugin.wrap(target, this); }
    default void setProperties(Properties properties) {}
}

// Plugin configuration annotations
@interface Intercepts {
    Signature[] value();
}

@interface Signature {
    Class<?> type();
    String method();  
    Class<?>[] args();
}

Plugin System

Core Types

// Execution types
enum ExecutorType {
    SIMPLE,    // Basic statement execution
    REUSE,     // PreparedStatement reuse
    BATCH      // Batch operations
}

// JDBC type enumeration  
enum JdbcType {
    VARCHAR, INTEGER, TIMESTAMP, BOOLEAN, BIGINT, DECIMAL, DATE, TIME, BLOB, CLOB, ARRAY
    // ... and all other standard JDBC types
}

// Auto-mapping behavior
enum AutoMappingBehavior {
    NONE,     // No auto mapping
    PARTIAL,  // Partial auto mapping  
    FULL      // Full auto mapping
}

// Local cache scope
enum LocalCacheScope {
    SESSION,   // Session-scoped caching
    STATEMENT  // Statement-scoped caching
}

// Configuration class containing all MyBatis settings
class Configuration {
    public Environment getEnvironment();
    public void setEnvironment(Environment environment);
    public boolean isSafeRowBoundsEnabled();
    public boolean isMapUnderscoreToCamelCase();
    public boolean isCacheEnabled();
    public ExecutorType getDefaultExecutorType();
    public AutoMappingBehavior getAutoMappingBehavior();
    public LocalCacheScope getLocalCacheScope();
    public JdbcType getJdbcTypeForNull();
    // ... many other configuration properties
}