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对象的更多相关文章

  1. 5 -- Hibernate的基本用法 --4 1 创建Configuration对象

    org.hibernate.cfg.Configuration实例代表了应用程序到SQL数据库的配置信息,Configuration对象提供了一个buildSessionFactory()方法,该方法 ...

  2. Configuration对象

    Configuration对象 Hibernate的持久化操作离不开SessionFactory对象,使用该对象的openSession()方法可以打开Session对象.而SessionFactor ...

  3. Mybatis源码解析,一步一步从浅入深(四):将configuration.xml的解析到Configuration对象实例

    在Mybatis源码解析,一步一步从浅入深(二):按步骤解析源码中我们看到了XMLConfigBuilder(xml配置解析器)的实例化.而且这个实例化过程在文章:Mybatis源码解析,一步一步从浅 ...

  4. 无法为请求的 Configuration 对象创建配置文件 错误原因

    Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); 无法为请求的 Configura ...

  5. mybatis源码解读(二)——构建Configuration对象

    Configuration 对象保存了所有mybatis的配置信息,主要包括: ①. mybatis-configuration.xml 基础配置文件 ②. mapper.xml 映射器配置文件 1. ...

  6. C#/ASP.NET应用程序配置文件app.config/web.config的增、删、改操作,无法为请求的 Configuration 对象创建配置文件。

    应用程序配置文件,对于asp.net是 web.config,对于WINFORM程序是 App.Config(ExeName.exe.config). 配置文件,对于程序本身来说,就是基础和依据,其本 ...

  7. 阶段3 1.Mybatis_03.自定义Mybatis框架_2.自定义Mybatis的分析-创建代理对象的分析

    如何创建代理对象,以及使用设计模式带来的优势 调用的组合关系 不关注的,执行JDBC那一套.第二个是解析XML,解析的技术有很多.

  8. Mybatis-学习笔记(1)SqlSessionFactory、SqlSession、Mybatis配置文件configuration的属性标签

    1.mybatis引入项目,只需要引入mybatis-x.x.x.jar包即可. (当然数据库驱动的引入必不可少) 2.SqlSessionFactory 由SqlSessionFactoryBuil ...

  9. WCF初探-13:WCF客户端为双工服务创建回调对象

    前言: 在WCF初探-5:WCF消息交换模式之双工通讯(Duplex)博文中,我讲解了双工通信服务的一个应用场景,即订阅和发布模式,这一篇,我将通过一个消息发送的例子讲解一下WCF客户端如何为双工服务 ...

随机推荐

  1. C#一个关于委托和事件通俗易懂的例子

    using System; namespace Test { public class 室友 { public delegate void 这是一个委托(); public void 起床晨跑去() ...

  2. MathType初级教程:怎么安装MathType

    MathType 由美国Design Science公司开发,是一款功能强大的数学公式编辑器,它同时支持Windows和Macintosh 两种操作系统,有很好的兼容性,能够在各种文档中加入复杂的数学 ...

  3. 记录下自己常用的全框架HTML代码

    纯粹记录下,没有任何意义. 也不推荐使用 <frameset rows="> <frame src=" name="topFrame" scr ...

  4. sql替换数据库字段中的字符

    UPDATE `table_name` SET `field_name` = replace (`field_name`,'from_str','to_str') WHERE ……说明:table_n ...

  5. 换个思路理解Javascript中的this

    https://segmentfault.com/a/1190000010328752

  6. [Scikit-learn] Dynamic Bayesian Network - Kalman Filter

    看上去不错的网站:http://iacs-courses.seas.harvard.edu/courses/am207/blog/lecture-18.html SciPy Cookbook:http ...

  7. MVC 5 视图之公用代码

    一.公共模板 1.@RenderBody() 在网站公用部分通过一个占位符@RenderBody()来为网站独立部分预留一个位置.然后私有页面顶部通过@{Layout="公用模板路径&quo ...

  8. SDUT -refresh的停车场(栈和队列)

    题目描写叙述  refresh近期发了一笔横財,开了一家停车场.因为土地有限,停车场内停车数量有限,可是要求进停车场的车辆过多. 当停车场满时,要进入的车辆会进入便道等待.最先进入便道的车辆会优先 进 ...

  9. 处理unsigned相减错误(BIGINT UNSIGNED value is out of range)

    mysql 当两个字段想减时,如果其中一个或两个字段的类型的unsigned无签名类型,如果想减的值小于0则会报错(BIGINT UNSIGNED value is out of range) 测试: ...

  10. Triton调试记录

    先编译Release版本 先从下拉列表选择Release-MT-DLL,然后选中Triton-vc14工程, 修改项目属性配置为Release-MT-DLL-NODX,NODX的意思是不使用Direc ...