Mybatis internals
本文介绍 Mybatis 以及 spring-mybatis 的架构与实现
mybatis
SqlSessionprimary interface for working with Mybatis. Through this interface to execute commands,get mappers and manage transactions.SqlSessionFactorycreate aSqlSessionout of connection or dataSource.
interface SqlSession  {
+ void select(String,Object,ResultHandler)
+ int insert(String)
+ int update(String)
+ int delete(String)
+ void commit()
+ void rollback()
+ void close()
+ void clearCache()
+ Configuration getConfiguration()
+ Connection getConnection()
}
interface SqlSessionFactory  {
    + openSession(): SqlSession
    + getConfiguration(): Configuration
    }
    SqlSessionFactory -> SqlSession: create
class DefaultSqlSession implements SqlSession {
    Configuration configuration
    Executor executor
    boolean autoCommit
    boolean dirty
    List<Cursor<?>> cursorList
}
class DefaultSqlSessionFactory implements SqlSessionFactory {
  + DefaultSqlSessionFactory(Configuration): 
  - configuration: Configuration
}
class Configuration {
    Environment environment
    Map<String, MappedStatement> mappedStatements
    ProxyFactory proxyFactory
    InterceptorChain interceptorChain
}
class Environment {
  - transactionFactory: TransactionFactory
  - dataSource: DataSource
}
class InterceptorChain {
    List<Interceptor> interceptors
}
interface Interceptor {
    Object intercept(Invocation invocation)
}
DefaultSqlSession o-- Configuration
DefaultSqlSessionFactory o-- Configuration
Configuration *-- InterceptorChain
Configuration *-- Environment
InterceptorChain o-- Interceptor
mybatis-spring
SqlSessionTemplateThread-safe,Spring managedSqlSessionthat works with Spring transaction management to ensure that the actual SqlSession used is the one associated with the current Spring transaction.SqlSessionInterceptorused internally bySqlSessionTemplateto interceptDefaultSqlSession, to control mybatis session life cycle.
package mybatis {
    interface SqlSession  {
        
    }
    class DefaultSqlSession implements SqlSession {
}
 
    interface SqlSessionFactory {}
    
    SqlSessionFactory -> SqlSession: create
}
package mybatis-spring {
    
    class SqlSessionTemplate {
        - exceptionTranslator: PersistenceExceptionTranslator
        - executorType: ExecutorType
        - sqlSessionFactory: SqlSessionFactory
        - sqlSessionProxy: SqlSession
    }
    class SqlSessionInterceptor implements InvocationHandler
}
SqlSession <|-right. SqlSessionTemplate
SqlSessionTemplate *-- SqlSessionInterceptor
SqlSessionInterceptor -left-> DefaultSqlSession: intercept
pageHelper
interface SqlSession  {
+ void select(String,Object,ResultHandler)
+ int insert(String)
+ int update(String)
+ int delete(String)
+ void commit()
+ void rollback()
+ void close()
+ void clearCache()
+ Configuration getConfiguration()
+ Connection getConnection()
}
class DefaultSqlSession implements SqlSession {
    Configuration configuration
    Executor executor
    boolean autoCommit
    boolean dirty
    List<Cursor<?>> cursorList
}
class Configuration {
    Map<String, MappedStatement> mappedStatements
    InterceptorChain interceptorChain
}
class InterceptorChain {
    List<Interceptor> interceptors
}
DefaultSqlSession o-- Configuration
Configuration *-- InterceptorChain
interface Interceptor {
    Object intercept(Invocation invocation)
}
package pageHelper {
    class PageInterceptor  {
        Dialect dialect
    }
interface Dialect  {
}
class AbstractDialect implements Dialect{
}
class AbstractRowBoundsDialect extends AbstractDialect {
}
class MySqlRowBoundsDialect extends AbstractRowBoundsDialect{
}
PageInterceptor *-- Dialect
}
InterceptorChain *-- Interceptor
Interceptor <|--  PageInterceptor