org.springframework.orm.ibatis
Class SqlMapClientTemplate

java.lang.Object
  extended by org.springframework.jdbc.support.JdbcAccessor
      extended by org.springframework.orm.ibatis.SqlMapClientTemplate
All Implemented Interfaces:
InitializingBean, SqlMapClientOperations

public class SqlMapClientTemplate
extends JdbcAccessor
implements SqlMapClientOperations

Helper class that simplifies data access via the iBATIS SqlMapClient API, converting checked SQLExceptions into unchecked DataAccessExceptions, following the org.springframework.dao exception hierarchy. Uses the same SQLExceptionTranslator mechanism as JdbcTemplate.

The main method of this class executes a callback that implements a data access action. Furthermore, this class provides numerous convenience methods that mirror SqlMapExecutor's execution methods.

It is generally recommended to use the convenience methods on this template for plain query/insert/update/delete operations. However, for more complex operations like batch updates, a custom SqlMapClientCallback must be implemented, usually as anonymous inner class. For example:

 getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
         public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                 executor.startBatch();
                 executor.update("insertSomething", "myParamValue");
                 executor.update("insertSomethingElse", "myOtherParamValue");
                 executor.executeBatch();
                 return null;
         }
 });
The template needs a SqlMapClient to work on, passed in via the "sqlMapClient" property. A Spring context typically uses a SqlMapClientFactoryBean to build the SqlMapClient. The template an additionally be configured with a DataSource for fetching Connections, although this is not necessary if a DataSource is specified for the SqlMapClient itself (typically through SqlMapClientFactoryBean's "dataSource" property).

Since:
24.02.2004
Author:
Juergen Hoeller
See Also:
execute(org.springframework.orm.ibatis.SqlMapClientCallback), setSqlMapClient(com.ibatis.sqlmap.client.SqlMapClient), JdbcAccessor.setDataSource(javax.sql.DataSource), JdbcAccessor.setExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator), SqlMapClientFactoryBean.setDataSource(javax.sql.DataSource), SqlMapTransactionManager.getDataSource(), SqlMapExecutor

Field Summary
 
Fields inherited from class org.springframework.jdbc.support.JdbcAccessor
logger
 
Constructor Summary
SqlMapClientTemplate()
          Create a new SqlMapClientTemplate.
SqlMapClientTemplate(javax.sql.DataSource dataSource, com.ibatis.sqlmap.client.SqlMapClient sqlMapClient)
          Create a new SqlMapTemplate.
SqlMapClientTemplate(com.ibatis.sqlmap.client.SqlMapClient sqlMapClient)
          Create a new SqlMapTemplate.
 
Method Summary
 void afterPropertiesSet()
          Eagerly initialize the exception translator, if demanded, creating a default one for the specified DataSource if none set.
 int delete(java.lang.String statementName)
           
 int delete(java.lang.String statementName, java.lang.Object parameterObject)
           
 void delete(java.lang.String statementName, java.lang.Object parameterObject, int requiredRowsAffected)
          Convenience method provided by Spring: execute a delete operation with an automatic check that the delete affected the given required number of rows.
<T> T
execute(SqlMapClientCallback<T> action)
          Execute the given data access action on a SqlMapExecutor.
 java.util.List executeWithListResult(SqlMapClientCallback<java.util.List> action)
          Deprecated. as of Spring 3.0 - not really needed anymore with generic execute(org.springframework.orm.ibatis.SqlMapClientCallback) method
 java.util.Map executeWithMapResult(SqlMapClientCallback<java.util.Map> action)
          Deprecated. as of Spring 3.0 - not really needed anymore with generic execute(org.springframework.orm.ibatis.SqlMapClientCallback) method
 javax.sql.DataSource getDataSource()
          If no DataSource specified, use SqlMapClient's DataSource.
 com.ibatis.sqlmap.client.SqlMapClient getSqlMapClient()
          Return the iBATIS Database Layer SqlMapClient that this template works with.
 java.lang.Object insert(java.lang.String statementName)
           
 java.lang.Object insert(java.lang.String statementName, java.lang.Object parameterObject)
           
 java.util.List queryForList(java.lang.String statementName)
           
 java.util.List queryForList(java.lang.String statementName, int skipResults, int maxResults)
           
 java.util.List queryForList(java.lang.String statementName, java.lang.Object parameterObject)
           
 java.util.List queryForList(java.lang.String statementName, java.lang.Object parameterObject, int skipResults, int maxResults)
           
 java.util.Map queryForMap(java.lang.String statementName, java.lang.Object parameterObject, java.lang.String keyProperty)
           
 java.util.Map queryForMap(java.lang.String statementName, java.lang.Object parameterObject, java.lang.String keyProperty, java.lang.String valueProperty)
           
 java.lang.Object queryForObject(java.lang.String statementName)
           
 java.lang.Object queryForObject(java.lang.String statementName, java.lang.Object parameterObject)
           
 java.lang.Object queryForObject(java.lang.String statementName, java.lang.Object parameterObject, java.lang.Object resultObject)
           
 void queryWithRowHandler(java.lang.String statementName, java.lang.Object parameterObject, com.ibatis.sqlmap.client.event.RowHandler rowHandler)
           
 void queryWithRowHandler(java.lang.String statementName, com.ibatis.sqlmap.client.event.RowHandler rowHandler)
           
 void setSqlMapClient(com.ibatis.sqlmap.client.SqlMapClient sqlMapClient)
          Set the iBATIS Database Layer SqlMapClient that defines the mapped statements.
 int update(java.lang.String statementName)
           
 int update(java.lang.String statementName, java.lang.Object parameterObject)
           
 void update(java.lang.String statementName, java.lang.Object parameterObject, int requiredRowsAffected)
          Convenience method provided by Spring: execute an update operation with an automatic check that the update affected the given required number of rows.
 
