org.mybatis.spring
Class SqlSessionTemplate

java.lang.Object
  extended by org.mybatis.spring.SqlSessionTemplate
All Implemented Interfaces:
org.apache.ibatis.session.SqlSession

public class SqlSessionTemplate
extends Object
implements org.apache.ibatis.session.SqlSession

Thread safe, Spring managed, SqlSession that works with Spring transaction management to ensure that that the actual SqlSession used is the one associated with the current Spring transaction. In addition, it manages the session life-cycle, including closing, committing or rolling back the session as necessary based on the Spring transaction configuration.

The template needs a SqlSessionFactory to create SqlSessions, passed as a constructor argument. It also can be constructed indicating the executor type to be used, if not, the default executor type, defined in the session factory will be used.

This template converts MyBatis PersistenceExceptions into unchecked DataAccessExceptions, using, by default, a MyBatisExceptionTranslator.

Because SqlSessionTemplate is thread safe, 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"> 
   <constructor-arg ref="sqlSessionFactory" /> 
 </bean>
 
 

Version:
$Id: SqlSessionTemplate.java 3908 2011-09-12 07:10:50Z [email protected] $
See Also:
SqlSessionFactory, MyBatisExceptionTranslator

Constructor Summary
SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
          Constructs a Spring managed SqlSession with the SqlSessionFactory provided as an argument.
SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory, org.apache.ibatis.session.ExecutorType executorType)
          Constructs a Spring managed SqlSession with the SqlSessionFactory provided as an argument and the given ExecutorType ExecutorType cannot be changed once the SqlSessionTemplate is constructed.
SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory, org.apache.ibatis.session.ExecutorType executorType, org.springframework.dao.support.PersistenceExceptionTranslator exceptionTranslator)
          Constructs a Spring managed SqlSession with the given SqlSessionFactory and ExecutorType.
 
Method Summary
 void clearCache()
          
 void close()
          
 void commit()
          
 void commit(boolean force)
          
 int delete(String statement)
          
 int delete(String statement, Object parameter)
          
 List<org.apache.ibatis.executor.BatchResult> flushStatements()
          
 org.apache.ibatis.session.Configuration getConfiguration()
          
 Connection getConnection()
          
 org.apache.ibatis.session.ExecutorType getExecutorType()
           
<T> T
getMapper(Class<T> type)
          
 org.springframework.dao.support.PersistenceExceptionTranslator getPersistenceExceptionTranslator()
           
 org.apache.ibatis.session.SqlSessionFactory getSqlSessionFactory()
           
 int insert(String statement)
          
 int insert(String statement, Object parameter)
          
 void rollback()
          
 void rollback(boolean force)
          
 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)
          
 List<?> selectList(String statement)
          
 List<?> selectList(String statement, Object parameter)
          
 List<?> selectList(String statement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds)
          
 Map<?,?> selectMap(String statement, Object parameter, String mapKey)
          
 Map<?,?> selectMap(String statement, Object parameter, String mapKey, org.apache.ibatis.session.RowBounds rowBounds)
          
 Map<?,?> selectMap(String statement, String mapKey)
          
 Object selectOne(String statement)
          
 Object selectOne(String statement, Object parameter)
          
 int update(String statement)
          
 int update(String statement, Object parameter)
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlSessionTemplate

public SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
Constructs a Spring managed SqlSession with the SqlSessionFactory provided as an argument.

Parameters:
sqlSessionFactory -

SqlSessionTemplate

public SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory,
                          org.apache.ibatis.session.ExecutorType executorType)
Constructs a Spring managed SqlSession with the SqlSessionFactory provided as an argument and the given ExecutorType ExecutorType cannot be changed once the SqlSessionTemplate is constructed.

Parameters:
sqlSessionFactory -
executorType -

SqlSessionTemplate

public SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory,
                          org.apache.ibatis.session.ExecutorType executorType,
                          org.springframework.dao.support.PersistenceExceptionTranslator exceptionTranslator)
