org.springframework.orm.ibatis
Class SqlMapTemplate

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

public class SqlMapTemplate
extends JdbcAccessor
implements SqlMapOperations

Helper class that simplifies data access via the MappedStatement API of iBATIS SQL Maps, and converts checked SQLExceptions into unchecked DataAccessExceptions, following the org.springframework.dao exception hierarchy. Uses the same SQLExceptionTranslator mechanism as JdbcTemplate.

The main method is execute, taking the name of a mapped statement defined in the iBATIS SqlMap config file and a callback that implements a data access action on the specified statement.

This class provides numerous convenience methods that mirror MappedStatement's executeXXX methods. See the MappedStatement javadocs for details on those methods.

NOTE: The SqlMap/MappedStatement API is the one to use with iBATIS SQL Maps 1.x. The SqlMapClient/SqlMapSession API is only available with SQL Maps 2.

Since:
28.11.2003
Author:
Juergen Hoeller
See Also:
execute(java.lang.String, org.springframework.orm.ibatis.SqlMapCallback), setSqlMap(com.ibatis.db.sqlmap.SqlMap), JdbcAccessor.setDataSource(javax.sql.DataSource), JdbcAccessor.setExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator), MappedStatement

Field Summary
 
Fields inherited from class org.springframework.jdbc.support.JdbcAccessor
logger
 
Constructor Summary
SqlMapTemplate()
          Create a new SqlMapTemplate.
SqlMapTemplate(DataSource dataSource, com.ibatis.db.sqlmap.SqlMap sqlMap)
          Create a new SqlMapTemplate.
 
Method Summary
 void afterPropertiesSet()
          Eagerly initialize the exception translator, creating a default one for the specified DataSource if none set.
 Object execute(String statementName, SqlMapCallback action)
          Execute the given data access action on the given iBATIS mapped statement.
 List executeQueryForList(String statementName, Object parameterObject)
           
 List executeQueryForList(String statementName, Object parameterObject, int skipResults, int maxResults)
           
 Map executeQueryForMap(String statementName, Object parameterObject, String keyProperty)
           
 Map executeQueryForMap(String statementName, Object parameterObject, String keyProperty, String valueProperty)
           
 Object executeQueryForObject(String statementName, Object parameterObject)
           
 Object executeQueryForObject(String statementName, Object parameterObject, Object resultObject)
           
 void executeQueryWithRowHandler(String statementName, Object parameterObject, com.ibatis.db.sqlmap.RowHandler rowHandler)
           
 int executeUpdate(String statementName, Object parameterObject)
           
 List executeWithListResult(String statementName, SqlMapCallback action)
          Execute the given data access action on the given mapped statement, expecting a List result.
 Map executeWithMapResult(String statementName, SqlMapCallback action)
          Execute the given data access action on the given mapped statement, expecting a Map result.
 com.ibatis.db.sqlmap.SqlMap getSqlMap()
          Return the iBATIS Database Layer SqlMap that this template works with.
 void setSqlMap(com.ibatis.db.sqlmap.SqlMap sqlMap)
          Set the iBATIS Database Layer SqlMap that defines the mapped statements.
 
Methods inherited from class org.springframework.jdbc.support.JdbcAccessor
getDataSource, 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

SqlMapTemplate

public SqlMapTemplate()
Create a new SqlMapTemplate.


SqlMapTemplate

public SqlMapTemplate(DataSource dataSource,
                      com.ibatis.db.sqlmap.SqlMap sqlMap)
Create a new SqlMapTemplate.

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

setSqlMap

public void setSqlMap(com.ibatis.db.sqlmap.SqlMap sqlMap)
Set the iBATIS Database Layer SqlMap that defines the mapped statements.


getSqlMap

public com.ibatis.db.sqlmap.SqlMap getSqlMap()
Return the iBATIS Database Layer SqlMap that this template works with.


afterPropertiesSet

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

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

execute

public Object execute(String statementName,
                      SqlMapCallback action)
               throws DataAccessException
Execute the given data access action on the given iBATIS mapped statement.

Parameters:
statementName - name of the statement mapped in the iBATIS SqlMap config file
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

public List executeWithListResult(String statementName,
                                  SqlMapCallback action)
                           throws DataAccessException
Execute the given data access action on the given mapped statement, expecting a List result.

Parameters:
statementName - name of the statement mapped in the iBATIS SqlMap config file
action - callback object that specifies the data access action
Returns:
a List result
Throws:
DataAccessException - in case of SQL Maps errors

executeWithMapResult

public Map executeWithMapResult(String statementName,
                                SqlMapCallback action)
                         throws DataAccessException
Execute the given data access action on the given mapped statement, expecting a Map result.

Parameters:
statementName - name of the statement mapped in the iBATIS SqlMap config file
action - callback object that specifies the data access action
Returns:
a Map result
Throws:
DataAccessException - in case of SQL Maps errors

executeQueryForObject

public Object executeQueryForObject(String statementName,
                                    Object parameterObject)
                             throws DataAccessException
Specified by:
executeQueryForObject in interface SqlMapOperations
Throws:
DataAccessException

executeQueryForObject

public Object executeQueryForObject(String statementName,
                                    Object parameterObject,
                                    Object resultObject)
                             throws DataAccessException
Specified by:
executeQueryForObject in interface SqlMapOperations
Throws:
DataAccessException

executeQueryForList

public List executeQueryForList(String statementName,
                                Object parameterObject)
                         throws DataAccessException
Specified by:
executeQueryForList in interface SqlMapOperations
Throws:
DataAccessException

executeQueryForList

public List executeQueryForList(String statementName,
                                Object parameterObject,
                                int skipResults,
                                int maxResults)
                         throws DataAccessException
Specified by:
executeQueryForList in interface SqlMapOperations
Throws:
DataAccessException

executeQueryForMap

public Map executeQueryForMap(String statementName,
                              Object parameterObject,
                              String keyProperty)
                       throws DataAccessException
Specified by:
executeQueryForMap in interface SqlMapOperations
Throws:
DataAccessException

executeQueryForMap

public Map executeQueryForMap(String statementName,
                              Object parameterObject,
                              String keyProperty,
                              String valueProperty)
                       throws DataAccessException
Specified by:
executeQueryForMap in interface SqlMapOperations
Throws:
DataAccessException

executeQueryWithRowHandler

public void executeQueryWithRowHandler(String statementName,
                                       Object parameterObject,
                                       com.ibatis.db.sqlmap.RowHandler rowHandler)
                                throws DataAccessException
Specified by:
executeQueryWithRowHandler in interface SqlMapOperations
Throws:
DataAccessException

executeUpdate

public int executeUpdate(String statementName,
                         Object parameterObject)
                  throws DataAccessException
Specified by:
executeUpdate in interface SqlMapOperations
Throws:
DataAccessException


Copyright (c) 2002-2007 The Spring Framework Project.