【Java开源代码栏目提醒】:网学会员为需要Java开源代码的朋友们搜集整理了CachingStatement.java相关资料,希望对各位网友有所帮助!
/*
* Copyright 2004 Clinton Begin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibatis.sqlmap.engine.mapping.statement;
import com.ibatis.sqlmap.client.event.RowHandler;
import com.ibatis.sqlmap.engine.cache.CacheKey;
import com.ibatis.sqlmap.engine.cache.CacheModel;
import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
import com.ibatis.sqlmap.engine.mapping.sql.Sql;
import com.ibatis.sqlmap.engine.scope.StatementScope;
import com.ibatis.sqlmap.engine.transaction.Transaction;
import java.sql.SQLException;
import java.util.List;
//
public class CachingStatement extends MappedStatement {
private MappedStatement statement;
private CacheModel cacheModel;
public CachingStatement(MappedStatement statement, CacheModel cacheModel) {
this.statement = statement;
this.cacheModel = cacheModel;
}
public String getId() {
return statement.getId();
}
public StatementType getStatementType() {
return statement.getStatementType();
}
public Integer getResultSetType() {
return statement.getResultSetType();
}
public Integer getFetchSize() {
return statement.getFetchSize();
}
public ParameterMap getParameterMap() {
return statement.getParameterMap();
}
public ResultMap getResultMap() {
return statement.getResultMap();
}
public int executeUpdate(StatementScope statementScope, Transaction trans, Object parameterObject)
throws SQLException {
int n = statement.executeUpdate(statementScope, trans, parameterObject);
return n;
}
public Object executeQueryForObject(StatementScope statementScope, Transaction trans, Object parameterObject, Object resultObject)
throws SQLException {
//鑾峰彇褰撳墠鎿嶄綔鐨勫敮涓鐮?
CacheKey cacheKey = getCacheKey(statementScope, parameterObject);
//鍞竴鐮佺殑鍐嶆澶勭悊
cacheKey.update("executeQueryForObject");
//浠庣紦瀛樺鍣ㄤ腑鑾峰彇瀵瑰簲鍞竴鐮佺殑缂撳瓨瀵硅薄
Object object = cacheModel.getObject(cacheKey);
if (object == CacheModel.NULL_OBJECT){
// This was cached, but null
object = null;
}else if (object == null) {
//缂撳瓨瀹瑰櫒涓病鏈夊敮涓鐮佺殑缂撳瓨瀵硅薄锛屽埌鏁版嵁搴撳幓鏌ヨ锛岀劧鍚庢妸鏌ヨ缁撴灉淇濆瓨鍒扮紦瀛樺鍣ㄤ腑浣滀负缂撳瓨瀵硅薄
object = statement.executeQueryForObject(statementScope, trans, parameterObject, resultObject);
cacheModel.putObject(cacheKey, object);
}
return object;
}
public List executeQueryForList(StatementScope statementScope, Transaction trans, Object parameterObject, int skipResults, int maxResults)
throws SQLException {
//鑾峰彇褰撳墠鎿嶄綔鐨勫敮涓鐮?
CacheKey cacheKey = getCacheKey(statementScope, parameterObject);
//鍞竴鐮佺殑鍐嶆澶勭悊
cacheKey.update("executeQueryForList");
cacheKey.update(skipResults);
cacheKey.update(maxResults);
//浠庣紦瀛樺鍣ㄤ腑鑾峰彇瀵瑰簲鍞竴鐮佺殑缂撳瓨瀵硅薄
Object listAsObject = cacheModel.getObject(cacheKey);
List list;
if(listAsObject == CacheModel.NULL_OBJECT){
// The cached object was null
list = null;
}else if (listAsObject == null) {
//缂撳瓨瀹瑰