Constructs a Spring managed SqlSession with the given SqlSessionFactory and ExecutorType. A custom SQLExceptionTranslator can be provided as an argument so any PersistenceException thrown by MyBatis can be custom translated to a RuntimeException The SQLExceptionTranslator can also be null and thus no exception translation will be done and MyBatis exceptions will be thrown

Parameters:
sqlSessionFactory -
executorType -
exceptionTranslator -
Method Detail

getSqlSessionFactory

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

getExecutorType

public org.apache.ibatis.session.ExecutorType getExecutorType()

getPersistenceExceptionTranslator

public org.springframework.dao.support.PersistenceExceptionTranslator getPersistenceExceptionTranslator()

selectOne

public Object selectOne(String statement)

Specified by:
selectOne in interface org.apache.ibatis.session.SqlSession

selectOne

public Object selectOne(String statement,
                        Object parameter)

Specified by:
selectOne in interface org.apache.ibatis.session.SqlSession

selectMap

public Map<?,?> selectMap(String statement,
                          String mapKey)

Specified by:
selectMap in interface org.apache.ibatis.session.SqlSession

selectMap

public Map<?,?> selectMap(String statement,
                          Object parameter,
                          String mapKey)

Specified by:
selectMap in interface org.apache.ibatis.session.SqlSession

selectMap

public Map<?,?> selectMap(String statement,
                          Object parameter,
                          String mapKey,
                          org.apache.ibatis.session.RowBounds rowBounds)

Specified by:
selectMap in interface org.apache.ibatis.session.SqlSession

selectList

public List<?> selectList(String statement)

Specified by:
selectList in interface org.apache.ibatis.session.SqlSession

selectList

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

Specified by:
selectList in interface org.apache.ibatis.session.SqlSession

selectList

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

Specified by:
selectList in interface org.apache.ibatis.session.SqlSession

select

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

Specified by:
select in interface org.apache.ibatis.session.SqlSession

select

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

Specified by:
select in interface org.apache.ibatis.session.SqlSession

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 org.apache.ibatis.session.SqlSession

insert

public int insert(String statement)

Specified by:
insert in interface org.apache.ibatis.session.SqlSession

insert

public int insert(String statement,
                  Object parameter)

Specified by:
insert in interface org.apache.ibatis.session.SqlSession

update

public int update(String statement)

Specified by:
update in interface org.apache.ibatis.session.SqlSession

update

public int update(String statement,
                  Object parameter)

Specified by:
update in interface org.apache.ibatis.session.SqlSession

delete

public int delete(String statement)

Specified by:
delete in interface org.apache.ibatis.session.SqlSession

delete

public int delete(String statement,
                  Object parameter)

Specified by:
delete in interface org.apache.ibatis.session.SqlSession

getMapper

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

Specified by:
getMapper in interface org.apache.ibatis.session.SqlSession

commit

public void commit()

Specified by:
commit in interface org.apache.ibatis.session.SqlSession

commit

public void commit(boolean force)

Specified by:
commit in interface org.apache.ibatis.session.SqlSession

rollback

public void rollback()

Specified by:
rollback in interface org.apache.ibatis.session.SqlSession

rollback

public void rollback(boolean force)

Specified by:
rollback in interface org.apache.ibatis.session.SqlSession

close

public void close()

Specified by:
close in interface org.apache.ibatis.session.SqlSession

clearCache

public void clearCache()

Specified by:
clearCache in interface org.apache.ibatis.session.SqlSession

getConfiguration

public org.apache.ibatis.session.Configuration getConfiguration()

Specified by:
getConfiguration in interface org.apache.ibatis.session.SqlSession

getConnection

public Connection getConnection()

Specified by:
getConnection in interface org.apache.ibatis.session.SqlSession

flushStatements

public List<org.apache.ibatis.executor.BatchResult> flushStatements()

Specified by:
flushStatements in interface org.apache.ibatis.session.SqlSession
Since:
1.0.2


Copyright © 2010-2011 MyBatis.org. All Rights Reserved.