ibatis部分源码窥探

ibatis2.3.4部分类结构图

[img]http://dl.iteye.com/upload/attachment/607817/2a8babd7-06c9-3085-a437-4bf6d370dde8.jpg[/img]

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来达到分页效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值