Methods inherited from class org.springframework.jdbc.support.JdbcAccessor
getExceptionTranslator, isLazyInit, setDatabaseProductName, setDataSource, setExceptionTranslator, setLazyInit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlMapClientTemplate

public SqlMapClientTemplate()
Create a new SqlMapClientTemplate.


SqlMapClientTemplate

public SqlMapClientTemplate(com.ibatis.sqlmap.client.SqlMapClient sqlMapClient)
Create a new SqlMapTemplate.

Parameters:
sqlMapClient - iBATIS SqlMapClient that defines the mapped statements

SqlMapClientTemplate

public SqlMapClientTemplate(javax.sql.DataSource dataSource,
                            com.ibatis.sqlmap.client.SqlMapClient sqlMapClient)
Create a new SqlMapTemplate.

Parameters:
dataSource - JDBC DataSource to obtain connections from
sqlMapClient - iBATIS SqlMapClient that defines the mapped statements
Method Detail

setSqlMapClient

public void setSqlMapClient(com.ibatis.sqlmap.client.SqlMapClient sqlMapClient)
Set the iBATIS Database Layer SqlMapClient that defines the mapped statements.


getSqlMapClient

public com.ibatis.sqlmap.client.SqlMapClient getSqlMapClient()
Return the iBATIS Database Layer SqlMapClient that this template works with.


getDataSource

public javax.sql.DataSource getDataSource()
If no DataSource specified, use SqlMapClient's DataSource.

Overrides:
getDataSource in class JdbcAccessor
See Also:
SqlMapTransactionManager.getDataSource()

afterPropertiesSet

public void afterPropertiesSet()
Description copied from class: JdbcAccessor
Eagerly initialize the exception translator, if demanded, creating a default one for the specified DataSource if none set.

Specified by:
afterPropertiesSet in interface InitializingBean
Overrides:
afterPropertiesSet in class JdbcAccessor

execute

public <T> T execute(SqlMapClientCallback<T> action)
          throws DataAccessException
Execute the given data access action on a SqlMapExecutor.

Parameters:
action - callback object that specifies the data access action
Returns:
a result object returned by the action, or null
Throws:
DataAccessException - in case of SQL Maps errors

executeWithListResult

@Deprecated
public java.util.List executeWithListResult(SqlMapClientCallback<java.util.List> action)
                                     throws DataAccessException
Deprecated. as of Spring 3.0 - not really needed anymore with generic execute(org.springframework.orm.ibatis.SqlMapClientCallback) method

Execute the given data access action on a SqlMapExecutor, expecting a List result.

Parameters:
action - callback object that specifies the data access action
Returns:
the List result
Throws:
DataAccessException - in case of SQL Maps errors

executeWithMapResult

@Deprecated
public java.util.Map executeWithMapResult(SqlMapClientCallback<java.util.Map> action)
                                   throws DataAccessException
Deprecated. as of Spring 3.0 - not really needed anymore with generic execute(org.springframework.orm.ibatis.SqlMapClientCallback) method

Execute the given data access action on a SqlMapExecutor, expecting a Map result.

Parameters:
action - callback object that specifies the data access action
Returns:
the Map result
Throws:
DataAccessException - in case of SQL Maps errors

queryForObject

public java.lang.Object queryForObject(java.lang.String statementName)
                                throws DataAccessException
