Package org.mybatis.spring
Class SqlSessionTemplate
- java.lang.Object
-
- org.mybatis.spring.SqlSessionTemplate
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,org.apache.ibatis.session.SqlSession,org.springframework.beans.factory.DisposableBean
public class SqlSessionTemplate extends java.lang.Object implements org.apache.ibatis.session.SqlSession, org.springframework.beans.factory.DisposableBeanThread safe, Spring managed,SqlSessionthat 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>- Author:
- Putthiphong Boonphong, Hunter Presnall, Eduardo Macarron
- See Also:
SqlSessionFactory,MyBatisExceptionTranslator
-
-
Constructor Summary
Constructors Constructor Description SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)Constructs a Spring managed SqlSession with theSqlSessionFactoryprovided as an argument.SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory, org.apache.ibatis.session.ExecutorType executorType)Constructs a Spring managed SqlSession with theSqlSessionFactoryprovided as an argument and the givenExecutorTypeExecutorTypecannot be changed once theSqlSessionTemplateis constructed.SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory, org.apache.ibatis.session.ExecutorType executorType, org.springframework.dao.support.PersistenceExceptionTranslator exceptionTranslator)Constructs a Spring managedSqlSessionwith the givenSqlSessionFactoryandExecutorType.
-
Method Summary
Modifier and Type Method Description voidclearCache()voidclose()voidcommit()voidcommit(boolean force)intdelete(java.lang.String statement)intdelete(java.lang.String statement, java.lang.Object parameter)voiddestroy()Allow gently dispose bean:java.util.List<org.apache.ibatis.executor.BatchResult>flushStatements()org.apache.ibatis.session.ConfigurationgetConfiguration()java.sql.ConnectiongetConnection()org.apache.ibatis.session.ExecutorTypegetExecutorType()<T> TgetMapper(java.lang.Class<T> type)org.springframework.dao.support.PersistenceExceptionTranslatorgetPersistenceExceptionTranslator()org.apache.ibatis.session.SqlSessionFactorygetSqlSessionFactory()intinsert(java.lang.String statement)intinsert(java.lang.String statement, java.lang.Object parameter)voidrollback()voidrollback(boolean force)voidselect(java.lang.String statement, java.lang.Object parameter, org.apache.ibatis.session.ResultHandler handler)voidselect(java.lang.String statement, java.lang.Object parameter, org.apache.ibatis.session.RowBounds rowBounds, org.apache.ibatis.session.ResultHandler handler)voidselect(java.lang.String statement, org.apache.ibatis.session.ResultHandler handler)<T> org.apache.ibatis.cursor.Cursor<T>selectCursor(java.lang.String statement)<T> org.apache.ibatis.cursor.Cursor<T>selectCursor(java.lang.String statement, java.lang.Object parameter)<T> org.apache.ibatis.cursor.Cursor<T>selectCursor(java.lang.String statement, java.lang.Object parameter, org.apache.ibatis.session.RowBounds rowBounds)<E> java.util.List<E>selectList(java.lang.String statement)<E> java.util.List<E>selectList(java.lang.String statement, java.lang.Object parameter)<E> java.util.List<E>selectList(java.lang.String statement, java.lang.Object parameter, org.apache.ibatis.session.RowBounds rowBounds)<K,V>
java.util.Map<K,V>selectMap(java.lang.String statement, java.lang.Object parameter, java.lang.String mapKey)<K,V>
java.util.Map<K,V>selectMap(java.lang.String statement, java.lang.Object parameter, java.lang.String mapKey, org.apache.ibatis.session.RowBounds rowBounds)<K,V>
java.util.Map<K,V>selectMap(java.lang.String statement, java.lang.String mapKey)<T> TselectOne(java.lang.String statement)<T> TselectOne(java.lang.String statement, java.lang.Object parameter)intupdate(java.lang.String statement)intupdate(java.lang.String statement, java.lang.Object parameter)
-
-
-
Constructor Detail
-
SqlSessionTemplate
public SqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
Constructs a Spring managed SqlSession with theSqlSessionFactoryprovided 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 theSqlSessionFactoryprovided as an argument and the givenExecutorTypeExecutorTypecannot be changed once theSqlSessionTemplateis 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 managedSqlSessionwith the givenSqlSessionFactoryandExecutorType. A customSQLExceptionTranslatorcan be provided as an argument so anyPersistenceExceptionthrown by MyBatis can be custom translated to aRuntimeExceptionTheSQLExceptionTranslatorcan 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 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 <T> T selectOne(java.lang.String statement)
- Specified by:
selectOnein interfaceorg.apache.ibatis.session.SqlSession
-
selectOne
public <T> T selectOne(java.lang.String statement, java.lang.Object parameter)- Specified by:
selectOnein interfaceorg.apache.ibatis.session.SqlSession
-
selectMap
public <K,V> java.util.Map<K,V> selectMap(java.lang.String statement, java.lang.String mapKey)- Specified by:
selectMapin interfaceorg.apache.ibatis.session.SqlSession
-
selectMap
public <K,V> java.util.Map<K,V> selectMap(java.lang.String statement, java.lang.Object parameter, java.lang.String mapKey)- Specified by:
selectMapin interfaceorg.apache.ibatis.session.SqlSession
-
selectMap
public <K,V> java.util.Map<K,V> selectMap(java.lang.String statement, java.lang.Object parameter, java.lang.String mapKey, org.apache.ibatis.session.RowBounds rowBounds)- Specified by:
selectMapin interfaceorg.apache.ibatis.session.SqlSession
-
selectCursor
public <T> org.apache.ibatis.cursor.Cursor<T> selectCursor(java.lang.String statement)
- Specified by:
selectCursorin interfaceorg.apache.ibatis.session.SqlSession
-
selectCursor
public <T> org.apache.ibatis.cursor.Cursor<T> selectCursor(java.lang.String statement, java.lang.Object parameter)- Specified by:
selectCursorin interfaceorg.apache.ibatis.session.SqlSession
-
selectCursor
public <T> org.apache.ibatis.cursor.Cursor<T> selectCursor(java.lang.String statement, java.lang.Object parameter, org.apache.ibatis.session.RowBounds rowBounds)- Specified by:
selectCursorin interfaceorg.apache.ibatis.session.SqlSession
-
selectList
public <E> java.util.List<E> selectList(java.lang.String statement)
- Specified by:
selectListin interfaceorg.apache.ibatis.session.SqlSession
-
selectList
public <E> java.util.List<E> selectList(java.lang.String statement, java.lang.Object parameter)- Specified by:
selectListin interfaceorg.apache.ibatis.session.SqlSession
-
selectList
public <E> java.util.List<E> selectList(java.lang.String statement, java.lang.Object parameter, org.apache.ibatis.session.RowBounds rowBounds)- Specified by:
selectListin interfaceorg.apache.ibatis.session.SqlSession
-
select
public void select(java.lang.String statement, org.apache.ibatis.session.ResultHandler handler)- Specified by:
selectin interfaceorg.apache.ibatis.session.SqlSession
-
select
public void select(java.lang.String statement, java.lang.Object parameter, org.apache.ibatis.session.ResultHandler handler)- Specified by:
selectin interfaceorg.apache.ibatis.session.SqlSession
-
select
public void select(java.lang.String statement, java.lang.Object parameter, org.apache.ibatis.session.RowBounds rowBounds, org.apache.ibatis.session.ResultHandler handler)- Specified by:
selectin interfaceorg.apache.ibatis.session.SqlSession
-
insert
public int insert(java.lang.String statement)
- Specified by:
insertin interfaceorg.apache.ibatis.session.SqlSession
-
insert
public int insert(java.lang.String statement, java.lang.Object parameter)- Specified by:
insertin interfaceorg.apache.ibatis.session.SqlSession
-
update
public int update(java.lang.String statement)
- Specified by:
updatein interfaceorg.apache.ibatis.session.SqlSession
-
update
public int update(java.lang.String statement, java.lang.Object parameter)- Specified by:
updatein interfaceorg.apache.ibatis.session.SqlSession
-
delete
public int delete(java.lang.String statement)
- Specified by:
deletein interfaceorg.apache.ibatis.session.SqlSession
-
delete
public int delete(java.lang.String statement, java.lang.Object parameter)- Specified by:
deletein interfaceorg.apache.ibatis.session.SqlSession
-
getMapper
public <T> T getMapper(java.lang.Class<T> type)
- Specified by:
getMapperin interfaceorg.apache.ibatis.session.SqlSession
-
commit
public void commit()
- Specified by:
commitin interfaceorg.apache.ibatis.session.SqlSession
-
commit
public void commit(boolean force)
- Specified by:
commitin interfaceorg.apache.ibatis.session.SqlSession
-
rollback
public void rollback()
- Specified by:
rollbackin interfaceorg.apache.ibatis.session.SqlSession
-
rollback
public void rollback(boolean force)
- Specified by:
rollbackin interfaceorg.apache.ibatis.session.SqlSession
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfaceorg.apache.ibatis.session.SqlSession
-
clearCache
public void clearCache()
- Specified by:
clearCachein interfaceorg.apache.ibatis.session.SqlSession
-
getConfiguration
public org.apache.ibatis.session.Configuration getConfiguration()
- Specified by:
getConfigurationin interfaceorg.apache.ibatis.session.SqlSession
-
getConnection
public java.sql.Connection getConnection()
- Specified by:
getConnectionin interfaceorg.apache.ibatis.session.SqlSession
-
flushStatements
public java.util.List<org.apache.ibatis.executor.BatchResult> flushStatements()
- Specified by:
flushStatementsin interfaceorg.apache.ibatis.session.SqlSession- Since:
- 1.0.2
-
destroy
public void destroy()
Allow gently dispose bean:
The implementation of<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean>DisposableBeanforces spring context to useDisposableBean.destroy()method instead ofclose()to shutdown gently.- Specified by:
destroyin interfaceorg.springframework.beans.factory.DisposableBean- See Also:
close(), "org.springframework.beans.factory.support.DisposableBeanAdapter#inferDestroyMethodIfNecessary(Object, RootBeanDefinition)", "org.springframework.beans.factory.support.DisposableBeanAdapter#CLOSE_METHOD_NAME"
-
-