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. [转] CSocket 和CAsyncSocket类介绍

    微软的MFC把复杂的WinSock API函数封装到类里,这使得编写网络应用程序更容易. CAsyncSocket类逐个封装了WinSock API,为高级网络程序员提供了更加有力而灵活的方法.这个类 ...

  2. POJ 1337 A Lazy Worker(区间DP, 背包变形)

    Description There is a worker who may lack the motivation to perform at his peak level of efficiency ...

  3. VMware 12安装虚拟机Mac OS X 10.10(VMware12安装/共享文件夹)

    推荐电脑配置 1:Inter I5及以上 (A卡请自行百度大神解决方案) 必须开启CPU虚拟化:开机进入BIOS--->Intel Virtualization Technology---> ...

  4. Oracle-未能加载文件或程序集“oracle.dataaccess”或它的某一个依赖项。试图加载格式不正确的程序。

    未能加载文件或程序集“oracle.dataaccess”或它的某一个依赖项.试图加载格式不正确的程序. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以 ...

  5. Linux 启动文件、设置环境变量的位置

    系统级启动文件  ==================================== 1./etc/rc  主启动文件,不要修改它 2./etc/rc.conf  决定启动哪些系统自带的守护进程 ...

  6. ubuntu14.04 LTS 搜狗输入法安装和不能输入中文的解决方法

    搜狗输入法安装 1.首先通过Ubuntu软件中心,需要安装:fcitx https://pinyin.sogou.com/linux/help.php 2.然后再安装搜狗输入法包 https://pi ...

  7. Android长截屏-- ScrollView,ListView及RecyclerView截屏

    http://blog.csdn.net/wbwjx/article/details/46674157       Android长截屏-- ScrollView,ListView及RecyclerV ...

  8. gvim编辑器_vimrc文件

    set nocompatiblesource $VIMRUNTIME/vimrc_example.vimsource $VIMRUNTIME/mswin.vimbehave mswin set dif ...

  9. 二、微信小游戏开发 多线程Worker

    微信多线程Worker教程 微信多线程Worker API 一.创建Worker,并和当前线程通讯 多线程worker只能创建1个.能和当前线程互传数据. 创建worker 在微信开发者工具中,在当前 ...

  10. {sharepoint}It may have been deleted or renamed by another user

    Symptom Consider the following scenario: We In the xslt: <xsl:param name="CustomItem"&g ...