Package org.mybatis.spring
Class SqlSessionTemplate
java.lang.Object
org.mybatis.spring.SqlSessionTemplate
- All Implemented Interfaces:
Closeable
,AutoCloseable
,org.apache.ibatis.session.SqlSession
,org.springframework.beans.factory.DisposableBean
public class SqlSessionTemplate
extends Object
implements org.apache.ibatis.session.SqlSession, org.springframework.beans.factory.DisposableBean
Thread safe, Spring managed,
SqlSession
that works with Spring transaction management to ensure 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>
- Author:
- Putthiphong Boonphong, Hunter Presnall, Eduardo Macarron
- See Also:
-
Constructor Summary
ConstructorDescriptionSqlSessionTemplate
(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory) Constructs a Spring managed SqlSession with theSqlSessionFactory
provided as an argument.SqlSessionTemplate
(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory, org.apache.ibatis.session.ExecutorType executorType) Constructs a Spring managed SqlSession with theSqlSessionFactory
provided as an argument and the givenExecutorType
ExecutorType
cannot be changed once theSqlSessionTemplate
is constructed.SqlSessionTemplate
(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory, org.apache.ibatis.session.ExecutorType executorType, org.springframework.dao.support.PersistenceExceptionTranslator exceptionTranslator) Constructs a Spring managedSqlSession
with the givenSqlSessionFactory
andExecutorType
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
close()
void
commit()
void
commit
(boolean force) int
int
void
destroy()
Allow gently dispose bean:List
<org.apache.ibatis.executor.BatchResult> org.apache.ibatis.session.Configuration
org.apache.ibatis.session.ExecutorType
<T> T
org.springframework.dao.support.PersistenceExceptionTranslator
org.apache.ibatis.session.SqlSessionFactory
int
int
void
rollback()
void
rollback
(boolean force) void
void
select
(String statement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds, org.apache.ibatis.session.ResultHandler handler) void
<T> org.apache.ibatis.cursor.Cursor
<T> selectCursor
(String statement) <T> org.apache.ibatis.cursor.Cursor
<T> selectCursor
(String statement, Object parameter) <T> org.apache.ibatis.cursor.Cursor
<T> selectCursor
(String statement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds) <E> List
<E> selectList
(String statement) <E> List
<E> selectList
(String statement, Object parameter) <E> List
<E> selectList
(String statement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds) <K,
V> Map <K, V> <K,
V> Map <K, V> selectMap
(String statement, Object parameter, String mapKey, org.apache.ibatis.session.RowBounds rowBounds) <K,
V> Map <K, V> <T> T
<T> T
int
int
-
Constructor Details
-
SqlSessionTemplate
public SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory) Constructs a Spring managed SqlSession with theSqlSessionFactory
provided as an argument.- Parameters:
sqlSessionFactory
- a factory of SqlSession
-
SqlSessionTemplate
public SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory, org.apache.ibatis.session.ExecutorType executorType) Constructs a Spring managed SqlSession with theSqlSessionFactory
provided as an argument and the givenExecutorType
ExecutorType
cannot be changed once theSqlSessionTemplate
is constructed.- Parameters:
sqlSessionFactory
- a factory of SqlSessionexecutorType
- an executor type on session
-
SqlSessionTemplate
public SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory, org.apache.ibatis.session.ExecutorType executorType, org.springframework.dao.support.PersistenceExceptionTranslator exceptionTranslator) Constructs a Spring managedSqlSession
with the givenSqlSessionFactory
andExecutorType
. A customSQLExceptionTranslator
can be provided as an argument so anyPersistenceException
thrown by MyBatis can be custom translated to aRuntimeException
TheSQLExceptionTranslator
can also be null and thus no exception translation will be done and MyBatis exceptions will be thrown- Parameters:
sqlSessionFactory
- a factory of SqlSessionexecutorType
- an executor type on sessionexceptionTranslator
- a translator of exception
-
-
Method Details
-
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
- Specified by:
selectOne
in interfaceorg.apache.ibatis.session.SqlSession
-
selectOne
-
selectMap
-
selectMap
-
selectMap
-
selectCursor
- Specified by:
selectCursor
in interfaceorg.apache.ibatis.session.SqlSession
-
selectCursor
-
selectCursor
-
selectList
-
selectList
-
selectList
-
select
- Specified by:
select
in interfaceorg.apache.ibatis.session.SqlSession
-
select
-
select
-
insert
- Specified by:
insert
in interfaceorg.apache.ibatis.session.SqlSession
-
insert
-
update
- Specified by:
update
in interfaceorg.apache.ibatis.session.SqlSession
-
update
-
delete
- Specified by:
delete
in interfaceorg.apache.ibatis.session.SqlSession
-
delete
-
getMapper
- Specified by:
getMapper
in interfaceorg.apache.ibatis.session.SqlSession
-
commit
public void commit()- Specified by:
commit
in interfaceorg.apache.ibatis.session.SqlSession
-
commit
public void commit(boolean force) - Specified by:
commit
in interfaceorg.apache.ibatis.session.SqlSession
-
rollback
public void rollback()- Specified by:
rollback
in interfaceorg.apache.ibatis.session.SqlSession
-
rollback
public void rollback(boolean force) - Specified by:
rollback
in interfaceorg.apache.ibatis.session.SqlSession
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceorg.apache.ibatis.session.SqlSession
-
clearCache
public void clearCache()- Specified by:
clearCache
in interfaceorg.apache.ibatis.session.SqlSession
-
getConfiguration
public org.apache.ibatis.session.Configuration getConfiguration()- Specified by:
getConfiguration
in interfaceorg.apache.ibatis.session.SqlSession
-
getConnection
- Specified by:
getConnection
in interfaceorg.apache.ibatis.session.SqlSession
-
flushStatements
- Specified by:
flushStatements
in interfaceorg.apache.ibatis.session.SqlSession
-
destroy
Allow gently dispose bean:<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean>
DisposableBean
forces spring context to useDisposableBean.destroy()
method instead ofclose()
to shutdown gently.- Specified by:
destroy
in interfaceorg.springframework.beans.factory.DisposableBean
- Throws:
Exception
- See Also:
-