Package org.mybatis.spring
Class SqlSessionFactoryBean
- java.lang.Object
-
- org.mybatis.spring.SqlSessionFactoryBean
-
- All Implemented Interfaces:
java.util.EventListener,org.springframework.beans.factory.FactoryBean<org.apache.ibatis.session.SqlSessionFactory>,org.springframework.beans.factory.InitializingBean,org.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>
public class SqlSessionFactoryBean extends java.lang.Object implements org.springframework.beans.factory.FactoryBean<org.apache.ibatis.session.SqlSessionFactory>, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>FactoryBeanthat creates a MyBatisSqlSessionFactory. This is the usual way to set up a shared MyBatisSqlSessionFactoryin a Spring application context; the SqlSessionFactory can then be passed to MyBatis-based DAOs via dependency injection. EitherDataSourceTransactionManagerorJtaTransactionManagercan be used for transaction demarcation in combination with aSqlSessionFactory. 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
- See Also:
setConfigLocation(org.springframework.core.io.Resource),setDataSource(javax.sql.DataSource)
-
-
Constructor Summary
Constructors Constructor Description SqlSessionFactoryBean()
-
Method Summary
Modifier and Type Method Description voidafterPropertiesSet()protected org.apache.ibatis.session.SqlSessionFactorybuildSqlSessionFactory()Build aSqlSessionFactoryinstance.org.apache.ibatis.cache.CachegetCache()Gets the Cache.org.apache.ibatis.mapping.DatabaseIdProvidergetDatabaseIdProvider()Gets the DatabaseIdProviderorg.apache.ibatis.session.SqlSessionFactorygetObject()java.lang.Class<? extends org.apache.ibatis.session.SqlSessionFactory>getObjectType()java.lang.Class<? extends org.apache.ibatis.io.VFS>getVfs()Gets the VFS.booleanisSingleton()voidonApplicationEvent(org.springframework.context.ApplicationEvent event)voidsetCache(org.apache.ibatis.cache.Cache cache)Sets the Cache.voidsetConfigLocation(org.springframework.core.io.Resource configLocation)Set the location of the MyBatisSqlSessionFactoryconfig file.voidsetConfiguration(org.apache.ibatis.session.Configuration configuration)Set a customized MyBatis configuration.voidsetConfigurationProperties(java.util.Properties sqlSessionFactoryProperties)Set optional properties to be passed into the SqlSession configuration, as alternative to a<properties>tag in the configuration xml file.voidsetDatabaseIdProvider(org.apache.ibatis.mapping.DatabaseIdProvider databaseIdProvider)Sets the DatabaseIdProvider.voidsetDataSource(javax.sql.DataSource dataSource)Set the JDBCDataSourcethat this instance should manage transactions for.voidsetDefaultScriptingLanguageDriver(java.lang.Class<? extends org.apache.ibatis.scripting.LanguageDriver> defaultScriptingLanguageDriver)Set a default scripting language driver class.voidsetEnvironment(java.lang.String environment)NOTE: This class overrides anyEnvironmentyou have set in the MyBatis config file.voidsetFailFast(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.voidsetMapperLocations(org.springframework.core.io.Resource... mapperLocations)Set locations of MyBatis mapper files that are going to be merged into theSqlSessionFactoryconfiguration at runtime.voidsetObjectFactory(org.apache.ibatis.reflection.factory.ObjectFactory objectFactory)Sets the ObjectFactory.voidsetObjectWrapperFactory(org.apache.ibatis.reflection.wrapper.ObjectWrapperFactory objectWrapperFactory)Sets the ObjectWrapperFactory.voidsetPlugins(org.apache.ibatis.plugin.Interceptor... plugins)Mybatis plugin list.voidsetScriptingLanguageDrivers(org.apache.ibatis.scripting.LanguageDriver... scriptingLanguageDrivers)Set scripting language drivers.voidsetSqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder sqlSessionFactoryBuilder)Sets theSqlSessionFactoryBuilderto use when creating theSqlSessionFactory.voidsetTransactionFactory(org.apache.ibatis.transaction.TransactionFactory transactionFactory)Set the MyBatis TransactionFactory to use.voidsetTypeAliases(java.lang.Class<?>... typeAliases)List of type aliases to register.voidsetTypeAliasesPackage(java.lang.String typeAliasesPackage)Packages to search for type aliases.voidsetTypeAliasesSuperType(java.lang.Class<?> typeAliasesSuperType)Super class which domain objects have to extend to have a type alias created.voidsetTypeHandlers(org.apache.ibatis.type.TypeHandler<?>... typeHandlers)Set type handlers.voidsetTypeHandlersPackage(java.lang.String typeHandlersPackage)Packages to search for type handlers.voidsetVfs(java.lang.Class<? extends org.apache.ibatis.io.VFS> vfs)Sets the VFS.
-
-
-
Method Detail
-
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 java.lang.Class<? extends org.apache.ibatis.io.VFS> getVfs()
Gets the VFS.- Returns:
- a specified VFS
-
setVfs
public void setVfs(java.lang.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(java.lang.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(java.lang.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(java.lang.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 withMappedTypesand optionally withMappedJdbcTypes- Parameters:
typeHandlers- Type handler list- Since:
- 1.0.1
-
setTypeAliases
public void setTypeAliases(java.lang.Class<?>... typeAliases)
List of type aliases to register. They can be annotated withAlias- 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 MyBatisSqlSessionFactoryconfig 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 theSqlSessionFactoryconfiguration 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(java.util.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.- Parameters:
sqlSessionFactoryProperties- optional properties for the SqlSessionFactory
-
setDataSource
public void setDataSource(javax.sql.DataSource dataSource)
Set the JDBCDataSourcethat this instance should manage transactions for. TheDataSourceshould match the one used by theSqlSessionFactory: for example, you could specify the same JNDI DataSource for both. A transactional JDBCConnectionfor thisDataSourcewill be provided to application code accessing thisDataSourcedirectly viaDataSourceUtilsorDataSourceTransactionManager. TheDataSourcespecified here should be the targetDataSourceto manage transactions for, not aTransactionAwareDataSourceProxy. Only data access code may work withTransactionAwareDataSourceProxy, while the transaction manager needs to work on the underlying targetDataSource. If there's nevertheless aTransactionAwareDataSourceProxypassed in, it will be unwrapped to extract its targetDataSource.- Parameters:
dataSource- a JDBCDataSource
-
setSqlSessionFactoryBuilder
public void setSqlSessionFactoryBuilder(org.apache.ibatis.session.SqlSessionFactoryBuilder sqlSessionFactoryBuilder)
Sets theSqlSessionFactoryBuilderto use when creating theSqlSessionFactory. This is mainly meant for testing so that mock SqlSessionFactory classes can be injected. By default,SqlSessionFactoryBuildercreatesDefaultSqlSessionFactoryinstances.- Parameters:
sqlSessionFactoryBuilder- a SqlSessionFactoryBuilder
-
setTransactionFactory
public void setTransactionFactory(org.apache.ibatis.transaction.TransactionFactory transactionFactory)
Set the MyBatis TransactionFactory to use. Default isSpringManagedTransactionFactoryThe defaultSpringManagedTransactionFactoryshould 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 defaultTransactionFactory. 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:
SpringManagedTransactionFactory
-
setEnvironment
public void setEnvironment(java.lang.String environment)
NOTE: This class overrides anyEnvironmentyou have set in the MyBatis config file. This is used only as a placeholder name. The default value isSqlSessionFactoryBean.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(java.lang.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
-
afterPropertiesSet
public void afterPropertiesSet() throws java.lang.Exception- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
java.lang.Exception
-
buildSqlSessionFactory
protected org.apache.ibatis.session.SqlSessionFactory buildSqlSessionFactory() throws java.lang.ExceptionBuild aSqlSessionFactoryinstance. The default implementation uses the standard MyBatisXMLConfigBuilderAPI to build aSqlSessionFactoryinstance based on an Reader. Since 1.3.0, it can be specified aConfigurationinstance directly(without config file).- Returns:
- SqlSessionFactory
- Throws:
java.lang.Exception- if configuration is failed
-
getObject
public org.apache.ibatis.session.SqlSessionFactory getObject() throws java.lang.Exception- Specified by:
getObjectin interfaceorg.springframework.beans.factory.FactoryBean<org.apache.ibatis.session.SqlSessionFactory>- Throws:
java.lang.Exception
-
getObjectType
public java.lang.Class<? extends org.apache.ibatis.session.SqlSessionFactory> getObjectType()
- Specified by:
getObjectTypein interfaceorg.springframework.beans.factory.FactoryBean<org.apache.ibatis.session.SqlSessionFactory>
-
isSingleton
public boolean isSingleton()
- Specified by:
isSingletonin interfaceorg.springframework.beans.factory.FactoryBean<org.apache.ibatis.session.SqlSessionFactory>
-
onApplicationEvent
public void onApplicationEvent(org.springframework.context.ApplicationEvent event)
- Specified by:
onApplicationEventin interfaceorg.springframework.context.ApplicationListener<org.springframework.context.ApplicationEvent>
-
-