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.DaoSupport
Convenient 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:
  • Field Summary

    Fields inherited from class org.springframework.dao.support.DaoSupport

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    createSqlSessionTemplate(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
    Create a SqlSessionTemplate for the given SqlSessionFactory.
    org.apache.ibatis.session.SqlSession
    Users should use this method to get a SqlSession to call its statement methods This is SqlSession is managed by spring.
    final org.apache.ibatis.session.SqlSessionFactory
    Return the MyBatis SqlSessionFactory used by this DAO.
    Return the SqlSessionTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.
    void
    setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sqlSessionFactory)
    Set MyBatis SqlSessionFactory to be used by this DAO.
    void
    Set the SqlSessionTemplate for this DAO explicitly, as an alternative to specifying a SqlSessionFactory.

    Methods inherited from class org.springframework.dao.support.DaoSupport

    afterPropertiesSet, initDao

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SqlSessionDaoSupport

      public SqlSessionDaoSupport()
  • Method Details

    • 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:
    • 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:
    • 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 via new 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:
      checkDaoConfig in class org.springframework.dao.support.DaoSupport