Mybatis 创建Configuration对象
Mybatis 创建Configuration对象是在项目启动时就创建了。 具体创建流程为:
https://blog.csdn.net/wolfcode_cn/article/details/80747923
MyBatis的本质是java对数据库的操作。
SqlSessonFactory相当于一个数据库连接池,SqlSession相当于一个数库连接。
Mapper是一个接口,它由SqlSession所创建。
Mybatis-config.xml: Mybatis配置文件
RoleMapper.java 映射器接口
RoleMapper.xml 映射器XML文件,描述映射关系,SQL等内容。
1:SpringBoot项目中使用默认配置,先读取application.yml中关于Mybatis的配置文件,找到MyBatis-config.xml文件和Mapper.xml文件的位置
mybatis:
mapper-locations: classpath:mybatis/mapper/*.xml #Mapper所在的配置文件路径,进行扫描
config-location: classpath:mybatis/mybatis-config.xml # mybaits-config文件
Mybatis的运行过程分为两大步:
一:读取配置文件缓存到Configuration对象,用于创建SqlSessionFactory。
二:SqlSession的执行过程。
一:构建SqlSessionFactory过程。
1: 通过 org.apache.ibatis.builder.xml.XMLConfigBuilder 解析配置的XML文件,读取配置信息存入 org.apache.ibatis.session.Configuration 类对象中。
2:使用 Configuration 对象去创建 org.apache.ibatis.session.SqlSessionFactory 。 一般创建默认的实现类 DefaultSqlSessionFactory,通过Builder模式创建, Configuration作为统领,一步一步去创建得到Configuration对象。
二:SqlSession的运行过程
1:有了SqlSessionFactory很容易得到SqlSession ,SqlSession是一个接口,给出了增删改查方法。
public interface SqlSession extends Closeable {
/**
* Retrieve a single row mapped from the statement key
* @param <T> the returned object type
* @param statement
* @return Mapped object
*/
<T> T selectOne(String statement);
}:
2:通过SqlSession创建一个Mapper代理对象,一般在service层使用
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//另一种方法
@AutoWired
UserMapper userMapper;
将会调用
public class DefaultSqlSession implements SqlSession {
@Override
public <T> T getMapper(Class<T> type) {
return configuration.<T>getMapper(type, this);
}
2.1:使用到了configuration的getMapper方法
public class Configuration {
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
return mapperRegistry.getMapper(type, sqlSession);
}
}
2.2:使用mapperRegistry的getMapper的方法
public class MapperRegistry {
@SuppressWarnings("unchecked")
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
final MapperProxyFactory<T> mapperProxyFactory = (MapperProxyFactory<T>) knownMappers.get(type);
if (mapperProxyFactory == null) {
throw new BindingException("Type " + type + " is not known to the MapperRegistry.");
}
try {
return mapperProxyFactory.newInstance(sqlSession);
} catch (Exception e) {
throw new BindingException("Error getting mapper instance. Cause: " + e, e);
}
}
}
2.3:使用mapperProxyFactory.newInstance方法,生成Mapper代理对象。
public class MapperProxyFactory<T> {
public T newInstance(SqlSession sqlSession) {
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
}
Mybatis 创建Configuration对象的更多相关文章
- 5 -- Hibernate的基本用法 --4 1 创建Configuration对象
org.hibernate.cfg.Configuration实例代表了应用程序到SQL数据库的配置信息,Configuration对象提供了一个buildSessionFactory()方法,该方法 ...
- Configuration对象
Configuration对象 Hibernate的持久化操作离不开SessionFactory对象,使用该对象的openSession()方法可以打开Session对象.而SessionFactor ...
- Mybatis源码解析,一步一步从浅入深(四):将configuration.xml的解析到Configuration对象实例
在Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码中我们看到了XMLConfigBuilder(xml配置解析器)的实例化.而且这个实例化过程在文章:Mybatis源码解析,一步一步从浅 ...
- 无法为请求的 Configuration 对象创建配置文件 错误原因
Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); 无法为请求的 Configura ...
- mybatis源码解读(二)——构建Configuration对象
Configuration 对象保存了所有mybatis的配置信息,主要包括: ①. mybatis-configuration.xml 基础配置文件 ②. mapper.xml 映射器配置文件 1. ...
- C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。
应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...
- 阶段3 1.Mybatis_03.自定义Mybatis框架_2.自定义Mybatis的分析-创建代理对象的分析
如何创建代理对象,以及使用设计模式带来的优势 调用的组合关系 不关注的,执行JDBC那一套.第二个是解析XML,解析的技术有很多.
- Mybatis-学习笔记(1)SqlSessionFactory、SqlSession、Mybatis配置文件configuration的属性标签
1.mybatis引入项目,只需要引入mybatis-x.x.x.jar包即可. (当然数据库驱动的引入必不可少) 2.SqlSessionFactory 由SqlSessionFactoryBuil ...
- WCF初探-13:WCF客户端为双工服务创建回调对象
前言: 在WCF初探-5:WCF消息交换模式之双工通讯(Duplex)博文中,我讲解了双工通信服务的一个应用场景,即订阅和发布模式,这一篇,我将通过一个消息发送的例子讲解一下WCF客户端如何为双工服务 ...
随机推荐
- 你真的了解HTML吗?–雅虎面试题
http://helloweb.wang/jingyan~jiqiao/589.html
- 手机web不同屏幕字体大小高度自适应
body{font-size:0.6rem:} <script> //使用rem策略,不断更新html的fontsize (function(){ function sizeHtm ...
- Effective C++ Item 34 Differentiate between inheritance of interface and inheritance of implementation
1. 成员函数的接口总是被继承. 如 Item32 所说, public 意味着 is-a, 所以对 base class 为真的任何事情对 derived class 也为真 2. 声明一个 pur ...
- ajax的原理及实现方式
Ajax:Asynchronous javascript and xml,实现了客户端与服务器进行数据交流过程同时是异步发送请求.使用技术的好处是:不用页面刷新,并且在等待页面传输数据的同时可以进行其 ...
- Java Integer常量池
public class IntegerExample { public static void main(String[] javalatte) { Integer i = 10; Integer ...
- redis基础----->redis的基本使用(一)
这里我们就在虚拟机中安装redis,并且使用java和python实现简单的操作.深情是我承担不起的重担,情话只是偶尔兑现的谎言. redis的使用 下载地址:https://redis.io/.安装 ...
- 由JS函数返回值引发的一场”血案"
---恢复内容开始--- 啊... 本来昨天晚上想写来着,结果陪老婆看电视剧就忘了... 呢滴神啊,原谅我吧. 背景:昨天在项目中做一个小功能的时候,出现了个小问题,而且一开始找了半天也没找到原因. ...
- 【BZOJ3037/2068】创世纪/[Poi2004]SZP 树形DP
[BZOJ3037]创世纪 Description applepi手里有一本书<创世纪>,里面记录了这样一个故事……上帝手中有着N 种被称作“世界元素”的东西,现在他要把它们中的一部分投放 ...
- 【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay
[BZOJ1269][AHOI2006]文本编辑器editor Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目 ...
- c# SQL Server数据库操作-数据适配器类:SqlDataAdapter
SqlDataAdapter类主要在MSSQL与DataSet之间执行数据传输工具,本节将介绍如何使用SqlDataAdapter类来填充DataSet和MSSQL执行新增.修改..删除等操作. 功能 ...