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. asp.net线程批量导入数据时通过ajax获取执行状态

    最近因为工作中遇到一个需求,需要做了一个批量导入功能,但长时间运行没个反馈状态,很容易让人看了心急,产生各种臆想!为了解决心里障碍,写了这么个功能. 通过线程执行导入,并把正在执行的状态存入sessi ...

  2. UGUI 的多分辨率适配

    1.Canvas的属性配置 2.Canvas Scaler的属性配置 3.根据不同的屏幕的比例动态修改缩放基准 void Start () { float standard_width = 960f; ...

  3. 如何:为 IIS 7.0 配置 <system.webServer> 节

    https://technet.microsoft.com/zh-cn/sysinternals/bb763179.aspx https://www.cnblogs.com/tl2f/p/501615 ...

  4. UITextField 全属性

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...

  5. python2.0_s12_day10_rabbitMQ使用介绍

    RabbitMQ RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. MQ全称为Message Queue, 消息队列(M ...

  6. shell基础篇(五)条件判断

    写脚本时:有时要判断字符串是否相等,数字测试.这对后面学习的shell语句,循环,条件语句做好基础. 条件判断格式  1. test condition : test命令  2. [ conditio ...

  7. 安全日志:/var/log/secure

    /var/log/secure 一般用来记录安全相关的信息,记录最多的是哪些用户登录服务器的相关日志,如果该文件很大,说明有人在破解你的 root 密码 [root@localhost ~]$ tai ...

  8. mac 获取idea&&datagrip激活码

    mac 版本的修改如下: 1). Command+Shift+G 2). /private/etc/ 3). 找到hosts文件,用文集编辑器打开 4). 输入0.0.0.0 account.jetb ...

  9. 关于android定位的坐标系问题

    按照正常的思路,我们通过GPS或者基站定位等方式获取到经纬度信息后,把它放到地图上,就能够完成定位.但实际上,我们很有可能会在实际操作中发现,我们的定位出现了较大的偏移.这是因为我国出于国家安全(或者 ...

  10. 剑指offer练习

    1.题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数.  public c ...