org.mybatis.spring.support
Class SqlSessionDaoSupport

java.lang.Object
  extended by org.springframework.dao.support.DaoSupport
      extended by org.mybatis.spring.support.SqlSessionDaoSupport
All Implemented Interfaces:
org.springframework.beans.factory.InitializingBean

public abstract class SqlSessionDaoSupport
extends org.springframework.dao.support.DaoSupport

Convenient super class for MyBatis SqlSession data access objects. In the usual case, all that a DAO needs is an SqlSessionFactory. This class also supports passing in an SqlSessionTemplate if a custom DataSource or ExceptionTranslator is needed for a specific DAO.

By default, each DAO gets its own SqlSessionTemplate which holds the SqlSessionFactory. SqlSessionTemplate is thread safe, so a single instance cannot be shared by all DAOs; there should also be a small memory savings by doing this. To support a shared template, this class has a constructor that accepts an SqlSessionTemplate. This pattern can be used in Spring configuration files as follows:

   
     
       
         
       
     
   
 
   
 

Version:
$Id: SqlSessionDaoSupport.java 2332 2010-08-19 14:09:10Z simone.tripodi $
See Also:
setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory), setSqlSessionTemplate(org.mybatis.spring.SqlSessionTemplate), SqlSessionTemplate, JdbcAccessor.setExceptionTranslator(org.springframework.jdbc.support.SQLExceptionTranslator)

Field Summary
 
Fields inherited from class org.springframework.dao.support.DaoSupport
logger
 
Constructor Summary
SqlSessionDaoSupport()
           
SqlSessionDaoSupport(SqlSessionTemplate sessionTemplate)
           
 
Method Summary
protected  void checkDaoConfig()
           
 DataSource getDataSource()
          Return the JDBC DataSource used by this DAO.
 org.apache.ibatis.session.SqlSessionFactory getSqlSessionFactory()
          Return the SqlSessionFactory that this DAO uses.
 SqlSessionTemplate getSqlSessionTemplate()
          Return the SqlSessionTemplate for this DAO, pre-initialized with the SqlSessionFactory or set explicitly.
 void setDataSource(DataSource dataSource)
          Set the JDBC DataSource to be used by this DAO.
 void setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sessionFactory)
          Set the SqlSessionFactory to work with.
 void setSqlSessionTemplate(SqlSessionTemplate sessionTemplate)
          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 Detail

SqlSessionDaoSupport

public SqlSessionDaoSupport()

SqlSessionDaoSupport

public SqlSessionDaoSupport(SqlSessionTemplate sessionTemplate)
Method Detail

setDataSource

public final void setDataSource(DataSource dataSource)
Set the JDBC DataSource to be used by this DAO. Not required: The SqlSessionFactory defines a shared DataSource.

This is a no-op if an external SqlSessionTemplate has been set.

See Also:
setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory)

getDataSource

public final DataSource getDataSource()
Return the JDBC DataSource used by this DAO.


setSqlSessionFactory

public final void setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory sessionFactory)
Set the SqlSessionFactory to work with.

This is a no-op if an external SqlSessionTemplate has been set.

See Also:
setSqlSessionTemplate(org.mybatis.spring.SqlSessionTemplate)

getSqlSessionFactory

public final org.apache.ibatis.session.SqlSessionFactory getSqlSessionFactory()
Return the SqlSessionFactory that this DAO uses.


setSqlSessionTemplate

public final void setSqlSessionTemplate(SqlSessionTemplate sessionTemplate)
Set the SqlSessionTemplate for this DAO explicitly, as an alternative to specifying a SqlSessionFactory.

See Also:
setSqlSessionFactory(org.apache.ibatis.session.SqlSessionFactory)

getSqlSessionTemplate

public final SqlSessionTemplate getSqlSessionTemplate()
Return the SqlSessionTemplate for this DAO, pre-initialized with the SqlSessionFactory or set explicitly.


checkDaoConfig

protected void checkDaoConfig()
Specified by:
checkDaoConfig in class org.springframework.dao.support.DaoSupport


Copyright © 2010 MyBatis.org. All Rights Reserved.