MyBatis主要包含的核心部件
- SqlSession : Mybatis工作的主要顶层API,表示和数据库交互的会话,完成必要数据库增删改查功能。这是MyBatis主要的一个类,用来执行SQL,获取映射器,管理事务。通常情况下,在程序中使用的Mybatis的API就是这个接口定义的方法。
- Executor: Mybatis执行器,是Mybatis调度的核心,负责SQL语句的生成和查询缓存的维护
- StatementHandler: 封装了JDBC Statement操作,负责对JDBC statement的操作,如设置参数、将Statement结果集转换成List集合。
- ParameterHandler :参数处理器,负责对用户传递的参数转换成JDBC Statement所需要的参数
- ResultSetHandler :结果集处理器,负责将JDBC返回的ResultSet结果集对象转换成List类型的集合
- TypeHandler: 类型处理器,负责java数据类型和jdbc数据类型之间的映射和转换
- MappedStatement :映射语句,维护了一条<select|update|delete|insert>节点的封装
- SqlSource: SQL源码,负责根据用户传递的parameterObject,动态地生成SQL语句,将信息封装到BoundSql对象中,并返回
- BoundSql:绑定的SQL,是从SqlSource而来,将动态内容都处理完得到的SQL语句字符串,其中包括?,还有绑定的参数
- Configuration:Mybatis所有的配置信息都维护在Configuration对象之中
- MapperProxyFactory:映射器代理工厂,主要是使用jdk代理mapper的实现以实现调用SqlSessoin,接口类名称对应配置文件的namespace的值,等标签的id对应方法名称。
关系图如下(图片来源于网络)
SqlSession源码
package org.apache.ibatis.session;
/**
* 这是MyBatis主要的一个类,用来执行SQL,获取映射器,管理事务,是顶层API接口
*
* 通常情况下,我们在应用程序中使用的Mybatis的API就是这个接口定义的方法。
*
*/
public interface SqlSession extends Closeable {
/**
* Retrieve a single row mapped from the statement key
* 根据指定的SqlID获取一条记录的封装对象
* @param <T> the returned object type 封装之后的对象类型
* @param statement sqlID
* @return Mapped object 封装之后的对象
*/
<T> T selectOne(String statement);
/**
* Retrieve a single row mapped from the statement key and parameter.
* 根据指定的SqlID获取一条记录的封装对象,只不过这个方法容许我们可以给sql传递一些参数
* 一般在实际使用中,这个参数传递的是pojo,或者Map或者ImmutableMap
* @param <T> the returned object type
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @return Mapped object
*/
<T> T selectOne(String statement, Object parameter);
/**
* Retrieve a list of mapped objects from the statement key and parameter.
* 根据指定的sqlId获取多条记录
* @param <E> the returned list element type
* @param statement Unique identifier matching the statement to use.
* @return List of mapped object
*/
<E> List<E> selectList(String statement);
/**
* Retrieve a list of mapped objects from the statement key and parameter.
* 获取多条记录,这个方法容许我们可以传递一些参数
* @param <E> the returned list element type
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @return List of mapped object
*/
<E> List<E> selectList(String statement, Object parameter);
/**
* Retrieve a list of mapped objects from the statement key and parameter,
* within the specified row bounds.
* 获取多条记录,这个方法容许我们可以传递一些参数,不过这个方法容许我们进行
* 分页查询。
*
* 需要注意的是默认情况下,Mybatis为了扩展性,仅仅支持内存分页。也就是会先把
* 所有的数据查询出来以后,然后在内存中进行分页。因此在实际的情况中,需要注意
* 这一点。
*
* 一般情况下公司都会编写自己的Mybatis 物理分页插件
* @param <E> the returned list element type
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @param rowBounds Bounds to limit object retrieval
* @return List of mapped object
*/
<E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds);
/**
* The selectMap is a special case in that it is designed to convert a list
* of results into a Map based on one of the properties in the resulting
* objects.
* Eg. Return a of Map[Integer,Author] for selectMap("selectAuthors","id")
* 将查询到的结果列表转换为Map类型。
* @param <K> the returned Map keys type
* @param <V> the returned Map values type
* @param statement Unique identifier matching the statement to use.
* @param mapKey The property to use as key for each value in the list. 这个参数会作为结果map的key
* @return Map containing key pair data.
*/
<K, V> Map<K, V> selectMap(String statement, String mapKey);
/**
* The selectMap is a special case in that it is designed to convert a list
* of results into a Map based on one of the properties in the resulting
* objects.
* 将查询到的结果列表转换为Map类型。这个方法容许我们传入需要的参数
* @param <K> the returned Map keys type
* @param <V> the returned Map values type
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @param mapKey The property to use as key for each value in the list.
* @return Map containing key pair data.
*/
<K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey);
/**
* The selectMap is a special case in that it is designed to convert a list
* of results into a Map based on one of the properties in the resulting
* objects.
* 获取多条记录,加上分页,并存入Map
* @param <K> the returned Map keys type
* @param <V> the returned Map values type
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @param mapKey The property to use as key for each value in the list.
* @param rowBounds Bounds to limit object retrieval
* @return Map containing key pair data.
*/
<K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds);
/**
* Retrieve a single row mapped from the statement key and parameter
* using a {@code ResultHandler}.
*
* @param statement Unique identifier matching the statement to use.
* @param parameter A parameter object to pass to the statement.
* @param handler ResultHandler that will handle each retrieved row
* @return Mapped object
*/
void select(String statement, Object parameter