ibatis2.3.4部分类结构图
SqlMapClientImpl拥有SqlMapExecutorDelegate,ThreadLocal两个对象。还有getLocalSqlMapSession(),openSession()和insert(),update,delete(),queryForObject()几个方法。代码:
public SqlMapExecutorDelegate delegate;
protected ThreadLocal localSqlMapSession = new ThreadLocal();
public SqlMapSession openSession() {
SqlMapSessionImpl sqlMapSession = new SqlMapSessionImpl(this);
sqlMapSession.open();
return sqlMapSession;
}
public SqlMapSession openSession(Connection conn) {
try {
SqlMapSessionImpl sqlMapSession = new SqlMapSessionImpl(this);
sqlMapSession.open();
sqlMapSession.setUserConnection(conn);
return sqlMapSession;
} catch (SQLException e) {
throw new SqlMapException("Error setting user provided connection. Cause: " + e, e);
}
}
protected SqlMapSessionImpl getLocalSqlMapSession() {
SqlMapSessionImpl sqlMapSession = (SqlMapSessionImpl) localSqlMapSession.get();
if (sqlMapSession == null || sqlMapSession.isClosed()) {
sqlMapSession = new SqlMapSessionImpl(this);
localSqlMapSession.set(sqlMapSession);
}
return sqlMapSession;
}
public Object insert(String id, Object param) throws SQLException {
return getLocalSqlMapSession().insert(id, param);
}
public int update(String id, Object param) throws SQLException {
return getLocalSqlMapSession().update(id, param);
}
public int delete(String id, Object param) throws SQLException {
return getLocalSqlMapSession().delete(id, param);
}
public Object queryForObject(String id, Object paramObject) throws SQLException {
return getLocalSqlMapSession().queryForObject(id, paramObject);
}
不难看出SqlMapClientImpl拥有多个SqlMapSessionImpl(它们隶属于不同线程,通过localSqlMapSession 来管理)。
而其中的insert(),update(),delete(),queryForObject()将委托给SqlMapSessionImpl。
那在看看SqlMapSessionImpl部分代码:
protected SqlMapExecutorDelegate delegate;
protected SessionScope sessionScope;
public SqlMapSessionImpl(SqlMapClientImpl client) {
this.delegate = client.getDelegate();
this.sessionScope = this.delegate.beginSessionScope();
this.sessionScope.setSqlMapClient(client);
this.sessionScope.setSqlMapExecutor(client);
this.sessionScope.setSqlMapTxMgr(client);
this.closed = false;
}
public Object insert(String id, Object param) throws SQLException {
return delegate.insert(sessionScope, id, param);
}
public int update(String id, Object param) throws SQLException {
return delegate.update(sessionScope, id, param);
}
public int delete(String id, Object param) throws SQLException {
return delegate.delete(sessionScope, id, param);
}
public Object queryForObject(String id, Object paramObject) throws SQLException {
return delegate.queryForObject(sessionScope, id, paramObject);
}
首先是构造函数,它将初始化一些信息,注意this.delegate = client.getDelegate();说明SqlMapClientImpl,SqlMapSessionImpl所使用的是同一个SqlMapExecutorDelegate。
而它的insert(),update,delete(),queryForObject()这几个方法将委托给SqlMapExecutorDelegate。
看看SqlMapExecutorDelegate代码(只是部分代码,没有贴全):
protected SqlExecutor sqlExecutor;
public int update(SessionScope sessionScope, String id, Object param) throws SQLException {
int rows = 0;
MappedStatement ms = getMappedStatement(id);
Transaction trans = getTransaction(sessionScope);
boolean autoStart = trans == null;
try {
trans = autoStartTransaction(sessionScope, autoStart, trans);
StatementScope statementScope = beginStatementScope(sessionScope, ms);
try {
rows = ms.executeUpdate(statementScope, trans, param);
} finally {
endStatementScope(statementScope);
}
autoCommitTransaction(sessionScope, autoStart);
} finally {
autoEndTransaction(sessionScope, autoStart);
}
return rows;
}
SqlMapExecutorDelegate将做些事务控制,从代码rows = ms.executeUpdate(statementScope, trans, param);可以看出之后将具体操作委托给MappedStatement。
继续跟下去,看下MappedStatement的相关代码:
public int executeUpdate(StatementScope statementScope, Transaction trans, Object parameterObject)
throws SQLException {
ErrorContext errorContext = statementScope.getErrorContext();
errorContext.setActivity("preparing the mapped statement for execution");
errorContext.setObjectId(this.getId());
errorContext.setResource(this.getResource());
statementScope.getSession().setCommitRequired(true);
try {
parameterObject = validateParameter(parameterObject);
Sql sql = getSql();
errorContext.setMoreInfo("Check the parameter map.");
ParameterMap parameterMap = sql.getParameterMap(statementScope, parameterObject);
errorContext.setMoreInfo("Check the result map.");
ResultMap resultMap = sql.getResultMap(statementScope, parameterObject);
statementScope.setResultMap(resultMap);
statementScope.setParameterMap(parameterMap);
int rows = 0;
errorContext.setMoreInfo("Check the parameter map.");
Object[] parameters = parameterMap.getParameterObjectValues(statementScope, parameterObject);
errorContext.setMoreInfo("Check the SQL statement.");
String sqlString = sql.getSql(statementScope, parameterObject);
errorContext.setActivity("executing mapped statement");
errorContext.setMoreInfo("Check the statement or the result map.");
rows = sqlExecuteUpdate(statementScope, trans.getConnection(), sqlString, parameters);// important
errorContext.setMoreInfo("Check the output parameters.");
if (parameterObject != null) {
postProcessParameterObject(statementScope, parameterObject, parameters);
}
errorContext.reset();
sql.cleanup(statementScope);
notifyListeners();
return rows;
} catch (SQLException e) {
errorContext.setCause(e);
throw new NestedSQLException(errorContext.toString(), e.getSQLState(), e.getErrorCode(), e);
} catch (Exception e) {
errorContext.setCause(e);
throw new NestedSQLException(errorContext.toString(), e);
}
}
protected int sqlExecuteUpdate(StatementScope statementScope, Connection conn, String sqlString, Object[] parameters) throws SQLException {
if (statementScope.getSession().isInBatch()) {
getSqlExecutor().addBatch(statementScope, conn, sqlString, parameters);
return 0;
} else {
return getSqlExecutor().executeUpdate(statementScope, conn, sqlString, parameters);
}
}
public SqlExecutor getSqlExecutor() {
return sqlMapClient.getSqlExecutor();
}
哦,executeUpdate(StatementScope statementScope, Transaction trans, Object parameterObject)中的
rows = sqlExecuteUpdate(statementScope, trans.getConnection(), sqlString, parameters);将调用
sqlExecuteUpdate(StatementScope statementScope, Connection conn, String sqlString, Object[] parameters),
而sqlExecuteUpdate(StatementScope statementScope, Connection conn, String sqlString, Object[] parameters)
将调用SqlExecutor的executeUpdate来进行具体操作。而这个SqlExecutor是来自SqlMapExecutorDelegate的。
所以我们在做ibatis分页的时候可以通过反射向SqlMapExecutorDelegate强制注入自定义的SqlExecutor来达到分页效果。
- 大小: 33.7 KB
分享到:
相关推荐
ibatis的原码 ibatis源码 ibatis源码 ibatis源码
iBATIS框架源码剖析源码 iBATIS框架源码剖析源码 iBATIS框架源码剖析源码
iBATIS框架源码剖析
iBATIS框架源码剖析的第二部分,下了他就下完了
ibatis源码
iBATIS一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2001年发起的开放源代码项目。于2010年6月16号被谷歌托管,改名为MyBatis。是一个基于SQL映射支持Java和·NET的持久层框架。
ibatis 2.3.4 的源码 public abstract Object insert(String paramString, Object paramObject) throws SQLException; public abstract Object insert(String paramString) throws SQLException; public ...
mybatis的前身ibatis源码剖析
ibatis2.3源码.可以直接与myeclipse进行关联的那种,有需要的朋友可以看看。如果先进行评论再下载的话,就相当于是免费的。我会陆续上传其它源码的。
springmvc+ibatis写的开发源码,对学习springmvc+ibatis的框架搭建和配置有帮助,下载后能够在eclipse中运行。
iBATIS框架源码剖析 共两个压缩包,这是第一个 iBATIS框架源码剖析.part1.rar
iBATIS框架源码剖析 共两个压缩包,这是第二个 iBATIS框架源码剖析.part2.rar
ibatis3源码+驱动 。
iBATIS框架源码剖析有两部分哦,注意下载!!
ibatis2.3 源码
ibatis 框架源码剖析 书籍源代码 带有详尽注释 .这个是我买的书,其实书的内容简单,大部分的内容都在源代码注释中
ibatis源码 学习参考 对于学习ibatis很有帮助
NULL 博文链接:https://jsufly.iteye.com/blog/508249