SqlMapClient中包含着session的管理.
SqlMapClientTemplate用于session的封装,以及异常的捕捉.
所以按照以上的推断来说.应该尽量使用SqlMapClientTemplate.
保证session以及Exception的正常以及统一.
SqlMapClientTemplate源码:
/* */ package org.springframework.orm.ibatis; /* */ /* */ import com.ibatis.common.util.PaginatedList; /* */ import com.ibatis.sqlmap.client.SqlMapClient; /* */ import com.ibatis.sqlmap.client.SqlMapExecutor; /* */ import com.ibatis.sqlmap.client.SqlMapSession; /* */ import com.ibatis.sqlmap.client.event.RowHandler; /* */ import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient; /* */ import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate; /* */ import java.sql.Connection; /* */ import java.sql.SQLException; /* */ import java.util.List; /* */ import java.util.Map; /* */ import javax.sql.DataSource; /* */ import org.springframework.dao.DataAccessException; /* */ import org.springframework.dao.InvalidDataAccessApiUsageException; /* */ import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException; /* */ import org.springframework.jdbc.datasource.DataSourceUtils; /* */ import org.springframework.jdbc.support.JdbcAccessor; /* */ import org.springframework.jdbc.support.SQLExceptionTranslator; /* */ import org.springframework.util.Assert; /* */ /* */ public class SqlMapClientTemplate extends JdbcAccessor /* */ implements SqlMapClientOperations /* */ { /* */ private SqlMapClient sqlMapClient; /* */ /* */ public SqlMapClientTemplate() /* */ { /* */ } /* */ /* */ public SqlMapClientTemplate(SqlMapClient sqlMapClient) /* */ { /* 101 */ setSqlMapClient(sqlMapClient); /* 102 */ afterPropertiesSet(); /* */ } /* */ /* */ public SqlMapClientTemplate(DataSource dataSource, SqlMapClient sqlMapClient) /* */ { /* 111 */ setDataSource(dataSource); /* 112 */ setSqlMapClient(sqlMapClient); /* 113 */ afterPropertiesSet(); /* */ } /* */ /* */ public void setSqlMapClient(SqlMapClient sqlMapClient) /* */ { /* 120 */ this.sqlMapClient = sqlMapClient; /* */ } /* */ /* */ public SqlMapClient getSqlMapClient() /* */ { /* 127 */ return this.sqlMapClient; /* */ } /* */ /* */ public DataSource getDataSource() /* */ { /* 135 */ DataSource ds = super.getDataSource(); /* 136 */ return ds != null ? ds : this.sqlMapClient.getDataSource(); /* */ } /* */ /* */ public void afterPropertiesSet() { /* 140 */ if (this.sqlMapClient == null) { /* 141 */ throw new IllegalArgumentException("sqlMapClient is required"); /* */ } /* 143 */ super.afterPropertiesSet(); /* */ } /* */ /* */ public Object execute(SqlMapClientCallback action) /* */ throws DataAccessException /* */ { /* 154 */ Assert.notNull(this.sqlMapClient, "No SqlMapClient specified"); /* */ /* 163 */ SqlMapSession session = this.sqlMapClient.openSession(); /* */ try { /* 165 */ Connection con = DataSourceUtils.getConnection(getDataSource()); /* */ try { /* 167 */ session.setUserConnection(con); /* 168 */ Object localObject1 = action.doInSqlMapClient(session); /* */ /* 174 */ DataSourceUtils.releaseConnection(con, getDataSource()); /* */ /* 178 */ session.close(); /* 179 */ return localObject1; /* */ } /* */ catch (SQLException ex) /* */ { /* 171 */ throw getExceptionTranslator().translate("SqlMapClient operation", null, ex); /* */ } /* */ finally { /* 174 */ DataSourceUtils.releaseConnection(con, getDataSource()); /* */ } /* */ } /* */ finally { /* 178 */ session.close(); /* 179 */ }throw localObject3; /* */ } /* */ /* */ public List executeWithListResult(SqlMapClientCallback action) /* */ throws DataAccessException /* */ { /* 190 */ return (List)execute(action); /* */ } /* */ /* */ public Map executeWithMapResult(SqlMapClientCallback action) /* */ throws DataAccessException /* */ { /* 201 */ return (Map)execute(action); /* */ } /* */ /* */ public Object queryForObject(String statementName, Object parameterObject) /* */ throws DataAccessException /* */ { /* 208 */ return execute(new SqlMapClientCallback(statementName, parameterObject) { private final String val$statementName; /* */ private final Object val$parameterObject; /* */ /* 210 */ public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return executor.queryForObject(this.val$statementName, this.val$parameterObject); /* */ } /* */ }); /* */ } /* */ /* */ public Object queryForObject(String statementName, Object parameterObject, Object resultObject) /* */ throws DataAccessException /* */ { /* 219 */ return execute(new SqlMapClientCallback(statementName, parameterObject, resultObject) { private final String val$statementName; /* */ private final Object val$parameterObject; /* */ private final Object val$resultObject; /* */ /* 221 */ public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return executor.queryForObject(this.val$statementName, this.val$parameterObject, this.val$resultObject); } /* */ }); /* */ } /* */ /* */ public List queryForList(String statementName, Object parameterObject) /* */ throws DataAccessException /* */ { /* 229 */ return executeWithListResult(new SqlMapClientCallback(statementName, parameterObject) { private final String val$statementName; /* */ private final Object val$parameterObject; /* */ /* 231 */ public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return executor.queryForList(this.val$statementName, this.val$parameterObject); /* */ } /* */ }); /* */ } /* */ /* */ public List queryForList(String statementName, Object parameterObject, int skipResults, int maxResults) /* */ throws DataAccessException /* */ { /* 240 */ return executeWithListResult(new SqlMapClientCallback(statementName, parameterObject, skipResults, maxResults) { private final String val$statementName; /* */ private final Object val$parameterObject; /* */ private final int val$skipResults; /* */ private final int val$maxResults; /* */ /* 242 */ public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return executor.queryForList(this.val$statementName, this.val$parameterObject, this.val$skipResults, this.val$maxResults); /* */ } /* */ }); /* */ } /* */ /* */ public void queryWithRowHandler(String statementName, Object parameterObject, RowHandler rowHandler) /* */ throws DataAccessException /* */ { /* 251 */ execute(new SqlMapClientCallback(statementName, parameterObject, rowHandler) { private final String val$statementName; /* */ private final Object val$parameterObject; /* */ private final RowHandler val$rowHandler; /* */ /* 253 */ public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { executor.queryWithRowHandler(this.val$statementName, this.val$parameterObject, this.val$rowHandler); /* 254 */ return null; /* */ } /* */ }); /* */ } /* */ /* */ public PaginatedList queryForPaginatedList(String statementName, Object parameterObject, int pageSize) /* */ throws DataAccessException /* */ { /* 264 */ if (((this.sqlMapClient instanceof ExtendedSqlMapClient)) && (((ExtendedSqlMapClient)this.sqlMapClient).getDelegate().getTxManager() == null)) /* */ { /* 266 */ throw new InvalidDataAccessApiUsageException("SqlMapClient needs to have DataSource to allow for lazy loading - specify SqlMapClientFactoryBean‘s ‘dataSource‘ property"); /* */ } /* */ /* 271 */ return (PaginatedList)execute(new SqlMapClientCallback(statementName, parameterObject, pageSize) { private final String val$statementName; /* */ private final Object val$parameterObject; /* */ private final int val$pageSize; /* */ /* 273 */ public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return executor.queryForPaginatedList(this.val$statementName, this.val$parameterObject, this.val$pageSize); /* */ } /* */ }); /* */ } /* */ /* */ public Map queryForMap(String statementName, Object parameterObject, String keyProperty) /* */ throws DataAccessException /* */ { /* 282 */ return executeWithMapResult(new SqlMapClientCallback(statementName, parameterObject, keyProperty) { private final String val$statementName; /* */ private final Object val$parameterObject; /* */ private final String val$keyProperty; /* */ /* 284 */ public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return executor.queryForMap(this.val$statementName, this.val$parameterObject, this.val$keyProperty); /* */ } /* */ }); /* */ } /* */ /* */ public Map queryForMap(String statementName, Object parameterObject, String keyProperty, String valueProperty) /* */ throws DataAccessException /* */ { /* 293 */ return executeWithMapResult(new SqlMapClientCallback(statementName, parameterObject, keyProperty, valueProperty) { private final String val$statementName; /* */ private final Object val$parameterObject; /* */ private final String val$keyProperty; /* */ private final String val$valueProperty; /* */ /* 295 */ public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return executor.queryForMap(this.val$statementName, this.val$parameterObject, this.val$keyProperty, this.val$valueProperty); } /* */ }); /* */ } /* */ /* */ public Object insert(String statementName, Object parameterObject) /* */ throws DataAccessException /* */ { /* 303 */ return execute(new SqlMapClientCallback(statementName, parameterObject) { private final String val$statementName; /* */ private final Object val$parameterObject; /* */ /* 305 */ public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return executor.insert(this.val$statementName, this.val$parameterObject); } /* */ }); /* */ } /* */ /* */ public int update(String statementName, Object parameterObject) /* */ throws DataAccessException /* */ { /* 313 */ Integer result = (Integer)execute(new SqlMapClientCallback(statementName, parameterObject) { private final String val$statementName; /* */ private final Object val$parameterObject; /* */ /* 315 */ public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return new Integer(executor.update(this.val$statementName, this.val$parameterObject)); /* */ } /* */ }); /* 318 */ return result.intValue(); /* */ } /* */ /* */ public int delete(String statementName, Object parameterObject) /* */ throws DataAccessException /* */ { /* 324 */ Integer result = (Integer)execute(new SqlMapClientCallback(statementName, parameterObject) { private final String val$statementName; /* */ private final Object val$parameterObject; /* */ /* 326 */ public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { return new Integer(executor.delete(this.val$statementName, this.val$parameterObject)); /* */ } /* */ }); /* 329 */ return result.intValue(); /* */ } /* */ /* */ public void update(String statementName, Object parameterObject, int requiredRowsAffected) /* */ throws DataAccessException /* */ { /* 335 */ int actualRowsAffected = update(statementName, parameterObject); /* 336 */ if (actualRowsAffected != requiredRowsAffected) /* 337 */ throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(statementName, requiredRowsAffected, actualRowsAffected); /* */ } /* */ /* */ public void delete(String statementName, Object parameterObject, int requiredRowsAffected) /* */ throws DataAccessException /* */ { /* 345 */ int actualRowsAffected = delete(statementName, parameterObject); /* 346 */ if (actualRowsAffected != requiredRowsAffected) /* 347 */ throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(statementName, requiredRowsAffected, actualRowsAffected); /* */ } /* */ } /* Location: C:\Users\ex_zhangkenan\Desktop\spring.jar * Qualified Name: org.springframework.orm.ibatis.SqlMapClientTemplate * JD-Core Version: 0.6.0 */
SqlMapClient源码:
package com.ibatis.sqlmap.client; import java.sql.Connection; public abstract interface SqlMapClient extends SqlMapExecutor, SqlMapTransactionManager { public abstract SqlMapSession openSession(); public abstract SqlMapSession openSession(Connection paramConnection); /** @deprecated */ public abstract SqlMapSession getSession(); public abstract void flushDataCache(); public abstract void flushDataCache(String paramString); } /* Location: C:\Users\ex_zhangkenan\Desktop\ibatis-sqlmap-2.jar * Qualified Name: com.ibatis.sqlmap.client.SqlMapClient * JD-Core Version: 0.6.0 */