org.mybatis.spring
Class SqlSessionTemplate

java.lang.Object
  extended by org.springframework.jdbc.support.JdbcAccessor
      extended by org.mybatis.spring.SqlSessionTemplate
All Implemented Interfaces:
SqlSessionOperations, org.springframework.beans.factory.InitializingBean

public class SqlSessionTemplate
extends org.springframework.jdbc.support.JdbcAccessor
implements SqlSessionOperations

Helper class that simplifies data access via the MyBatis SqlSession 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 SqlSession'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 SqlSessionCallback must be implemented, usually as anonymous inner class. For example:

 getSqlSessionTemplate().execute(new SqlSessionCallback<Object>() {
     public Object doInSqlSession(SqlSession sqlSession) throws SQLException {
         sqlSession.getMapper(MyMapper.class).update(parameterObject);
         sqlSession.update("MyMapper.update", otherParameterObject);
         return null;
     }
 }, ExecutorType.BATCH);
 
 
The template needs a SqlSessionFactory to create SqlSessions, passed in via the "sqlSessionFactory" property or as a constructor argument.

SqlSessionTemplate is thread safe, so a single instance can be shared by all DAOs; there should also be a small memory savings by doing this. This pattern can be used in Spring configuration files as follows:

 <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
     <property name="sqlSessionFactory" ref="sqlSessionFactory" />
   </bean>
 
 

Version:
$Id: SqlSessionTemplate.java 2987 2010-10-31 20:35:40Z eduardo.macarron $
See Also:
execute(org.mybatis.spring.SqlSessionCallback), setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory), SqlSessionFactoryBean.setDataSource(javax.sql.DataSource), SqlSessionFactory.getConfiguration(), SqlSession, SqlSessionOperations

Field Summary
 
Fields inherited from class org.springframework.jdbc.support.JdbcAccessor
logger
 
Constructor Summary
SqlSessionTemplate()
          This constructor is left here to enable the creation of the SqlSessionTemplate using this xml in the applicationContext.xml.
SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
           
 
Method Summary
 void afterPropertiesSet()
          
 int delete(String statement)
          
 int delete(String statement, Object parameter)
          
<T> T
execute(SqlSessionCallback<T> action)
          Execute the given data access action with the proper SqlSession (got from current transaction or a new one)
<T> T
execute(SqlSessionCallback<T> action, org.apache.ibatis.session.ExecutorType executorType)
          Execute the given data access action on a Executor.
 DataSource getDataSource()
          
<T> T
getMapper(Class<T> type)
          
 org.apache.ibatis.session.SqlSessionFactory getSqlSessionFactory()
           
 int insert(String statement)
          
 int insert(String statement, Object parameter)
          
 void select(String statement, Object parameter, org.apache.ibatis.session.ResultHandler handler)
          
 void select(String statement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds, org.apache.ibatis.session.ResultHandler handler)
          
 void select(String statement, org.apache.ibatis.session.ResultHandler handler)
          
<T> List<T>
selectList(String statement)
          
<T> List<T>
selectList(String statement, Object parameter)
          
<T> List<T>
selectList(String statement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds)
          
 Object selectOne(String statement)
          
 Object selectOne(String statement, Object parameter)
          
 void setDataSource(DataSource dataSource)
          
 void setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
           
 int update(String statement)
          
 int update(String statement, Object parameter)
          
protected  org.springframework.dao.DataAccessException wrapException(Throwable t)
          Translates MyBatis exceptions into Spring DataAccessExceptions.
 
Methods inherited from class org.springframework.jdbc.support.JdbcAccessor
getExceptionTranslator, isLazyInit, setDatabaseProductName, setExceptionTranslator, setLazyInit
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlSessionTemplate

public SqlSessionTemplate()
This constructor is left here to enable the creation of the SqlSessionTemplate using this xml in the applicationContext.xml. Otherwise constructor should be used and that will not match how other mybatis-spring beans are created.
 <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
   <property name="sqlSessionFactory" ref="sqlSessionFactory" />
 </bean>
 
 


SqlSessionTemplate

public SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
Method Detail

getSqlSessionFactory

public org.apache.ibatis.session.SqlSessionFactory getSqlSessionFactory()

setSqlSessionFactory

public void setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)

setDataSource

public void setDataSource(DataSource dataSource)

