public class SqlSessionTemplate extends Object implements org.apache.ibatis.session.SqlSession, org.springframework.beans.factory.DisposableBean
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>
SqlSessionFactory,
MyBatisExceptionTranslator| Constructor and Description |
|---|
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. |
| Modifier and Type | Method and Description |
|---|---|
void |
clearCache() |
void |
close() |
void |
commit() |
void |
commit(boolean force) |
int |
delete(String statement) |
int |
delete(String statement,
Object parameter) |
void |
destroy()
Allow gently dispose bean:
|
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) |
<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> |
selectMap(String statement,
Object parameter,
String mapKey) |
<K,V> Map<K,V> |
selectMap(String statement,
Object parameter,
String mapKey,
org.apache.ibatis.session.RowBounds rowBounds) |
<K,V> Map<K,V> |
selectMap(String statement,
String mapKey) |
<T> T |
selectOne(String statement) |
<T> T |
selectOne(String statement,
Object parameter) |
int |
update(String statement) |
int |
update(String statement,
Object parameter) |
public SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
SqlSessionFactory provided as an argument.sqlSessionFactory - a factory of SqlSessionpublic SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory,
org.apache.ibatis.session.ExecutorType executorType)
SqlSessionFactory provided as an argument and the given
ExecutorType ExecutorType cannot be changed once the SqlSessionTemplate is constructed.sqlSessionFactory - a factory of SqlSessionexecutorType - an executor type on sessionpublic SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory,
org.apache.ibatis.session.ExecutorType executorType,
org.springframework.dao.support.PersistenceExceptionTranslator exceptionTranslator)
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 thrownsqlSessionFactory - a factory of SqlSessionexecutorType - an executor type on sessionexceptionTranslator - a translator of exceptionpublic org.apache.ibatis.session.SqlSessionFactory getSqlSessionFactory()
public org.apache.ibatis.session.ExecutorType getExecutorType()
public org.springframework.dao.support.PersistenceExceptionTranslator getPersistenceExceptionTranslator()
public <T> T selectOne(String statement)
selectOne in interface org.apache.ibatis.session.SqlSessionpublic <T> T selectOne(String statement, Object parameter)
selectOne in interface org.apache.ibatis.session.SqlSessionpublic <K,V> Map<K,V> selectMap(String statement, String mapKey)
selectMap in interface org.apache.ibatis.session.SqlSessionpublic <K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey)
selectMap in interface org.apache.ibatis.session.SqlSessionpublic <K,V> Map<K,V> selectMap(String statement, Object parameter, String mapKey, org.apache.ibatis.session.RowBounds rowBounds)
selectMap in interface org.apache.ibatis.session.SqlSessionpublic <T> org.apache.ibatis.cursor.Cursor<T> selectCursor(String statement)
selectCursor in interface org.apache.ibatis.session.SqlSessionpublic <T> org.apache.ibatis.cursor.Cursor<T> selectCursor(String statement, Object parameter)
selectCursor in interface org.apache.ibatis.session.SqlSessionpublic <T> org.apache.ibatis.cursor.Cursor<T> selectCursor(String statement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds)
selectCursor in interface org.apache.ibatis.session.SqlSessionpublic <E> List<E> selectList(String statement)
selectList in interface org.apache.ibatis.session.SqlSessionpublic <E> List<E> selectList(String statement, Object parameter)
selectList in interface org.apache.ibatis.session.SqlSessionpublic <E> List<E> selectList(String statement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds)
selectList in interface org.apache.ibatis.session.SqlSessionpublic void select(String statement, org.apache.ibatis.session.ResultHandler handler)
select in interface org.apache.ibatis.session.SqlSessionpublic void select(String statement, Object parameter, org.apache.ibatis.session.ResultHandler handler)
select in interface org.apache.ibatis.session.SqlSessionpublic void select(String statement, Object parameter, org.apache.ibatis.session.RowBounds rowBounds, org.apache.ibatis.session.ResultHandler handler)
select in interface org.apache.ibatis.session.SqlSessionpublic int insert(String statement)
insert in interface org.apache.ibatis.session.SqlSessionpublic int insert(String statement, Object parameter)
insert in interface org.apache.ibatis.session.SqlSessionpublic int update(String statement)
update in interface org.apache.ibatis.session.SqlSessionpublic int update(String statement, Object parameter)
update in interface org.apache.ibatis.session.SqlSessionpublic int delete(String statement)
delete in interface org.apache.ibatis.session.SqlSessionpublic int delete(String statement, Object parameter)
delete in interface org.apache.ibatis.session.SqlSessionpublic <T> T getMapper(Class<T> type)
getMapper in interface org.apache.ibatis.session.SqlSessionpublic void commit()
commit in interface org.apache.ibatis.session.SqlSessionpublic void commit(boolean force)
commit in interface org.apache.ibatis.session.SqlSessionpublic void rollback()
rollback in interface org.apache.ibatis.session.SqlSessionpublic void rollback(boolean force)
rollback in interface org.apache.ibatis.session.SqlSessionpublic void close()
close in interface Closeableclose in interface AutoCloseableclose in interface org.apache.ibatis.session.SqlSessionpublic void clearCache()
clearCache in interface org.apache.ibatis.session.SqlSessionpublic org.apache.ibatis.session.Configuration getConfiguration()
getConfiguration in interface org.apache.ibatis.session.SqlSessionpublic Connection getConnection()
getConnection in interface org.apache.ibatis.session.SqlSessionpublic List<org.apache.ibatis.executor.BatchResult> flushStatements()
flushStatements in interface org.apache.ibatis.session.SqlSessionpublic void destroy()
throws Exception
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
The implementation of DisposableBean forces spring context to use DisposableBean.destroy() method
instead of close() to shutdown gently.destroy in interface org.springframework.beans.factory.DisposableBeanExceptionclose(),
"org.springframework.beans.factory.support.DisposableBeanAdapter#inferDestroyMethodIfNecessary(Object, RootBeanDefinition)",
"org.springframework.beans.factory.support.DisposableBeanAdapter#CLOSE_METHOD_NAME"Copyright © 2010–2020 MyBatis.org. All rights reserved.