zoukankan      html  css  js  c++  java
  • [mybatis-spring]sqlSessionFactoryBean

    在mybatis中,SqlSessionFactory由SqlSessionFactoryBuilder创建.

    在mybatis-spring中,是由SqlSessionFactoryBean创建的.

    1.创建

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
    </bean>

    注意SqlSessionFactoryBean实现了Spring的FactoryBean接口 (see section 3.8 of the Spring documentation).

    这意味着Spring最终创建的bean不是SqlSessionFactoryBean自身,

    而是SqlSessionFactoryBean的getObject()方法的返回结果.(看Java等价代码第二行)

    在这种情况下,Spring会在程序启动时为你创建一个SqlSessionFactory,并且将其以sqlSessionFactory 的名称存储.

    等价的Java代码如下:

    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    SqlSessionFactory sessionFactory = factoryBean.getObject();

    通常在mybatis-spring的使用中,不需要直接使用SqlSessionFactoryBean或者对应的SqlSessionFactory.

    Session factory会被注入到MapperFactoryBean或者其他继承SqlSessionDaoSupport的DAO.

    2.属性.

    唯一必须的一个属性:JDBC 数据源.

    常用的属性有configLocation,用来指定Mybatis的配置XML文件.

    只有当MyBatis配置需要更改时才需要使用.这时,通常还会选择<settings> or <typeAliases>

      注意:这个配置不需要是一个完整的mybatis配置. Specifically,任何environments,dataSourcec transactionManager都会被忽略.

    SqlSessionFactoryBean 使用设置的值来创建它自己的定制化mybatis environment.

    #另一个需要配置文件的原因是:#

    mybatis mapper XML 文件不是和mapper类 在同一个classpath 位置下.

    这样配置有两个可选的方法.

      第一个是手动指定XML文件的classpath,使用mybatis配置文件的<mappers> 选项.

      //之前的写法.

      第二个是使用factory bean的 mapperLocations 属性.

    The mapperLocations property takes a list of resource locations. 

    这个属性可以用来指定mybatis xml mapper 文件.

    value可以办好Ant-style pattern来加载某个目录下的单所有文件,

    or to recursively search all paths from a base location. 

    (或者递归地搜索基位置下的所有路径)

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="mapperLocations" value="classpath*:sample/config/mappers/**/*.xml" />
    </bean>

    这回加载sample.config.mappers 包和其子包下的所有xml格式的mybatis mapper文件.

    /*

    One property that may be required in an environment with container managed transactions is transactionFactoryClass .

    Please see the relevant section in the Transactions chapter.

    In case you are using the multi-db feature you will need to set the databaseIdProvider property:

    */

    mybatis-spring 1.3.0版本后,增加了configuration属性,
    可以将mybatis的配置文件省略,而将其配置内容放入该bean下的一个属性中

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="configuration">
        <bean class="org.apache.ibatis.session.Configuration">
          <property name="mapUnderscoreToCamelCase" value="true"/>
        </bean>
      </property>
    </bean>
    宛如智障,暗藏锋芒
  • 相关阅读:
    意外发现,VC断点可加在构造函数的左括号上
    C++中的INL
    如何用DELPHI编程修改外部EXE文件的版本信
    j2ee面试宝典翻译(1)
    华为总裁任正非:允许小部分力量去颠覆性创新
    QStringList与QString互转
    QTreeView只显示指定驱动器及其目录,隐藏所有兄弟节点
    Protected Functions 是理解OO的难点和关键
    技术人员的创业陷阱:我能,但不管用户在哪里!
    大陆的创业环境和风气的确产生巨大变化,大众创业“蔚然成风”
  • 原文地址:https://www.cnblogs.com/zienzir/p/9109773.html
Copyright © 2011-2022 走看看