博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MyBatis之SqlSession
阅读量:4203 次
发布时间:2019-05-26

本文共 6207 字,大约阅读时间需要 20 分钟。

MyBatis官方文档

  • ORM框架

SqlSession创建过程

  • SqlSessionFactoryBuilder —》 SqlSessionFactory —》 SqlSession

作用

  • MyBatis的核心接口
  • 通过这个接口,你可以执行SQL命令、获取mapper实例、进行事务管理

范围和生命周期

每个线程都应该有它自己的 SqlSession 实例。SqlSession 的实例不是线程安全的,因此是不能被共享的,所以它的最佳的范围是请求或方法范围。

源码

package org.apache.ibatis.session;import java.io.Closeable;import java.sql.Connection;import java.util.List;import java.util.Map;import org.apache.ibatis.cursor.Cursor;import org.apache.ibatis.executor.BatchResult;/** * The primary Java interface for working with MyBatis. * Through this interface you can execute commands, get mappers and  * manage transactions. * @author Clinton Begin */public interface SqlSession extends Closeable {
/** * Retrieve a single row mapped from the statement key */
T selectOne(String statement); /** * Retrieve a single row mapped from the statement key and * parameter. */
T selectOne(String statement, Object parameter); /** * Retrieve a list of mapped objects from the statement key * and parameter. */
List
selectList(String statement); /** * Retrieve a list of mapped objects from the statement key * and parameter. */
List
selectList(String statement, Object parameter); /** * Retrieve a list of mapped objects from the statement key and * parameter. */
List
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. */
Map
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
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
selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds); /** * A Cursor offers the same results as a List, except it fetches data lazily using an Iterator. */
Cursor
selectCursor(String statement); /** * A Cursor offers the same results as a List, except it fetches data lazily using an Iterator. */
Cursor
selectCursor(String statement, Object parameter); /** * A Cursor offers the same results as a List, except it fetches data lazily using an Iterator. */
Cursor
selectCursor(String statement, Object parameter, RowBounds rowBounds); /** * Retrieve a single row mapped from the statement key and parameter * using a {@code ResultHandler}. */ void select(String statement, Object parameter, ResultHandler handler); /** * Retrieve a single row mapped from the statement * using a {@code ResultHandler}. */ void select(String statement, ResultHandler handler); /** * Retrieve a single row mapped from the statement key and parameter * using a {@code ResultHandler} and {@code RowBounds} */ 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. */ int insert(String statement, Object parameter); /** * Execute an update 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 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. */ 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. */ 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. */ int delete(String statement, Object parameter); /** * 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. * @return BatchResult list of updated records * @since 3.0.6 */ List
flushStatements(); /** * Closes the session */ @Override void close(); /** * Clears local session cache */ void clearCache(); /** * Retrieves current configuration * @return Configuration */ Configuration getConfiguration(); /** * Retrieves a mapper. * @param
the mapper type * @param type Mapper interface class * @return a mapper bound to this SqlSession */
T getMapper(Class
type); /** * Retrieves inner database connection * @return Connection */ Connection getConnection();}

转载地址:http://jnvli.baihongyu.com/

你可能感兴趣的文章