Class SqlSessionFactoryBean

java.lang.Object
org.mybatis.spring.SqlSessionFactoryBean
All Implemented Interfaces:
EventListener, org.springframework.beans.factory.FactoryBean<org.apache.ibatis.session.SqlSessionFactory>, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>

public class SqlSessionFactoryBean extends Object implements org.springframework.beans.factory.FactoryBean<org.apache.ibatis.session.SqlSessionFactory>, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>
FactoryBean that creates a 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.

Author:
Putthiphong Boonphong, Hunter Presnall, Eduardo Macarron, EddĂș MelĂ©ndez, Kazuki Shimizu, Jens Schauder
See Also:
  • Field Summary

    Fields inherited from interface org.springframework.beans.factory.FactoryBean

    OBJECT_TYPE_ATTRIBUTE
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addMapperLocations(org.springframework.core.io.Resource... mapperLocations)
    Add locations of MyBatis mapper files that are going to be merged into the SqlSessionFactory configuration at runtime.
    void
    addPlugins(org.apache.ibatis.plugin.Interceptor... plugins)
    Add Mybatis plugins.
    void
    addScriptingLanguageDrivers(org.apache.ibatis.scripting.LanguageDriver... scriptingLanguageDrivers)
    Add scripting language drivers.
    void
    addTypeAliases(Class<?>... typeAliases)
    Add type aliases.
    void
    addTypeHandlers(org.apache.ibatis.type.TypeHandler<?>... typeHandlers)
    Add type handlers.
    void
    protected org.apache.ibatis.session.SqlSessionFactory
    Build a SqlSessionFactory instance.
    org.apache.ibatis.cache.Cache
    Gets the Cache.
    org.apache.ibatis.mapping.DatabaseIdProvider
    Gets the DatabaseIdProvider
    org.apache.ibatis.session.SqlSessionFactory
    Class<? extends org.apache.ibatis.session.SqlSessionFactory>
    Class<? extends org.apache.ibatis.io.VFS>
    Gets the VFS.
    boolean
    void
    onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent event)
    void
    setCache(org.apache.ibatis.cache.Cache cache)
    Sets the Cache.
    void
    setConfigLocation(org.springframework.core.io.Resource configLocation)
    Set the location of the MyBatis SqlSessionFactory config file.
    void
    setConfiguration(org.apache.ibatis.session.Configuration configuration)
    Set a customized MyBatis configuration.
    void
    setConfigurationProperties(Properties sqlSessionFactoryProperties)
    Set optional properties to be passed into the SqlSession configuration, as alternative to a &lt;properties&gt; tag in the configuration xml file.
    void
    setDatabaseIdProvider(org.apache.ibatis.mapping.DatabaseIdProvider databaseIdProvider)
    Sets the DatabaseIdProvider.
    void
    Set the JDBC DataSource that this instance should manage transactions for.
    void
    setDefaultEnumTypeHandler(Class<? extends org.apache.ibatis.type.TypeHandler> defaultEnumTypeHandler)
    Set the default type handler class for enum.
    void
    setDefaultScriptingLanguageDriver(Class<? extends org.apache.ibatis.scripting.LanguageDriver> defaultScriptingLanguageDriver)
    Set a default scripting language driver class.
    void
    setEnvironment(String environment)
    NOTE: This class overrides any Environment you have set in the MyBatis config file.
    void
    setFailFast(boolean failFast)
    If true, a final check is done on Configuration to assure that all mapped statements are fully loaded and there is no one still pending to resolve includes.
    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
    setObjectFactory(org.apache.ibatis.reflection.factory.ObjectFactory objectFactory)
    Sets the ObjectFactory.
    void
    setObjectWrapperFactory(org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory objectWrapperFactory)
    Sets the ObjectWrapperFactory.
    void
    setPlugins(org.apache.ibatis.plugin.Interceptor... plugins)
    Mybatis plugin list.
    void
    setScriptingLanguageDrivers(org.apache.ibatis.scripting.LanguageDriver... scriptingLanguageDrivers)
    Set scripting language drivers.
    void
    setSqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder sqlSessionFactoryBuilder)
    Sets the SqlSessionFactoryBuilder to use when creating the SqlSessionFactory.
    void
    setTransactionFactory(org.apache.ibatis.transaction.TransactionFactory transactionFactory)
    Set the MyBatis TransactionFactory to use.
    void
    setTypeAliases(Class<?>... typeAliases)
    List of type aliases to register.
    void
    setTypeAliasesPackage(String typeAliasesPackage)
    Packages to search for type aliases.
    void
    setTypeAliasesSuperType(Class<?> typeAliasesSuperType)
    Super class which domain objects have to extend to have a type alias created.
    void
    setTypeHandlers(org.apache.ibatis.type.TypeHandler<?>... typeHandlers)
    Set type handlers.
    void
    setTypeHandlersPackage(String typeHandlersPackage)
    Packages to search for type handlers.
    void
    setVfs(Class<? extends org.apache.ibatis.io.VFS> vfs)
    Sets the VFS.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.context.ApplicationListener

    supportsAsyncExecution
  • Constructor Details

    • SqlSessionFactoryBean

      public SqlSessionFactoryBean()
  • Method Details

    • setObjectFactory

      public void setObjectFactory(org.apache.ibatis.reflection.factory.ObjectFactory objectFactory)
      Sets the ObjectFactory.
      Parameters:
      objectFactory - a custom ObjectFactory
      Since:
      1.1.2
    • setObjectWrapperFactory

      public void setObjectWrapperFactory(org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory objectWrapperFactory)
      Sets the ObjectWrapperFactory.
      Parameters:
      objectWrapperFactory - a specified ObjectWrapperFactory
      Since:
      1.1.2
    • getDatabaseIdProvider

      public org.apache.ibatis.mapping.DatabaseIdProvider getDatabaseIdProvider()
      Gets the DatabaseIdProvider
      Returns:
      a specified DatabaseIdProvider
      Since:
      1.1.0
    • setDatabaseIdProvider

      public void setDatabaseIdProvider(org.apache.ibatis.mapping.DatabaseIdProvider databaseIdProvider)
      Sets the DatabaseIdProvider. As of version 1.2.2 this variable is not initialized by default.
      Parameters:
      databaseIdProvider - a DatabaseIdProvider
      Since:
      1.1.0
    • getVfs

      public Class<? extends org.apache.ibatis.io.VFS> getVfs()
      Gets the VFS.
      Returns:
      a specified VFS
    • setVfs

      public void setVfs(Class<? extends org.apache.ibatis.io.VFS> vfs)
      Sets the VFS.
      Parameters:
      vfs - a VFS
    • getCache

      public org.apache.ibatis.cache.Cache getCache()
      Gets the Cache.
      Returns:
      a specified Cache
    • setCache

      public void setCache(org.apache.ibatis.cache.Cache cache)
      Sets the Cache.
      Parameters:
      cache - a Cache
    • setPlugins

      public void setPlugins(org.apache.ibatis.plugin.Interceptor... plugins)
      Mybatis plugin list.
      Parameters:
      plugins - list of plugins
      Since:
      1.0.1
    • setTypeAliasesPackage

      public void setTypeAliasesPackage(String typeAliasesPackage)
      Packages to search for type aliases.

      Since 2.0.1, allow to specify a wildcard such as com.example.*.model.

      Parameters:
      typeAliasesPackage - package to scan for domain objects
      Since:
      1.0.1
    • setTypeAliasesSuperType

      public void setTypeAliasesSuperType(Class<?> typeAliasesSuperType)
      Super class which domain objects have to extend to have a type alias created. No effect if there is no package to scan configured.
      Parameters:
      typeAliasesSuperType - super class for domain objects
      Since:
      1.1.2
    • setTypeHandlersPackage

      public void setTypeHandlersPackage(String typeHandlersPackage)
      Packages to search for type handlers.

      Since 2.0.1, allow to specify a wildcard such as com.example.*.typehandler.

      Parameters:
      typeHandlersPackage - package to scan for type handlers
      Since:
      1.0.1
    • setTypeHandlers

      public void setTypeHandlers(org.apache.ibatis.type.TypeHandler<?>... typeHandlers)
      Set type handlers. They must be annotated with MappedTypes and optionally with MappedJdbcTypes
      Parameters:
      typeHandlers - Type handler list
      Since:
      1.0.1
    • setDefaultEnumTypeHandler

      public void setDefaultEnumTypeHandler(Class<? extends org.apache.ibatis.type.TypeHandler> defaultEnumTypeHandler)
      Set the default type handler class for enum.
      Parameters:
      defaultEnumTypeHandler - The default type handler class for enum
      Since:
      2.0.5
    • setTypeAliases

      public void setTypeAliases(Class<?>... typeAliases)
      List of type aliases to register. They can be annotated with Alias
      Parameters:
      typeAliases - Type aliases list
      Since:
      1.0.1
    • setFailFast

      public void setFailFast(boolean failFast)
      If true, a final check is done on Configuration to assure that all mapped statements are fully loaded and there is no one still pending to resolve includes. Defaults to false.
      Parameters:
      failFast - enable failFast
      Since:
      1.0.1
    • 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".
      Parameters:
      configLocation - a location the MyBatis config file
    • setConfiguration

      public void setConfiguration(org.apache.ibatis.session.Configuration configuration)
      Set a customized MyBatis configuration.
      Parameters:
      configuration - MyBatis configuration
      Since:
      1.3.0
    • 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".

      Parameters:
      mapperLocations - location of MyBatis mapper files
    • setConfigurationProperties

      public void setConfigurationProperties(Properties sqlSessionFactoryProperties)
      Set optional properties to be passed into the SqlSession configuration, as alternative to a &lt;properties&gt; tag in the configuration xml file. This will be used to resolve placeholders in the config file.
      Parameters:
      sqlSessionFactoryProperties - optional properties for the SqlSessionFactory
    • 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.

      Parameters:
      dataSource - a JDBC DataSource
    • 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.

      Parameters:
      sqlSessionFactoryBuilder - a SqlSessionFactoryBuilder
    • setTransactionFactory

      public void setTransactionFactory(org.apache.ibatis.transaction.TransactionFactory transactionFactory)
      Set the MyBatis TransactionFactory 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.

      Parameters:
      transactionFactory - the MyBatis TransactionFactory
      See Also:
    • 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
    • setScriptingLanguageDrivers

      public void setScriptingLanguageDrivers(org.apache.ibatis.scripting.LanguageDriver... scriptingLanguageDrivers)
      Set scripting language drivers.
      Parameters:
      scriptingLanguageDrivers - scripting language drivers
      Since:
      2.0.2
    • setDefaultScriptingLanguageDriver

      public void setDefaultScriptingLanguageDriver(Class<? extends org.apache.ibatis.scripting.LanguageDriver> defaultScriptingLanguageDriver)
      Set a default scripting language driver class.
      Parameters:
      defaultScriptingLanguageDriver - A default scripting language driver class
      Since:
      2.0.2
    • addMapperLocations

      public void addMapperLocations(org.springframework.core.io.Resource... mapperLocations)
      Add 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".

      Parameters:
      mapperLocations - location of MyBatis mapper files
      Since:
      3.0.2
      See Also:
    • addTypeHandlers

      public void addTypeHandlers(org.apache.ibatis.type.TypeHandler<?>... typeHandlers)
      Add type handlers.
      Parameters:
      typeHandlers - Type handler list
      Since:
      3.0.2
    • addScriptingLanguageDrivers

      public void addScriptingLanguageDrivers(org.apache.ibatis.scripting.LanguageDriver... scriptingLanguageDrivers)
      Add scripting language drivers.
      Parameters:
      scriptingLanguageDrivers - scripting language drivers
      Since:
      3.0.2
    • addPlugins

      public void addPlugins(org.apache.ibatis.plugin.Interceptor... plugins)
      Add Mybatis plugins.
      Parameters:
      plugins - list of plugins
      Since:
      3.0.2
    • addTypeAliases

      public void addTypeAliases(Class<?>... typeAliases)
      Add type aliases.
      Parameters:
      typeAliases - Type aliases list
      Since:
      3.0.2
    • 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 Exception
      Build a SqlSessionFactory instance.

      The default implementation uses the standard MyBatis XMLConfigBuilder API to build a SqlSessionFactory instance based on a Reader. Since 1.3.0, it can be specified a Configuration instance directly(without config file).

      Returns:
      SqlSessionFactory
      Throws:
      Exception - if configuration is failed
    • 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>
    • onApplicationEvent

      public void onApplicationEvent(org.springframework.context.event.ContextRefreshedEvent event)
      Specified by:
      onApplicationEvent in interface org.springframework.context.ApplicationListener<org.springframework.context.event.ContextRefreshedEvent>