您好,登錄后才能下訂單哦!
/**
* 這是MyBatis用來執(zhí)行SQL的類,獲取映射器,管理事務(wù)
*
*/
public interface SqlSession extends Closeable {
/**
* Retrieve a single row mapped from the statement key
* 根據(jù)指定的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.
* 根據(jù)指定的SqlID獲取一條記錄的封裝對象,只不過這個(gè)方法容許我們可以給sql傳遞一些參數(shù)
* 一般在實(shí)際使用中,這個(gè)參數(shù)傳遞的是pojo,或者M(jìn)ap或者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.
* 根據(jù)指定的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.
* 獲取多條記錄,這個(gè)方法容許我們可以傳遞一些參數(shù)
* @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.
* 獲取多條記錄,這個(gè)方法容許我們可以傳遞一些參數(shù),不過這個(gè)方法容許我們進(jìn)行
* 分頁查詢。
*
* 需要注意的是默認(rèn)情況下,Mybatis為了擴(kuò)展性,僅僅支持內(nèi)存分頁。也就是會先把
* 所有的數(shù)據(jù)查詢出來以后,然后在內(nèi)存中進(jìn)行分頁。因此在實(shí)際的情況中,需要注意
* 這一點(diǎn)。
*
* @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")
* 將查詢到的結(jié)果列表轉(zhuǎn)換為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. 這個(gè)參數(shù)會作為結(jié)果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.
* 將查詢到的結(jié)果列表轉(zhuǎn)換為Map類型。這個(gè)方法容許我們傳入需要的參數(shù)
* @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, ResultHandler handler);
/**
* Retrieve a single row mapped from the statement
* using a {@code ResultHandler}.
* 獲取一條記錄,并轉(zhuǎn)交給ResultHandler處理。這個(gè)方法容許我們自己定義對
* 查詢到的行的處理方式。
* @param statement Unique identifier matching the statement to use.
* @param handler ResultHandler that will handle each retrieved row
* @return Mapped object
*/
void select(String statement, ResultHandler handler);
/**
* Retrieve a single row mapped from the statement key and parameter
* using a {@code ResultHandler} and {@code RowBounds}
* 獲取一條記錄,加上分頁,并轉(zhuǎn)交給ResultHandler處理
* @param statement Unique identifier matching the statement to use.
* @param rowBounds RowBound instance to limit the query results
* @param handler ResultHandler that will handle each retrieved row
* @return Mapped object
*/
void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler);
/**
* Execute an insert statement.
* 插入記錄。
* @param statement Unique identifier matching the statement to execute.
* @return int The number of rows affected by the insert.
*/
int insert(String statement);
/**
* Execute an insert statement with the given parameter object. Any generated
* autoincrement values or selectKey entries will modify the given parameter
* object properties. Only the number of rows affected will be returned.
* 插入記錄,容許傳入?yún)?shù)。
* @param statement Unique identifier matching the statement to execute.
* @param parameter A parameter object to pass to the statement.
* @return int The number of rows affected by the insert. 注意返回的是受影響的行數(shù)
*/
int insert(String statement, Object parameter);
/**
* Execute an update statement. The number of rows affected will be returned.
* 更新記錄。返回的是受影響的行數(shù)
* @param statement Unique identifier matching the statement to execute.
* @return int The number of rows affected by the update.
*/
int update(String statement);
/**
* Execute an update statement. The number of rows affected will be returned.
* 更新記錄
* @param statement Unique identifier matching the statement to execute.
* @param parameter A parameter object to pass to the statement.
* @return int The number of rows affected by the update. 返回的是受影響的行數(shù)
*/
int update(String statement, Object parameter);
/**
* Execute a delete statement. The number of rows affected will be returned.
* 刪除記錄
* @param statement Unique identifier matching the statement to execute.
* @return int The number of rows affected by the delete. 返回的是受影響的行數(shù)
*/
int delete(String statement);
/**
* Execute a delete statement. The number of rows affected will be returned.
* 刪除記錄
* @param statement Unique identifier matching the statement to execute.
* @param parameter A parameter object to pass to the statement.
* @return int The number of rows affected by the delete. 返回的是受影響的行數(shù)
*/
int delete(String statement, Object parameter);
//以下是事務(wù)控制方法,commit,rollback
/**
* Flushes batch statements and commits database connection.
* Note that database connection will not be committed if no updates/deletes/inserts were called.
* To force the commit call {@link SqlSession#commit(boolean)}
*/
void commit();
/**
* Flushes batch statements and commits database connection.
* @param force forces connection commit
*/
void commit(boolean force);
/**
* Discards pending batch statements and rolls database connection back.
* Note that database connection will not be rolled back if no updates/deletes/inserts were called.
* To force the rollback call {@link SqlSession#rollback(boolean)}
*/
void rollback();
/**
* Discards pending batch statements and rolls database connection back.
* Note that database connection will not be rolled back if no updates/deletes/inserts were called.
* @param force forces connection rollback
*/
void rollback(boolean force);
/**
* Flushes batch statements.
* 刷新批處理語句,返回批處理結(jié)果
* @return BatchResult list of updated records
* @since 3.0.6
*/
List<BatchResult> flushStatements();
/**
* Closes the session
* 關(guān)閉Session
*/
@Override
void close();
/**
* Clears local session cache
* 清理Session緩存
*/
void clearCache();
/**
* Retrieves current configuration
* 得到配置
* @return Configuration
*/
Configuration getConfiguration();
/**
* Retrieves a mapper.
* 得到映射器
* 這個(gè)巧妙的使用了泛型,使得類型安全
* 到了MyBatis 3,還可以用注解,這樣xml都不用寫了
* @param <T> the mapper type
* @param type Mapper interface class
* @return a mapper bound to this SqlSession
*/
<T> T getMapper(Class<T> type);
/**
* Retrieves inner database connection
* 得到數(shù)據(jù)庫連接
* @return Connection
*/
Connection getConnection();
}
SqlSession是一個(gè)接口類,其實(shí)際實(shí)現(xiàn)類為DefaultSqlSession
// DefaultSqlSession屬性
public class DefaultSqlSession implements SqlSession {
private Configuration configuration;
private Executor executor;
/**
* 是否自動(dòng)提交
*/
private boolean autoCommit;
private boolean dirty;
}
DefaultSqlSession最為關(guān)鍵的是Executor executor,Executor為一個(gè)接口類,DefaultSqlSession對外提供的接口,內(nèi)部均調(diào)用Executor executor.
public interface Executor {
//不需要ResultHandler
ResultHandler NO_RESULT_HANDLER = null;
//更新
int update(MappedStatement ms, Object parameter) throws SQLException;
//查詢,帶分頁,帶緩存,BoundSql
<E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;
//查詢,帶分頁
<E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException;
//刷新批處理語句
List<BatchResult> flushStatements() throws SQLException;
//提交和回滾,參數(shù)是是否要強(qiáng)制
void commit(boolean required) throws SQLException;
void rollback(boolean required) throws SQLException;
//創(chuàng)建CacheKey
CacheKey createCacheKey(MappedStatement ms, Object parameterObject, RowBounds rowBounds, BoundSql boundSql);
//判斷是否緩存了
boolean isCached(MappedStatement ms, CacheKey key);
//清理Session緩存
void clearLocalCache();
//延遲加載
void deferLoad(MappedStatement ms, MetaObject resultObject, String property, CacheKey key, Class<?> targetType);
Transaction getTransaction();
void close(boolean forceRollback);
boolean isClosed();
void setExecutorWrapper(Executor executor);
}
在mybatis源碼中,Executor的子類為BaseExecutor,BaseExecutor是執(zhí)行器的基類
public abstract class BaseExecutor implements Executor {
private static final Log log = LogFactory.getLog(BaseExecutor.class);
protected Transaction transaction;
protected Executor wrapper;
//延遲加載隊(duì)列(線程安全)
protected ConcurrentLinkedQueue<DeferredLoad> deferredLoads;
//本地緩存機(jī)制(Local Cache)防止循環(huán)引用(circular references)和加速重復(fù)嵌套查詢(一級緩存)
//本地緩存
protected PerpetualCache localCache;
//本地輸出參數(shù)緩存
protected PerpetualCache localOutputParameterCache;
protected Configuration configuration;
//查詢堆棧
protected int queryStack = 0;
private boolean closed;
}
BatchExecutor、CachingExecutor、ReuseExecutor、SimpleExecutor都是BaseExecutor的子類
// 批量操作執(zhí)行器
public class BatchExecutor extends BaseExecutor {
public static final int BATCH_UPDATE_RETURN_VALUE = Integer.MIN_VALUE + 1002;
private final List<Statement> statementList = new ArrayList<Statement>();
private final List<BatchResult> batchResultList = new ArrayList<BatchResult>();
private String currentSql;
private MappedStatement currentStatement;
}
// 緩存執(zhí)行器
public class CachingExecutor implements Executor {
private Executor delegate;
private TransactionalCacheManager tcm = new TransactionalCacheManager();
}
// 可重用執(zhí)行器
public class ReuseExecutor extends BaseExecutor {
//可重用的執(zhí)行器內(nèi)部用了一個(gè)map,key為執(zhí)行的sql,value為sql對應(yīng)的Statement
private final Map<String, Statement> statementMap = new HashMap<String, Statement>();
}
// 簡單執(zhí)行器
public class SimpleExecutor extends BaseExecutor {
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。