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);

}

}

相关文章
相关标签/搜索