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>
    宛如智障,暗藏锋芒
  • 相关阅读:
    ES 2016+
    git-svn for mac
    Linux系统下安装rz/sz命令及使用说明
    Nginx location指令匹配顺序规则
    nginx修改配置后不生效的问题
    ssh and scp从远程服务器下载文件
    安装yii2 框架遇到的问题
    php添加openssl扩展
    GitHub Token for composer
    centos7+nginx+php+mysql环境搭建
  • 原文地址:https://www.cnblogs.com/zienzir/p/9109773.html
Copyright © 2011-2022 走看看