[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 ...
随机推荐
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem A - B
Array of integers is unimodal, if: it is strictly increasing in the beginning; after that it is cons ...
- python --- 08 文件操作
一. 文件 f = open(文件路径,mode = '模式',encoding = '编码格式') 1.基础 ① 读写时,主要看光标的位置 ②操作完成要写 f.close( ) f.flu ...
- Python3 tkinter基础 Canvas bind 鼠标左键点击时,在当前位置画椭圆形
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Python3基础 dict setdefault 根据键查找值,找不到键会添加
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- JPush Flutter Plugin(Futter推送-极光推送)
https://pub.flutter-io.cn/packages/jpush_flutter JPush's officially supported Flutter plugin (Androi ...
- 解决/var/log下没有messages文件的问题?
fedora23和centos7+ 都是使用的 systemd 来代替sysv 管理系统启动和服务了. 在systemd 中主要包含两个方面的内容, 当打开/etc/inittab 文件时, 会看到: ...
- What are the differences between Flyweight and Object Pool patterns?
What are the differences between Flyweight and Object Pool patterns? They differ in the way they are ...
- python 之 文件I/0
打开和关闭文件 open()函数 必须要open()内置函数打开一个文件,创建一个file对象,相关的方法才可以调用它进行读写. 语法 file object=open(file_name [,acc ...
- NOIP队内凉心互测总结(8.22update)
8.22(结束后一天) __stdcall讲题qwq 全是CF原题 D1T1 一看像是结论题,打了下表,水过 没错就是结论题,直接暴力就好 D1T2 看起来不好做,没有AC思路 打了暴力 40分 T2 ...
- spring注解预览
从Java5.0开始,Java开始支持注解.Spring做为Java生态中的领军框架,从2.5版本后也开始支持注解.相比起之前使用xml来配置Spring框架,使用注解提供了更多的控制Spring框架 ...