[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>
[mybatis-spring]sqlSessionFactoryBean的更多相关文章
- Mybatis异常:java.lang.ClassNotFoundException: org.mybatis.spring.SqlSessionFactoryBean
问题描述: 一月 15, 2014 3:43:13 下午 org.springframework.context.support.AbstractApplicationContext prepareR ...
- MyBatis Spring SqlSessionFactoryBean 配置
在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建.而在 MyBatis-Spring 中,则使用 SqlSessionFactory ...
- 报错org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.mybatis.spring.SqlSessionFactoryBean]
超级大坑 org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.mybati ...
- [DEBUG]-[org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:431)] 一直在创建sqlsession工厂原因
今天在做开发项目的时候出现一直在控台输出创建sqlsessionfactory'这个代码, tomcat一直在控制台上输出这个内容无法停止下来,那么到底是什么原因呢, 我们可以在输出信息上看到有个wa ...
- spring3.0+mybatis+spring快速入门
一.首先奉上项目目录结构: 说明: dao,mapping,model包下的所有内容可以使用Generator工具自助生成. 具体用法,可以网上学习一下,比较简单,主要做以下工作: 1.提供相关的数据 ...
- springMVC+mybatis+spring整合案例
1.web.xml a:配置spring监听,使web容器在启动时加载spring的applicationContext.xml <listener> <listener-class ...
- SpringMVC+Mybatis+Spring整合
Maven引入需要的JAR包 pom.xml <properties> <!-- spring版本号 --> <spring.version>4.0.2.RELEA ...
- MyBatis学习(一)、MyBatis简介与配置MyBatis+Spring+MySql
一.MyBatis简介与配置MyBatis+Spring+MySql 1.1MyBatis简介 MyBatis 是一个可以自定义SQL.存储过程和高级映射的持久层框架.MyBatis 摒除了大部分的J ...
- myBatis,Spring,SpringMVC三大框架ssm整合模板
整合步骤 创建web工程 导入整合所需的所有jar包 编写各层需要的配置文件 1) mybatis的全局配置文件 <configuration> <!-- 批量别名的设置 -- ...
- 多个mapper location时, mybatis spring的自动扫描配置
1. MapperScannerConfigurer 里面的basePackage, 多个package用逗号分隔 2. SqlSessionFactoryBean里面的mapperLocations ...
随机推荐
- 热扩容LVM形式的/(根)分区(无损增大、缩小LVM分区)
警告! 本文为虚拟机环境,生产环境请务必在操作前优先备份重要数据! 再有,请确保所需扩充的分区为非进程占用分区 实验背景:当时规划系统分区时/(根)目录分配过小 实验目的 : 无损增大/(根)分区容量 ...
- JVM内存结构之堆、栈、方法区以及直接内存、堆和栈区别
JVM内存结构之堆.栈.方法区以及直接内存.堆和栈区别 一. 理解JVM中堆与栈以及方法区 堆(heap):FIFO(队列优先,先进先出):二级缓存:*JVM中只有一个堆区被所有线程所共享:对象和数 ...
- Spring 学习——Spring IOC概念
Spring IOC 接口及面向接口编程 接口 定义及理解:接口是一个类的抽象声明,用于由内部操作分离出外部沟通的方式,使其内部进行修改而不影响其外部连接沟通的一种交互方式.不对外公开逻辑处理,只是返 ...
- ODAC(V9.5.15) 学习笔记(四)TCustomDADataSet(1)
1.SQL相关 名称 类型 说明 BaseSQL String 没有被AddWhere.SetOrderBy.FilterSQL等方法处理过的原始SQL语句 FinalSQL String 被AddW ...
- Vim 学习
主要分为三种模式: 一般模式 编辑模式 命令行模式 光标的移动 单词级 比单纯的逐个字符的移动,效率要高 w or W 向移动到下一单词开头 ★★ b or B 向左移动到单词开头 ★★ 块级 gg文 ...
- tp剩余未验证内容-3
为什么有时候会 出现 "上传文件保存错误"? public function save($file, $replace=true){ /* 移动文件 */ if (!move_up ...
- 为linux dns (bind named)服务器配置 单独的笔记
注意: 当在把 named.ca文件下载好13个根dns服务器的 全球记录后, 就不再需要别的 dns服务器来辅助获得了. 只要把所有 本地服务器 不能解析的请求, 都发送到 . 点根去就行了, 所以 ...
- Configuring Logstash
Configuring Logstash To configure Logstash, you create a config file that specifies which plugins yo ...
- BZOJ5018: [Snoi2017]英雄联盟
Description 正在上大学的小皮球热爱英雄联盟这款游戏,而且打的很菜,被网友们戏称为「小学生」.现在,小皮球终于受不 了网友们的嘲讽,决定变强了,他变强的方法就是:买皮肤!小皮球只会玩N个英雄 ...
- P2617 Dynamic Rankings(带修主席树)
所谓带修主席树,就是用树状数组的方法维护主席树的前缀和 思路 带修主席树的板子 注意数据范围显然要离散化即可 代码 #include <cstdio> #include <cstri ...