Package org.mybatis.spring.support
Class SqlSessionDaoSupport
- java.lang.Object
-
- org.springframework.dao.support.DaoSupport
-
- org.mybatis.spring.support.SqlSessionDaoSupport
-
- All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean
- Direct Known Subclasses:
MapperFactoryBean
public abstract class SqlSessionDaoSupport extends org.springframework.dao.support.DaoSupportConvenient super class for MyBatis SqlSession data access objects. It gives you access to the template which can then be used to execute SQL methods.This class needs a SqlSessionTemplate or a SqlSessionFactory. If both are set the SqlSessionFactory will be ignored.
- Author:
- Putthiphong Boonphong, Eduardo Macarron
- See Also:
setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory),setSqlSessionTemplate(org.mybatis.spring.SqlSessionTemplate),SqlSessionTemplate
-
-
Constructor Summary
Constructors Constructor Description SqlSessionDaoSupport()
-
Method Summary
Modifier and Type Method Description protected voidcheckDaoConfig()protected SqlSessionTemplatecreateSqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)Create a SqlSessionTemplate for the given SqlSessionFactory.org.apache.ibatis.session.SqlSessiongetSqlSession()Users should use this method to get a SqlSession to call its statement methods This is SqlSession is managed by spring.org.apache.ibatis.session.SqlSessionFactorygetSqlSessionFactory()Return the MyBatis SqlSessionFactory used by this DAO.SqlSessionTemplategetSqlSessionTemplate()Return the SqlSessionTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.voidsetSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)Set MyBatis SqlSessionFactory to be used by this DAO.voidsetSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate)Set the SqlSessionTemplate for this DAO explicitly, as an alternative to specifying a SqlSessionFactory.
-
-
-
Method Detail
-
setSqlSessionFactory
public void setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
Set MyBatis SqlSessionFactory to be used by this DAO. Will automatically create SqlSessionTemplate for the given SqlSessionFactory.- Parameters:
sqlSessionFactory- a factory of SqlSession
-
createSqlSessionTemplate
protected SqlSessionTemplate createSqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
Create a SqlSessionTemplate for the given SqlSessionFactory. Only invoked if populating the DAO with a SqlSessionFactory reference!Can be overridden in subclasses to provide a SqlSessionTemplate instance with different configuration, or a custom SqlSessionTemplate subclass.
- Parameters:
sqlSessionFactory- the MyBatis SqlSessionFactory to create a SqlSessionTemplate for- Returns:
- the new SqlSessionTemplate instance
- See Also:
setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory)
-
getSqlSessionFactory
public final org.apache.ibatis.session.SqlSessionFactory getSqlSessionFactory()
Return the MyBatis SqlSessionFactory used by this DAO.- Returns:
- a factory of SqlSession
-
setSqlSessionTemplate
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate)
Set the SqlSessionTemplate for this DAO explicitly, as an alternative to specifying a SqlSessionFactory.- Parameters:
sqlSessionTemplate- a template of SqlSession- See Also:
setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory)
-
getSqlSession
public org.apache.ibatis.session.SqlSession getSqlSession()
Users should use this method to get a SqlSession to call its statement methods This is SqlSession is managed by spring. Users should not commit/rollback/close it because it will be automatically done.- Returns:
- Spring managed thread safe SqlSession
-
getSqlSessionTemplate
public SqlSessionTemplate getSqlSessionTemplate()
Return the SqlSessionTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.Note: The returned SqlSessionTemplate is a shared instance. You may introspect its configuration, but not modify the configuration (other than from within an
DaoSupport.initDao()implementation). Consider creating a custom SqlSessionTemplate instance vianew SqlSessionTemplate(getSqlSessionFactory()), in which case you're allowed to customize the settings on the resulting instance.- Returns:
- a template of SqlSession
-
checkDaoConfig
protected void checkDaoConfig()
- Specified by:
checkDaoConfigin classorg.springframework.dao.support.DaoSupport
-
-