org.mybatis.spring
Class SqlSessionFactoryBean

java.lang.Object
  extended by org.mybatis.spring.SqlSessionFactoryBean
All Implemented Interfaces:
org.springframework.beans.factory.FactoryBean<org.apache.ibatis.session.SqlSessionFactory>, org.springframework.beans.factory.InitializingBean

public class SqlSessionFactoryBean
extends Object
implements org.springframework.beans.factory.FactoryBean<org.apache.ibatis.session.SqlSessionFactory>, org.springframework.beans.factory.InitializingBean

FactoryBean that creates an MyBatis SqlSessionFactory. This is the usual way to set up a shared MyBatis SqlSessionFactory in a Spring application context; the SqlSessionFactory can then be passed to MyBatis-based DAOs via dependency injection. Either DataSourceTransactionManager or JtaTransactionManager can be used for transaction demarcation in combination with a SqlSessionFactory. JTA should be used for transactions which span multiple databases or when container managed transactions (CMT) are being used. Allows for specifying a DataSource at the SqlSessionFactory level. This is preferable to per-DAO DataSource references, as it allows for lazy loading and avoids repeated DataSource references in every DAO.

Version:
$Id: SqlSessionFactoryBean.java 3266 2010-11-22 06:56:51Z simone.tripodi $
See Also:
setConfigLocation(org.springframework.core.io.Resource), setDataSource(javax.sql.DataSource)

Constructor Summary
SqlSessionFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
          
protected  org.apache.ibatis.session.SqlSessionFactory buildSqlSessionFactory()
          Build a SqlSessionFactory instance.
 org.apache.ibatis.session.SqlSessionFactory getObject()
          
 Class<? extends org.apache.ibatis.session.SqlSessionFactory> getObjectType()
          
 boolean isSingleton()
          
 void setConfigLocation(org.springframework.core.io.Resource configLocation)
          Set the location of the MyBatis SqlSessionFactory config file.
 void setConfigurationProperties(Properties sqlSessionFactoryProperties)
          Set optional properties to be passed into the SqlSession configuration, as alternative to a <properties> tag in the configuration xml file.
 void setDataSource(DataSource dataSource)
          Set the JDBC DataSource that this instance should manage transactions for.
 void setEnvironment(String environment)
          NOTE: This class overrides any Environment you have set in the MyBatis config file.
 void setMapperLocations(org.springframework.core.io.Resource[] mapperLocations)
          Set locations of MyBatis mapper files that are going to be merged into the SqlSessionFactory configuration at runtime.
 void setSqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder sqlSessionFactoryBuilder)
          Sets the SqlSessionFactoryBuilder to use when creating the SqlSessionFactory.
<TF extends org.apache.ibatis.transaction.TransactionFactory>
void
setTransactionFactoryClass(Class<TF> transactionFactoryClass)
          Set the MyBatis TransactionFactory class to use.
 void setTransactionFactoryProperties(Properties transactionFactoryProperties)
          Set properties to be passed to the TransactionFactory instance used by this SqlSessionFactory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SqlSessionFactoryBean

public SqlSessionFactoryBean()
Method Detail

setConfigLocation

public void setConfigLocation(org.springframework.core.io.Resource configLocation)
Set the location of the MyBatis SqlSessionFactory config file. A typical value is "WEB-INF/mybatis-configuration.xml".


setMapperLocations

public void setMapperLocations(org.springframework.core.io.Resource[] mapperLocations)
Set locations of MyBatis mapper files that are going to be merged into the SqlSessionFactory configuration at runtime. This is an alternative to specifying "<sqlmapper>" entries in an MyBatis config file. This property being based on Spring's resource abstraction also allows for specifying resource patterns here: e.g. "classpath*:sqlmap/*-mapper.xml".


setConfigurationProperties

public void setConfigurationProperties(Properties sqlSessionFactoryProperties)
Set optional properties to be passed into the SqlSession configuration, as alternative to a <properties> tag in the configuration xml file. This will be used to resolve placeholders in the config file.

See Also:
Configuration.getVariables()

setDataSource

