Class MapperFactoryBean<T>

java.lang.Object
org.springframework.dao.support.DaoSupport
org.mybatis.spring.support.SqlSessionDaoSupport
cn.lm.mybatis.spring.mapper.MapperFactoryBean<T>
All Implemented Interfaces:
org.springframework.beans.factory.FactoryBean<T>, org.springframework.beans.factory.InitializingBean

public class MapperFactoryBean<T> extends org.mybatis.spring.support.SqlSessionDaoSupport implements org.springframework.beans.factory.FactoryBean<T>
BeanFactory that enables injection of MyBatis mapper interfaces. It can be set up with a SqlSessionFactory or a pre-configured SqlSessionTemplate.

Sample configuration:

 
 <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true">
 <property name="sqlSessionFactory" ref="sqlSessionFactory" />
 </bean>
 <p>
 <bean id="oneMapper" parent="baseMapper">
 <property name="mapperInterface" value="my.package.MyMapperInterface" />
 </bean>
 <p>
 <bean id="anotherMapper" parent="baseMapper">
 <property name="mapperInterface" value="my.package.MyAnotherMapperInterface" />
 </bean>
 
 

Note that this factory can only inject interfaces, not concrete classes.

Author:
Eduardo Macarron, liuzh
See Also:
  • SqlSessionTemplate
  • Field Summary

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

    logger

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

    OBJECT_TYPE_ATTRIBUTE
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    MapperFactoryBean(Class<T> mapperInterface)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Return the mapper interface of the MyBatis mapper
    boolean
    Return the flag for addition into MyBatis config.
    boolean
    void
    setAddToConfig(boolean addToConfig)
    If addToConfig is false the mapper will not be added to MyBatis.
    void
    设置通用 Mapper 配置
    void
    setMapperInterface(Class<T> mapperInterface)
    Sets the mapper interface of the MyBatis mapper

    Methods inherited from class org.mybatis.spring.support.SqlSessionDaoSupport

    createSqlSessionTemplate, getSqlSession, getSqlSessionFactory, getSqlSessionTemplate, setSqlSessionFactory, setSqlSessionTemplate

    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

    • MapperFactoryBean

      public MapperFactoryBean()
    • MapperFactoryBean

      public MapperFactoryBean(Class<T> mapperInterface)
  • Method Details

    • checkDaoConfig

      protected void checkDaoConfig()
      Overrides:
      checkDaoConfig in class org.mybatis.spring.support.SqlSessionDaoSupport
    • getMapperInterface

      public Class<T> getMapperInterface()
      Return the mapper interface of the MyBatis mapper
      Returns:
      class of the interface
    • setMapperInterface

      public void setMapperInterface(Class<T> mapperInterface)
      Sets the mapper interface of the MyBatis mapper
      Parameters:
      mapperInterface - class of the interface
    • getObject

      public T getObject() throws Exception
      Specified by:
      getObject in interface org.springframework.beans.factory.FactoryBean<T>
      Throws:
      Exception
    • getObjectType

      public Class<T> getObjectType()
      Specified by:
      getObjectType in interface org.springframework.beans.factory.FactoryBean<T>
    • isAddToConfig

      public boolean isAddToConfig()
      Return the flag for addition into MyBatis config.
      Returns:
      true if the mapper will be added to MyBatis in the case it is not already registered.
    • setAddToConfig

      public void setAddToConfig(boolean addToConfig)
      If addToConfig is false the mapper will not be added to MyBatis. This means it must have been included in mybatis-config.xml.

      If it is true, the mapper will be added to MyBatis in the case it is not already registered.

      By default addToCofig is true.

      Parameters:
      addToConfig -
    • setMapperHelper

      public void setMapperHelper(MapperHelper mapperHelper)
      设置通用 Mapper 配置
      Parameters:
      mapperHelper -
    • isSingleton

      public boolean isSingleton()
      Specified by:
      isSingleton in interface org.springframework.beans.factory.FactoryBean<T>