public class

SqlSessionTemplate

extends Object
implements SqlSession DisposableBean
java.lang.Object
   ↳ org.mybatis.spring.SqlSessionTemplate

Class Overview

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>
 
 

See Also

Summary

Public Constructors
SqlSessionTemplate(SqlSessionFactory sqlSessionFactory)
Constructs a Spring managed SqlSession with the SqlSessionFactory provided as an argument.
SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, 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(SqlSessionFactory sqlSessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator)
Constructs a Spring managed SqlSession with the given SqlSessionFactory and ExecutorType.
Public Methods
void clearCache()
void close()
void commit(boolean force)
void commit()
int delete(String statement, Object parameter)
int delete(String statement)
void destroy()
Allow gently dispose bean:
 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg index="0" ref="sqlSessionFactory" />
 </bean>
 
The implementation of DisposableBean forces spring context to use destroy() method instead of close() to shutdown gently.
List<BatchResult> flushStatements()
Configuration getConfiguration()
Connection getConnection()
ExecutorType getExecutorType()
<T> T getMapper(Class<T> type)
PersistenceExceptionTranslator getPersistenceExceptionTranslator()
SqlSessionFactory getSqlSessionFactory()
int insert(String statement)
int insert(String statement, Object parameter)
void rollback(boolean force)
void rollback()
void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler)
void select(String statement, Object parameter, ResultHandler handler)
void select(String statement, ResultHandler handler)
<T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds)
<T> Cursor<T> selectCursor(String statement)
<T> Cursor<T> selectCursor(String statement, Object parameter)
<E> List<E> selectList(String statement, Object parameter)
<E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds)
<E> List<E> selectList(String statement)
<K, V> Map<K, V> selectMap(String statement, String mapKey)
<K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds)
<K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey)
<T> T selectOne(String statement, Object parameter)
<T> T selectOne(String statement)
int update(String statement, Object parameter)
int update(String statement)
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable
From interface org.apache.ibatis.session.SqlSession
From interface org.springframework.beans.factory.DisposableBean

Public Constructors

public SqlSessionTemplate (SqlSessionFactory sqlSessionFactory)

Constructs a Spring managed SqlSession with the SqlSessionFactory provided as an argument.

public SqlSessionTemplate (SqlSessionFactory sqlSessionFactory, 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.

public SqlSessionTemplate (SqlSessionFactory sqlSessionFactory, ExecutorType executorType, 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

Public Methods

public void clearCache ()

public void close ()

public void commit (boolean force)

public void commit ()

public int delete (String statement, Object parameter)

public int delete (String statement)

public void destroy ()

Allow gently dispose bean:

 <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  <constructor-arg index="0" ref="sqlSessionFactory" />
 </bean>
 
The implementation of DisposableBean forces spring context to use destroy() method instead of close() to shutdown gently.

Throws
Exception
See Also
  • close()
  • org.springframework.beans.factory.support.DisposableBeanAdapter#inferDestroyMethodIfNecessary
  • org.springframework.beans.factory.support.DisposableBeanAdapter#CLOSE_METHOD_NAME

public List<BatchResult> flushStatements ()

public Configuration getConfiguration ()

public Connection getConnection ()

public ExecutorType getExecutorType ()

public T getMapper (Class<T> type)

public PersistenceExceptionTranslator getPersistenceExceptionTranslator ()

public SqlSessionFactory getSqlSessionFactory ()

public int insert (String statement)

public int insert (String statement, Object parameter)

public void rollback (boolean force)

public void rollback ()

public void select (String statement, Object parameter, RowBounds rowBounds, ResultHandler handler)

public void select (String statement, Object parameter, ResultHandler handler)

public void select (String statement, ResultHandler handler)

public Cursor<T> selectCursor (String statement, Object parameter, RowBounds rowBounds)

public Cursor<T> selectCursor (String statement)

public Cursor<T> selectCursor (String statement, Object parameter)

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

public List<E> selectList (String statement, Object parameter, RowBounds rowBounds)

public List<E> selectList (String statement)

public Map<K, V> selectMap (String statement, String mapKey)

public Map<K, V> selectMap (String statement, Object parameter, String mapKey, RowBounds rowBounds)

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

public T selectOne (String statement, Object parameter)

public T selectOne (String statement)

public int update (String statement, Object parameter)

public int update (String statement)