Overrides:
setDataSource in class org.springframework.jdbc.support.JdbcAccessor

getDataSource

public DataSource getDataSource()

Overrides:
getDataSource in class org.springframework.jdbc.support.JdbcAccessor

afterPropertiesSet

public void afterPropertiesSet()

Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Overrides:
afterPropertiesSet in class org.springframework.jdbc.support.JdbcAccessor

execute

public <T> T execute(SqlSessionCallback<T> action)
          throws org.springframework.dao.DataAccessException
Execute the given data access action with the proper SqlSession (got from current transaction or a new one)

Type Parameters:
T -
Parameters:
action -
Returns:
Throws:
org.springframework.dao.DataAccessException

execute

public <T> T execute(SqlSessionCallback<T> action,
                     org.apache.ibatis.session.ExecutorType executorType)
          throws org.springframework.dao.DataAccessException
Execute the given data access action on a Executor.

Parameters:
action - callback object that specifies the data access action
executorType - SIMPLE, REUSE, BATCH
Returns:
a result object returned by the action, or null
Throws:
org.springframework.dao.DataAccessException - in case of errors

selectOne

public Object selectOne(String statement)

Specified by:
selectOne in interface SqlSessionOperations
See Also:
SqlSession.selectOne(String)

selectOne

public Object selectOne(String statement,
                        Object parameter)

Specified by:
selectOne in interface SqlSessionOperations
See Also:
SqlSession.selectOne(String, Object)

selectList

public <T> List<T> selectList(String statement)

Specified by:
selectList in interface SqlSessionOperations
See Also:
SqlSession.selectList(String, Object)

selectList

public <T> List<T> selectList(String statement,
                              Object parameter)

Specified by:
selectList in interface SqlSessionOperations
See Also:
SqlSession.selectList(String, Object)

selectList

public <T> List<T> selectList(String statement,
                              Object parameter,
                              org.apache.ibatis.session.RowBounds rowBounds)

Specified by:
selectList in interface SqlSessionOperations
See Also:
SqlSession.selectList(String, Object, org.apache.ibatis.session.RowBounds)

select

public void select(String statement,
                   Object parameter,
                   org.apache.ibatis.session.ResultHandler handler)

Specified by:
select in interface SqlSessionOperations
See Also:
SqlSession.select(String, Object, org.apache.ibatis.session.ResultHandler)

select

public void select(String statement,
                   org.apache.ibatis.session.ResultHandler handler)

Specified by:
select in interface SqlSessionOperations
See Also:
SqlSession.select(String, org.apache.ibatis.session.ResultHandler)

select

public void select(String statement,
                   Object parameter,
                   org.apache.ibatis.session.RowBounds rowBounds,
                   org.apache.ibatis.session.ResultHandler handler)

Specified by:
select in interface SqlSessionOperations
See Also:
SqlSession.select(String, Object, org.apache.ibatis.session.RowBounds, org.apache.ibatis.session.ResultHandler)

insert

public int insert(String statement)

Specified by:
insert in interface SqlSessionOperations
See Also:
SqlSession.insert(String)

insert

public int insert(String statement,
                  Object parameter)

Specified by:
insert in interface SqlSessionOperations
See Also:
SqlSession.insert(String, Object)

update

public int update(String statement)

Specified by:
update in interface SqlSessionOperations
See Also:
SqlSession.update(String)

update

public int update(String statement,
                  Object parameter)

Specified by:
update in interface SqlSessionOperations
See Also:
SqlSession.update(String, Object)

delete

public int delete(String statement)

Specified by:
delete in interface SqlSessionOperations
See Also:
SqlSession.delete(String)

delete

public int delete(String statement,
                  Object parameter)

Specified by:
delete in interface SqlSessionOperations
See Also:
SqlSession.delete(String, Object)

getMapper

public <T> T getMapper(Class<T> type)

Specified by:
getMapper in interface SqlSessionOperations
See Also:
SqlSession.getMapper(Class)

wrapException

protected org.springframework.dao.DataAccessException wrapException(Throwable t)
Translates MyBatis exceptions into Spring DataAccessExceptions. It uses JdbcAccessor.getExceptionTranslator() for the SqlException translation

Parameters:
t - the exception has to be converted to DataAccessException.
Returns:
a Spring DataAccessException


Copyright © 2010 MyBatis.org. All Rights Reserved.