Specified by:
queryForObject in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.queryForObject(String)

queryForObject

public java.lang.Object queryForObject(java.lang.String statementName,
                                       java.lang.Object parameterObject)
                                throws DataAccessException
Specified by:
queryForObject in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.queryForObject(String, Object)

queryForObject

public java.lang.Object queryForObject(java.lang.String statementName,
                                       java.lang.Object parameterObject,
                                       java.lang.Object resultObject)
                                throws DataAccessException
Specified by:
queryForObject in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.queryForObject(String, Object, Object)

queryForList

public java.util.List queryForList(java.lang.String statementName)
                            throws DataAccessException
Specified by:
queryForList in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.queryForList(String)

queryForList

public java.util.List queryForList(java.lang.String statementName,
                                   java.lang.Object parameterObject)
                            throws DataAccessException
Specified by:
queryForList in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.queryForList(String, Object)

queryForList

public java.util.List queryForList(java.lang.String statementName,
                                   int skipResults,
                                   int maxResults)
                            throws DataAccessException
Specified by:
queryForList in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.queryForList(String, int, int)

queryForList

public java.util.List queryForList(java.lang.String statementName,
                                   java.lang.Object parameterObject,
                                   int skipResults,
                                   int maxResults)
                            throws DataAccessException
Specified by:
queryForList in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.queryForList(String, Object, int, int)

queryWithRowHandler

public void queryWithRowHandler(java.lang.String statementName,
                                com.ibatis.sqlmap.client.event.RowHandler rowHandler)
                         throws DataAccessException
Specified by:
queryWithRowHandler in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.queryWithRowHandler(String, RowHandler)

queryWithRowHandler

public void queryWithRowHandler(java.lang.String statementName,
                                java.lang.Object parameterObject,
                                com.ibatis.sqlmap.client.event.RowHandler rowHandler)
                         throws DataAccessException
Specified by:
queryWithRowHandler in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.queryWithRowHandler(String, Object, RowHandler)

queryForMap

public java.util.Map queryForMap(java.lang.String statementName,
                                 java.lang.Object parameterObject,
                                 java.lang.String keyProperty)
                          throws DataAccessException
Specified by:
queryForMap in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.queryForMap(String, Object, String)

queryForMap

public java.util.Map queryForMap(java.lang.String statementName,
                                 java.lang.Object parameterObject,
                                 java.lang.String keyProperty,
                                 java.lang.String valueProperty)
                          throws DataAccessException
Specified by:
queryForMap in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.queryForMap(String, Object, String, String)

insert

public java.lang.Object insert(java.lang.String statementName)
                        throws DataAccessException
Specified by:
insert in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.insert(String)

insert

public java.lang.Object insert(java.lang.String statementName,
                               java.lang.Object parameterObject)
                        throws DataAccessException
Specified by:
insert in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.insert(String, Object)

update

public int update(java.lang.String statementName)
           throws DataAccessException
Specified by:
update in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.update(String)

update

public int update(java.lang.String statementName,
                  java.lang.Object parameterObject)
           throws DataAccessException
Specified by:
update in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.update(String, Object)

update

public void update(java.lang.String statementName,
                   java.lang.Object parameterObject,
                   int requiredRowsAffected)
            throws DataAccessException
Description copied from interface: SqlMapClientOperations
Convenience method provided by Spring: execute an update operation with an automatic check that the update affected the given required number of rows.

Specified by:
update in interface SqlMapClientOperations
Parameters:
statementName - the name of the mapped statement
parameterObject - the parameter object
requiredRowsAffected - the number of rows that the update is required to affect
Throws:
DataAccessException - in case of errors

delete

public int delete(java.lang.String statementName)
           throws DataAccessException
Specified by:
delete in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.delete(String)

delete

public int delete(java.lang.String statementName,
                  java.lang.Object parameterObject)
           throws DataAccessException
Specified by:
delete in interface SqlMapClientOperations
Throws:
DataAccessException - in case of errors
See Also:
SqlMapExecutor.delete(String, Object)

delete

public void delete(java.lang.String statementName,
                   java.lang.Object parameterObject,
                   int requiredRowsAffected)
            throws DataAccessException
Description copied from interface: SqlMapClientOperations
Convenience method provided by Spring: execute a delete operation with an automatic check that the delete affected the given required number of rows.

Specified by:
delete in interface SqlMapClientOperations
Parameters:
statementName - the name of the mapped statement
parameterObject - the parameter object
requiredRowsAffected - the number of rows that the delete is required to affect
Throws:
DataAccessException - in case of errors