mybatis中使用SqlSessionManager進行insert操做

SqlSessionManager的相關使用

 

 

private ThreadLocal<SqlSession> localSqlSession = new ThreadLocal<SqlSession>();sql

public static SqlSessionManager newInstance(Reader reader) {緩存

return new SqlSessionManager(new SqlSessionFactoryBuilder().build(reader, null, null));app

}ide

public void startManagedSession() {ui

this.localSqlSession.set(openSession());this

}spa

    使用sqlSessionManager調用相關實現接口的方法,sqlSessionFactory中獲取Configuration,Configuration中獲取mapper,MapperProxyFacotry獲取MapperProxy,在調用jdk的動態代理生成相關類的調用.net

 

manager.getMapper代理

@Override接口

public <T> T getMapper(Class<T> type) {

return getConfiguration().getMapper(type, this);

}

 

@Override

public Configuration getConfiguration() {

return sqlSessionFactory.getConfiguration();

}

MapperProxyFactory->MapperProxy->Proxy->用JDK自帶的動態代理生成映射器Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy)

 

public <T> T getMapper(Class<T> type, SqlSession sqlSession) {

final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);

if (mapperProxyFactory == null) {

throw new BindingException("Type " + type + " is not known to the MapperRegistry.");

}

try {

return mapperProxyFactory.newInstance(sqlSession);

} catch (Exception e) {

throw new BindingException("Error getting mapper instance. Cause: " + e, e);

}

}

 

    MapperProxy調用指定的MapperMethod . execute方法,經過SqlCommandType的type類型執行不一樣的sqlSession中的方法

這裏的insert方法調用sqlSession.insert方法調用DefaultSqlSession方法

@Override

public int insert(String statement, Object parameter) {

//insert也是調用update

return update(statement, parameter);

}

 

//核心update

@Override

public int update(String statement, Object parameter) {

try {

//每次要更新以前,dirty標誌設爲true

dirty = true;

MappedStatement ms = configuration.getMappedStatement(statement);

//轉而用執行器來update結果

return executor.update(ms, wrapCollection(parameter));

} catch (Exception e) {

throw ExceptionFactory.wrapException("Error updating database. Cause: " + e, e);

} finally {

ErrorContext.instance().reset();

}

}

 

CachingExecutor

 

@Override

public int update(MappedStatement ms, Object parameterObject) throws SQLException {

//刷新緩存完再update

flushCacheIfRequired(ms);

return delegate.update(ms, parameterObject);

}

 

BaseExecutor

//SqlSession.update/insert/delete會調用此方法

@Override

public int update(MappedStatement ms, Object parameter) throws SQLException {

ErrorContext.instance().resource(ms.getResource()).activity("executing an update").object(ms.getId());

if (closed) {

throw new ExecutorException("Executor was closed.");

}

//先清局部緩存,再更新,如何更新交由子類,模板方法模式

clearLocalCache();

return doUpdate(ms, parameter);

}

 

SimpleExecutor

//update

@Override

public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {

Statement stmt = null;

try {

Configuration configuration = ms.getConfiguration();

//新建一個StatementHandler

//這裏看到ResultHandler傳入的是null

StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);

//準備語句

stmt = prepareStatement(handler, ms.getStatementLog());

//StatementHandler.update

return handler.update(stmt);

} finally {

closeStatement(stmt);

}

}

相關文章
相關標籤/搜索