程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-06(1)

MyBatis系列第八篇:MyBatis工作原理

发布于2021-05-29 19:57     阅读(1026)     评论(0)     点赞(29)     收藏(5)


一、Mybatis工作原理

Mybatis分层框架图在这里插入图片描述

Mybatis工作原理图
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

源码分析:一般都是从helloworld入手

1、根据xml配置文件(全局配置文件mybatis-config.xml)创建一个SqlsessionFactory对象,mybatis-config.xml有数据源一些环境信息
2、sql映射文件EmployeeMapper.xml配置了每一个sql,以及sql的封装规则等。
3、将sql映射文件注册在全局配置文件中
4、写代码:
4.1.根据全局配置文件得到sqlsessionFactory
4.2.使用SqlSession工程进行crud、sqlseesion就代表和数据库进行会话,用完close
4.3.使用sql标识告知mybatis来执行哪个sql,sql都是保存在sql映射文件中

测试类SqlSessionFactoryBuilder处打断点

    /**
     * 1、根据xml配置文件(全局配置文件mybatis-config.xml)创建一个SqlsessionFactory对象,mybatis-config.xml有数据源一些环境信息
     * 2、sql映射文件EmployeeMapper.xml配置了每一个sql,以及sql的封装规则等。
     * 3、将sql映射文件注册在全局配置文件中
     * 4、写代码:
     * 4.1.根据全局配置文件得到sqlsessionFactory
     * 4.2.使用SqlSession工程进行crud,sqlseesion就代表和数据库进行会话,用完close
     * 4.3.使用sql标识告知mybatis来执行哪个sql,sql都是保存在sql映射文件中
     *
     * @throws IOException
     */
    @Test
    public void test() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2、获取SqlSession实例,能直接执行已经映射了的sql语句,selectOne:sql唯一标识,执行sql要用到的参数
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            Employee employee = openSession.selectOne("com.ming.dao.EmployeeMapper.getEmpByID", 1);
            System.out.println(employee);
        } finally {
            openSession.close();
        }
    }

1、获取SqlsessionFactory对象

XPathParser作用:用dom解析mybatis-config.xml标签的configuration标签

  public Configuration parse() {
    if (parsed) {
      throw new BuilderException("Each XMLConfigBuilder can only be used once.");
    }
    parsed = true;
    parseConfiguration(parser.evalNode("/configuration"));
    return configuration;
  }

在这里插入图片描述

一个MappedStatement对象代表一个增删改查标签的详细信息(id sqlResource等)

在这里插入图片描述
全局configuation的一个重要属性MappedStatement
在这里插入图片描述
KonwnMappers生成一个Mapper接口的代理工厂
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

总结:
第一步:根据mybatis-config.xml全局配置文件创建SqlSessionFactory对象、就是把配置文件的详细信息解析保存在了configuration对象中,返回包含了configuration的defaultSqsessionFactory对象

注意:mappedSatement对象代表一个增删改查的详细标签

2、获取sqlsession对象
mybatis-openSession
在这里插入图片描述总结:返回sqlsession的实现类defaultSqlsession对象,defaultSqlsession对象包含了executor和configuration,Executor(四大对象)对象会在这一步被创建

3、获取Mapper接口代理对象(MapperProxy)
在这里插入图片描述

返回getMapper接口的代理对象、包含了SqlSession对象
在这里插入图片描述

4、执行增删改查方法
查询流程

    @Test
    public void testInterface() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
            Employee employee = employeeMapper.getEmpByID(1);
            System.out.println(employee);
            Employee employee2 = employeeMapper.getEmpByID(5);
            System.out.println(employee2);
            System.out.println(employee==employee2);

        }finally {
            sqlSession.close();
        }
    }

在这里插入图片描述
在这里插入图片描述

二、Mybatis运行原理总结

1、根据配置文件(全局、SQL映射文件)初始化出configuration对象
2、创建一个defaultSqlSession对象,它里面包含configuration和executor(根据配置文件中的defaultEXecutorType创建出对应的Executor)

3、defaultSqlSession.getMapper()获取Mapper接口对应的MapperProxy
4、MapperProxy里面有defaultSqlSession

5、执行增删改查方法:
1、调用的是defaultSqlsesion的增删改查(会调用Executor的crud)
2、会创建一个statementhandler对象(同时也会创建出parameterHandler和resultSetHandler)
3、调用StatementHandler的prepareStatement()方法进行预编译handler.prepare()和参数设置handler.parameterize(stmt)
4、设置完成后调用StatementHandler的增删改查方法query()
5、参数预编译完成后使用resultSetHandler封装结果集

注意:四大对象每个创建的时候都有一个interceptorChain.pluginAll()方法
例如StatementHandler 对象的创建

 StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
  public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, 	RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
    StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);
    statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);
    return statementHandler;
  }

原文链接:https://blog.csdn.net/qq_29445811/article/details/117286842



所属网站分类: 技术文章 > 博客

作者:码神

链接:http://www.javaheidong.com/blog/article/207158/ad4ba2f440c719c7bf6e/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

29 0
收藏该文
已收藏

评论内容:(最多支持255个字符)