在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>

[mybatis-spring]sqlSessionFactoryBean的更多相关文章

  1. Mybatis异常:java.lang.ClassNotFoundException: org.mybatis.spring.SqlSessionFactoryBean

    问题描述: 一月 15, 2014 3:43:13 下午 org.springframework.context.support.AbstractApplicationContext prepareR ...

  2. MyBatis Spring SqlSessionFactoryBean 配置

    在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建.而在 MyBatis-Spring 中,则使用 SqlSessionFactory ...

  3. 报错org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.mybatis.spring.SqlSessionFactoryBean]

    超级大坑 org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.mybati ...

  4. [DEBUG]-[org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:431)] 一直在创建sqlsession工厂原因

    今天在做开发项目的时候出现一直在控台输出创建sqlsessionfactory'这个代码, tomcat一直在控制台上输出这个内容无法停止下来,那么到底是什么原因呢, 我们可以在输出信息上看到有个wa ...

  5. spring3.0+mybatis+spring快速入门

    一.首先奉上项目目录结构: 说明: dao,mapping,model包下的所有内容可以使用Generator工具自助生成. 具体用法,可以网上学习一下,比较简单,主要做以下工作: 1.提供相关的数据 ...

  6. springMVC+mybatis+spring整合案例

    1.web.xml a:配置spring监听,使web容器在启动时加载spring的applicationContext.xml <listener> <listener-class ...

  7. SpringMVC+Mybatis+Spring整合

    Maven引入需要的JAR包 pom.xml <properties> <!-- spring版本号 --> <spring.version>4.0.2.RELEA ...

  8. MyBatis学习(一)、MyBatis简介与配置MyBatis+Spring+MySql

    一.MyBatis简介与配置MyBatis+Spring+MySql 1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的J ...

  9. myBatis,Spring,SpringMVC三大框架ssm整合模板

    整合步骤 创建web工程 导入整合所需的所有jar包 编写各层需要的配置文件 1) mybatis的全局配置文件 <configuration>    <!-- 批量别名的设置 -- ...

  10. 多个mapper location时, mybatis spring的自动扫描配置

    1. MapperScannerConfigurer 里面的basePackage, 多个package用逗号分隔 2. SqlSessionFactoryBean里面的mapperLocations ...

随机推荐

  1. (13flask继续研究)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署

    解决3个问题: 1.自己实现一例flask项目: 2.在flask中,如何调用json传值: 3.进一步读懂现有代码. Flask 在整个系统中是作为一个后台框架,对外提供 api 服务,因此对它的理 ...

  2. uniGUI试用笔记(十)

    今天用LoadRunner对uniGUI的Standalone模式的程序进行了一次压力测试,程序采用三层模式,将应用服务器与Web服务器分离,由于条件限制,数据库.应用服务和Web服务都部署在同一条云 ...

  3. aria2的下载配置

    aria2的命令行命令是: aria2c 一种方式: aria2c "http://host/file.zip" 如同wget 第二种方式: rpc server方式:

  4. 【原理、注意点】Quartz的原理和需要注意的地方

    基本介绍和核心接口 1.quartz是完全基于java的可用于进行定时任务调度的开源框架,使用的时候需要引入: <dependency> <groupId>org.quartz ...

  5. oracle 之 定时任务,存储过程和游标等语法案例

    --定时任务 declare job20 number; begin sys.dbms_job.submit(job20,'test1;',sysdate,'sysdate+1'); end; --存 ...

  6. arm中断体系结构

     ARM处理器中有7种类型的异常,按优先级从高到低的排列如下:                   复位异常(Reset).     数据异常(Data Abort).     快速中断异常(FIQ) ...

  7. 【AI】微软人工智能学习笔记(一)

    数据分析平台 01|数据平台概况图示 上面图中所示就是微软人工智能数据平台的相关的技术. 02.1| Cortana Intelligence Suite 从上面图中可以看到, 其中有一个Cortan ...

  8. ZooKeeper分布式过程协同技术详解2——了解ZooKeeper

    这个服务如何实现这些协作方面的原语? ZooKeeper基础

  9. Git误操作 git reset强制回滚 恢复commit方法

    参考: 找回Git中丢失的Commit Git误操作 git reset强制回滚 恢复commit方法 使用Git时,常有误操作,在Commit之后又执行了git reset --hard HEAD强 ...

  10. js操作css变量

    原文:http://css-live.ru/articles/dostup-k-css-peremennym-i-ix-izmenenie-spomoshhyu-javascript.html :ro ...