[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 ...
随机推荐
- python简说(十五)MD5加密
def my_md5(s): news = str(s).encode() m = hashlib.md5(news) return m.hexdigest()
- tar 压缩指令基本用法
压缩:tar -cjvf aaa.tar.bz2 www.test.com/ --exclude *.log(-j是用bz2压缩,-exclude是排除.log后缀的文件) c-创建 j-bzip ...
- 动态规划之91 decode ways
题目链接:https://leetcode-cn.com/problems/decode-ways/description/ 参考:https://www.jianshu.com/p/5a604070 ...
- C++进程间通信之共享内存
转载:http://blog.csdn.net/taily_duan/article/details/51692999 转载:http://blog.csdn.net/fengrx/article/d ...
- 【Python019--函数与过程】
一.函数与过程 1.Python只有函数没有过程 >>> def hello(): print('Hello fishC!')>>> temp = hell ...
- django基础 -- 4. 模板语言 过滤器 模板继承 FBV 和CBV 装饰器 组件
一.语法 两种特殊符号(语法): {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 二.变量 1. 可直接用 {{ 变量名 }} (可调用字符串, 数字 ,列表,字典,对象等) ...
- Hakase and Nano 【思维博弈】
Hakase and Nano 时间限制: 1 Sec 内存限制: 128 MB 提交: 400 解决: 104 [提交] [状态] [命题人:admin] 题目描述 Hakase and Nan ...
- 2199: [Usaco2011 Jan]奶牛议会 2-sat
链接 https://www.luogu.org/problemnew/show/P3007 https://www.lydsy.com/JudgeOnline/problem.php?id=2199 ...
- ZOJ 3593 One Person Game(ExGcd + 最优解)题解
思路:题意转化为求 (ax+by=dis) || (ax+cy=dis) || (bx+cy=dis) 三个式子有解时的最小|x| + |y|.显然求解特解x,y直接用扩展欧几里得,那么怎么求|x| ...
- 算法总结(转自CS-Notes)
转载地址: 注意要点: 1.希尔排序:实际是将元素按步距h分为几组,每组元素没有关系,是组里每个元素跨步距h得到的一组元素是有序的,那么剩下的问题就是组内有序,再处理好组间边界即可.实际解决的方式是不 ...