public void setDataSource(DataSource dataSource)
Set the JDBC DataSource that this instance should manage transactions for. The DataSource should match the one used by the SqlSessionFactory: for example, you could specify the same JNDI DataSource for both. A transactional JDBC Connection for this DataSource will be provided to application code accessing this DataSource directly via DataSourceUtils or DataSourceTransactionManager. The DataSource specified here should be the target DataSource to manage transactions for, not a TransactionAwareDataSourceProxy. Only data access code may work with TransactionAwareDataSourceProxy, while the transaction manager needs to work on the underlying target DataSource. If there's nevertheless a TransactionAwareDataSourceProxy passed in, it will be unwrapped to extract its target DataSource.

See Also:
TransactionAwareDataSourceProxy, DataSourceUtils, DataSourceTransactionManager

setSqlSessionFactoryBuilder

public void setSqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder sqlSessionFactoryBuilder)
Sets the SqlSessionFactoryBuilder to use when creating the SqlSessionFactory. This is mainly meant for testing so that mock SqlSessionFactory classes can be injected. By default, SqlSessionFactoryBuilder creates DefaultSqlSessionFactory instances.

See Also:
SqlSessionFactoryBuilder

setTransactionFactoryClass

public <TF extends org.apache.ibatis.transaction.TransactionFactory> void setTransactionFactoryClass(Class<TF> transactionFactoryClass)
Set the MyBatis TransactionFactory class to use. Default is SpringManagedTransactionFactory. The default SpringManagedTransactionFactory should be appropriate for all cases: be it Spring transaction management, EJB CMT or plain JTA. If there is no active transaction, SqlSession operations will execute SQL statements non-transactionally. It is strongly recommended to use the default TransactionFactory. If not used, any attempt at getting an SqlSession through Spring's MyBatis framework will throw an exception if a transaction is active.

Type Parameters:
TF - the MyBatis TransactionFactory type
Parameters:
transactionFactoryClass - the MyBatis TransactionFactory class to use
See Also:
setDataSource(javax.sql.DataSource), setTransactionFactoryProperties(java.util.Properties), TransactionFactory, SpringManagedTransactionFactory, Transaction

setTransactionFactoryProperties

public void setTransactionFactoryProperties(Properties transactionFactoryProperties)
Set properties to be passed to the TransactionFactory instance used by this SqlSessionFactory. The default SpringManagedTransactionFactory does not have any user configurable properties.

See Also:
TransactionFactory.setProperties(java.util.Properties), JdbcTransactionFactory, ManagedTransactionFactory

setEnvironment

public void setEnvironment(String environment)
NOTE: This class overrides any Environment you have set in the MyBatis config file. This is used only as a placeholder name. The default value is SqlSessionFactoryBean.class.getSimpleName().

Parameters:
environment - the environment name

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception

Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
Exception

buildSqlSessionFactory

protected org.apache.ibatis.session.SqlSessionFactory buildSqlSessionFactory()
                                                                      throws IOException,
                                                                             IllegalAccessException,
                                                                             InstantiationException
Build a SqlSessionFactory instance. The default implementation uses the standard MyBatis XMLConfigBuilder API to build a SqlSessionFactory instance based on an Reader.

Returns:
SqlSessionFactory
Throws:
IOException - if loading the config file failed
IllegalAccessException
InstantiationException
See Also:
XMLConfigBuilder.parse()

getObject

public org.apache.ibatis.session.SqlSessionFactory getObject()
                                                      throws Exception

Specified by:
getObject in interface org.springframework.beans.factory.FactoryBean<org.apache.ibatis.session.SqlSessionFactory>
Throws:
Exception

getObjectType

public Class<? extends org.apache.ibatis.session.SqlSessionFactory> getObjectType()

Specified by:
getObjectType in interface org.springframework.beans.factory.FactoryBean<org.apache.ibatis.session.SqlSessionFactory>

isSingleton

public boolean isSingleton()

Specified by:
isSingleton in interface org.springframework.beans.factory.FactoryBean<org.apache.ibatis.session.SqlSessionFactory>


Copyright © 2010 MyBatis.org. All Rights Reserved.