📄 sqlmapexecutordelegate.java
字号:
}
return generatedKey;
}
/**
* Execute an update statement
*
* @param session - the session scope
* @param id - the statement ID
* @param param - the parameter object
* @return - the number of rows updated
* @throws SQLException - if the update fails
*/
public int update(SessionScope session, String id, Object param) throws SQLException {
int rows = 0;
MappedStatement ms = getMappedStatement(id);
Transaction trans = getTransaction(session);
boolean autoStart = trans == null;
try {
trans = autoStartTransaction(session, autoStart, trans);
RequestScope request = popRequest(session, ms);
try {
rows = ms.executeUpdate(request, trans, param);
} finally {
pushRequest(request);
}
autoCommitTransaction(session, autoStart);
} finally {
autoEndTransaction(session, autoStart);
}
return rows;
}
/**
* Execute a delete statement
*
* @param session - the session scope
* @param id - the statement ID
* @param param - the parameter object
* @return - the number of rows deleted
* @throws SQLException - if the delete fails
*/
public int delete(SessionScope session, String id, Object param) throws SQLException {
return update(session, id, param);
}
/**
* Execute a select for a single object
*
* @param session - the session scope
* @param id - the statement ID
* @param paramObject - the parameter object
* @return - the result of the query
* @throws SQLException - if the query fails
*/
public Object queryForObject(SessionScope session, String id, Object paramObject) throws SQLException {
return queryForObject(session, id, paramObject, null);
}
/**
* Execute a select for a single object
*
* @param session - the session scope
* @param id - the statement ID
* @param paramObject - the parameter object
* @param resultObject - the result object (if not supplied or null, a new object will be created)
* @return - the result of the query
* @throws SQLException - if the query fails
*/
public Object queryForObject(SessionScope session, String id, Object paramObject, Object resultObject) throws SQLException {
Object object = null;
MappedStatement ms = getMappedStatement(id);
Transaction trans = getTransaction(session);
boolean autoStart = trans == null;
try {
trans = autoStartTransaction(session, autoStart, trans);
RequestScope request = popRequest(session, ms);
try {
object = ms.executeQueryForObject(request, trans, paramObject, resultObject);
} finally {
pushRequest(request);
}
autoCommitTransaction(session, autoStart);
} finally {
autoEndTransaction(session, autoStart);
}
return object;
}
/**
* Execute a query for a list
*
* @param session - the session scope
* @param id - the statement ID
* @param paramObject - the parameter object
* @return - the data list
* @throws SQLException - if the query fails
*/
public List queryForList(SessionScope session, String id, Object paramObject) throws SQLException {
return queryForList(session, id, paramObject, SqlExecutor.NO_SKIPPED_RESULTS, SqlExecutor.NO_MAXIMUM_RESULTS);
}
/**
* Execute a query for a list
*
* @param session - the session scope
* @param id - the statement ID
* @param paramObject - the parameter object
* @param skip - the number of rows to skip
* @param max - the maximum number of rows to return
* @return - the data list
* @throws SQLException - if the query fails
*/
public List queryForList(SessionScope session, String id, Object paramObject, int skip, int max) throws SQLException {
List list = null;
MappedStatement ms = getMappedStatement(id);
Transaction trans = getTransaction(session);
boolean autoStart = trans == null;
try {
trans = autoStartTransaction(session, autoStart, trans);
RequestScope request = popRequest(session, ms);
try {
list = ms.executeQueryForList(request, trans, paramObject, skip, max);
} finally {
pushRequest(request);
}
autoCommitTransaction(session, autoStart);
} finally {
autoEndTransaction(session, autoStart);
}
return list;
}
/**
* Execute a query with a row handler.
* The row handler is called once per row in the query results.
*
* @param session - the session scope
* @param id - the statement ID
* @param paramObject - the parameter object
* @param rowHandler - the row handler
* @throws SQLException - if the query fails
*/
public void queryWithRowHandler(SessionScope session, String id, Object paramObject, RowHandler rowHandler) throws SQLException {
MappedStatement ms = getMappedStatement(id);
Transaction trans = getTransaction(session);
boolean autoStart = trans == null;
try {
trans = autoStartTransaction(session, autoStart, trans);
RequestScope request = popRequest(session, ms);
try {
ms.executeQueryWithRowHandler(request, trans, paramObject, rowHandler);
} finally {
pushRequest(request);
}
autoCommitTransaction(session, autoStart);
} finally {
autoEndTransaction(session, autoStart);
}
}
/**
* Execute a query and return a paginated list
*
* @param session - the session scope
* @param id - the statement ID
* @param paramObject - the parameter object
* @param pageSize - the page size
* @return - the data list
* @throws SQLException - if the query fails
* @deprecated All paginated list features have been deprecated
*/
public PaginatedList queryForPaginatedList(SessionScope session, String id, Object paramObject, int pageSize) throws SQLException {
return new PaginatedDataList(session.getSqlMapExecutor(), id, paramObject, pageSize);
}
/**
* Execute a query for a map.
* The map has the table key as the key, and the results as the map data
*
* @param session - the session scope
* @param id - the statement ID
* @param paramObject - the parameter object
* @param keyProp - the key property (from the results for the map)
* @return - the Map
* @throws SQLException - if the query fails
*/
public Map queryForMap(SessionScope session, String id, Object paramObject, String keyProp) throws SQLException {
return queryForMap(session, id, paramObject, keyProp, null);
}
/**
* Execute a query for a map.
* The map has the table key as the key, and a property from the results as the map data
*
* @param session - the session scope
* @param id - the statement ID
* @param paramObject - the parameter object
* @param keyProp - the property for the map key
* @param valueProp - the property for the map data
* @return - the Map
* @throws SQLException - if the query fails
*/
public Map queryForMap(SessionScope session, String id, Object paramObject, String keyProp, String valueProp) throws SQLException {
Map map = new HashMap();
List list = queryForList(session, id, paramObject);
for (int i = 0, n = list.size(); i < n; i++) {
Object object = list.get(i);
Object key = PROBE.getObject(object, keyProp);
Object value = null;
if (valueProp == null) {
value = object;
} else {
value = PROBE.getObject(object, valueProp);
}
map.put(key, value);
}
return map;
}
// -- Transaction Control Methods
/**
* Start a transaction on the session
*
* @param session - the session
* @throws SQLException - if the transaction could not be started
*/
public void startTransaction(SessionScope session) throws SQLException {
try {
txManager.begin(session);
} catch (TransactionException e) {
throw new NestedSQLException("Could not start transaction. Cause: " + e, e);
}
}
/**
* Start a transaction on the session with the specified isolation level.
*
* @param session - the session
* @throws SQLException - if the transaction could not be started
*/
public void startTransaction(SessionScope session, int transactionIsolation) throws SQLException {
try {
txManager.begin(session, transactionIsolation);
} catch (TransactionException e) {
throw new NestedSQLException("Could not start transaction. Cause: " + e, e);
}
}
/**
* Commit the transaction on a session
*
* @param session - the session
* @throws SQLException - if the transaction could not be committed
*/
public void commitTransaction(SessionScope session) throws SQLException {
try {
// Auto batch execution
if (session.isInBatch()) {
executeBatch(session);
}
sqlExecutor.cleanup(session);
txManager.commit(session);
} catch (TransactionException e) {
throw new NestedSQLException("Could not commit transaction. Cause: " + e, e);
}
}
/**
* End the transaction on a session
*
* @param session - the session
* @throws SQLException - if the transaction could not be ended
*/
public void endTransaction(SessionScope session) throws SQLException {
try {
try {
sqlExecutor.cleanup(session);
} finally {
txManager.end(session);
}
} catch (TransactionException e) {
throw new NestedSQLException("Error while ending transaction. Cause: " + e, e);
}
}
/**
* Start a batch for a session
*
* @param session - the session
*/
public void startBatch(SessionScope session) {
session.setInBatch(true);
}
/**
* Execute a batch for a session
*
* @param session - the session
* @return - the number of rows impacted by the batch
* @throws SQLException - if the batch fails
*/
public int executeBatch(SessionScope session) throws SQLException {
session.setInBatch(false);
return sqlExecutor.executeBatch(session);
}
/**
* Execute a batch for a session
*
* @param session - the session
* @return - a List of BatchResult objects (may be null if no batch
* has been initiated). There will be one BatchResult object in the
* list for each sub-batch executed
* @throws SQLException if a database access error occurs, or the drive
* does not support batch statements
* @throws BatchException if the driver throws BatchUpdateException
*/
public List executeBatchDetailed(SessionScope session) throws SQLException, BatchException {
session.setInBatch(false);
return sqlExecutor.executeBatchDetailed(session);
}
/**
* Use a user-provided transaction for a session
*
* @param session - the session scope
* @param userConnection - the user supplied connection
*/
public void setUserProvidedTransaction(SessionScope session, Connection userConnection) {
if (session.getTransactionState() == TransactionState.STATE_USER_PROVIDED) {
session.recallTransactionState();
}
if (userConnection != null) {
Connection conn = userConnection;
session.saveTransactionState();
session.setTransaction(new UserProvidedTransaction(conn));
session.setTransactionState(TransactionState.STATE_USER_PROVIDED);
} else {
session.setTransaction(null);
session.closePreparedStatements();
session.reset(); // used to be pushSession, which is probably incorrect.
}
}
/**
* Get the DataSource for the session
*
* @return - the DataSource
*/
public DataSource getDataSource() {
DataSource ds = null;
if (txManager != null) {
ds = txManager.getDataSource();
}
return ds;
}
/**
* Getter for the SqlExecutor
*
* @return the SqlExecutor
*/
public SqlExecutor getSqlExecutor() {
return sqlExecutor;
}
/**
* Get a transaction for the session
*
* @param session - the session
* @return - the transaction
*/
public Transaction getTransaction(SessionScope session) {
return session.getTransaction();
}
// -- Protected Methods
protected void autoEndTransaction(SessionScope session, boolean autoStart) throws SQLException {
if (autoStart) {
session.getSqlMapTxMgr().endTransaction();
}
}
protected void autoCommitTransaction(SessionScope session, boolean autoStart) throws SQLException {
if (autoStart) {
session.getSqlMapTxMgr().commitTransaction();
}
}
protected Transaction autoStartTransaction(SessionScope session, boolean autoStart, Transaction trans) throws SQLException {
Transaction transaction = trans;
if (autoStart) {
session.getSqlMapTxMgr().startTransaction();
transaction = getTransaction(session);
}
return transaction;
}
public boolean equals(Object obj) {
return this == obj;
}
public int hashCode() {
CacheKey key = new CacheKey();
if (txManager != null) {
key.update(txManager);
if (txManager.getDataSource() != null) {
key.update(txManager.getDataSource());
}
}
key.update(System.identityHashCode(this));
return key.hashCode();
}
protected RequestScope popRequest(SessionScope session, MappedStatement mappedStatement) {
RequestScope request = (RequestScope) requestPool.pop();
session.incrementRequestStackDepth();
request.setSession(session);
mappedStatement.initRequest(request);
return request;
}
protected void pushRequest(RequestScope request) {
request.getSession().decrementRequestStackDepth();
request.reset();
requestPool.push(request);
}
protected SessionScope popSession() {
return (SessionScope) sessionPool.pop();
}
protected void pushSession(SessionScope session) {
session.reset();
sessionPool.push(session);
}
public ResultObjectFactory getResultObjectFactory() {
return resultObjectFactory;
}
public void setResultObjectFactory(ResultObjectFactory resultObjectFactory) {
this.resultObjectFactory = resultObjectFactory;
}
public boolean isStatementCacheEnabled() {
return statementCacheEnabled;
}
public void setStatementCacheEnabled(boolean statementCacheEnabled) {
this.statementCacheEnabled = statementCacheEnabled;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -