mybatis 调用 oracle函数_MyBatis之启动分析(一)

本文主要分析了MyBatis启动过程,包括SqlSessionFactoryBuilder的使用,XMLConfigBuilder的实例化及解析,以及SqlSessionFactory和SqlSession的创建。内容涵盖环境配置、对象初始化、XML解析等方面,旨在理解MyBatis与Oracle函数交互的基础机制。
摘要由CSDN通过智能技术生成
e4fa8864f0338f01fac9dfd20fafa477.png 作者丨ytao  来源丨ytao(ytao-blog)

前言

MyBatis 作为目前最常用的持久层框架之一,分析其源码,对我们的使用过程中可更好的运用它。本系列基于 mybatis-3.4.6 进行分析。MyBatis 的初始化工作就是解析主配置文件,映射配置文件以及注解信息。然后保存在 org.apache.ibatis.session.Configuration ,供后期执行数据请求的相关调用。 Configuration 里有大量配置信息,在后面每涉及到一个相关配置,会进行详细的分析。

启动

publicstaticvoid main(String[] args) throwsIOException{// 获取配置文件Reader reader = Resources.getResourceAsReader("mybatis-config.xml");// 通过 SqlSessionFactoryBuilder 构建 sqlSession 工厂SqlSessionFactory sqlSessionFactory = newSqlSessionFactoryBuilder().build(reader);// 获取 sqlSession 实例SqlSession sqlSession = sqlSessionFactory.openSession(); reader.close(); sqlSession.close();}

分析

SqlSessionFactoryBuilder 类

SqlSessionFactoryBuilder 的 build() 是Mybatis启动的初始化入口,使用builder模式加载配置文件。通过查看该类,使用方法重载,有以下9个方法: 46e072a74fd1c802d70de7841001c6cf.png 方法重载最终实现处理的方法源码如下:
publicSqlSessionFactory build(Reader reader, String environment, Properties properties) {try{// 实例化 XMLConfigBuilder,用于读取配置文件信息XMLConfigBuilder parser = newXMLConfigBuilder(reader, environment, properties);// 解析配置信息,保存到 Configurationreturn build(parser.parse());} catch(Exception e) {throwExceptionFactory.wrapException("Error building SqlSession.", e);} finally{ErrorContext.instance().reset();try{ reader.close();} catch(IOException e) {// Intentionally ignore. Prefer previous error.}}}
  • environment 是指定加载环境,默认值为 null。
  • properties 是属性配置文件,默认值为 null。同时读取配置文件既可字符流读取,也支持字节流读取。
publicSqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {try{XMLConfigBuilder parser = newXMLConfigBuilder(inputStream, environment, properties);return build(parser.parse());} catch(Exception e) {throwExceptionFactory.wrapException("Error building SqlSession.", e);} finally{ErrorContext.instance().reset();try{ inputStream.close();} catch(IOException e) {// Intentionally ignore. Prefer previous error.}}}

实例化 XMLConfigBuilder 类

通过 SqlSessionFactoryBuilder 中 XMLConfigBuilderparser=newXMLConfigBuilder(reader,environment,properties) , 分析 XMLConfigBuilder实例化过程。该类中有四个变量:
privateboolean parsed;privatefinalXPathParser parser;privateString environment;privatefinalReflectorFactory localReflectorFactory = newDefaultReflectorFactory();
  • parsed 是否解析,一次解析即可。用于标志配置文件只解析一次, true为已解析过。
  • parser 解析配置的解析器
  • environment 加载环境,即 SqlSessionFactoryBuilder 中的 environment
  • localReflectorFactory 用于创建和缓存 Reflector对象,一个类对应一个 Reflector。因为参数处理、结果映射等操作时,会涉及大量的反射操作。 DefaultReflectorFactory实现类比较简单,这里不再进行讲解。
XMLConfigBuilder构建函数实现:
publicXMLConfigBuilder(Reader reader, String environment, Properties props) {this(newXPathParser(reader, true, props, newXMLMapperEntityResolver()), environment, props);}
实例化 XPathParser 对象
首先实例化 XPathParser 对象,里面定义了5个变量:
privatefinalDocument document;privateboolean validation;privateEntityResolver entityResolver;privateProperties variables;privateXPath xpath;
  • document 保存document对象
  • validation xml解析时是否验证文档
  • entityResolver 加载dtd文件
  • variables 配置文件定义的值
  • xpath Xpath对象,用于对XML文件节点的操作
XPathParser 对象构造函数有: 3c9bc8df9138353f8c795e84c90a18a7.png 函数里面都处理了两件事:
publicXPathParser(Reader reader, boolean validation, Properties variables, EntityResolver entityResolver) { commonConstructor(validation, variables, entityResolver);this.document = createDocument(newInputSource(reader));}
  • 初始化赋值,和创建 XPath对象,用于对XML文件节点的操作。
privatevoid commonConstructor(boolean validation, Properties variables, EntityResolver entityResolver) {this.validation = validation;this.entityResolver = entityResolver;this.variables = variables;// 创建Xpath对象,用于对XML文件节点的操作XPathFactory factory = XPathFactory.newInstance();this.xpath = factory.newXPath();}
  • 创建 Document对象并赋值到 document变量, 这里属于Document创建的操作,不再详细讲述,不懂可以点击这里查看API (https://docs.oracle.com/javase/8/docs/api/org/w3c/dom/Document.html?is-external=true)
privateDocument createDocument(InputSource inputSource) {// important: this must only be called AFTER common constructortry{// 实例化 DocumentBuilderFactory 对象,用于创建 DocumentBuilder 对象DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();// 是否校验文档 factory.setValidating(validation);// 设置 DocumentBuilderFactory 的配置 factory.setNamespaceAware(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(false); factory.setCoalescing(false); factory.setExpandEntityReferences(true);// 创建 DocumentBuilderDocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(entityResolver); builder.setErrorHandler(newErrorHandler() {@Overridepublicvoid error(SAXParseException exception) throwsSAXException{throw exception;}@Overridepublicvoid fatalError(SAXParseException exception) throwsSAXException{throw exception;}@Overridepublicvoid warning(SAXParseException exception) throwsSAXException{}});// 加载文件return builder.parse(inputSource);} catch(Exception e) {thrownewBuilderException("Error creating document instance. Cause: "+ e, e);}}
XMLConfigBuilder构造函数赋值
privateXMLConfigBuilder(XPathParser parser, String environment, Properties props) {super(newConfiguration());ErrorContext.instance().resource("SQL Mapper Configuration");this.configuration.setVariables(props);this.parsed = false;this.environment = environment;this.parser = parser;}
  1. 初始化父类 BaseBuilder的值。
  2. 将外部值赋值给对象。
  3. 将实例化的 XPathParser赋值给 parser
最后返回 XMLConfigBuilder 对象。

解析 XMLConfigBuilder 对象

通过 XMLConfigBuilder.parse() 解析配置信息,保存至 Configuration 。解析详解在后面文章中进行分析。
publicConfiguration parse() {// 是否解析过配置文件if(parsed) {thrownewBuilderException("Each XMLConfigBuilder can only be used once.");}// 标志解析过,定义为 true parsed = true;// 解析 configuration 节点中的信息 parseConfiguration(parser.evalNode("/configuration"));return configuration;}

创建 SqlSessionFactory

DefaultSqlSessionFactory 实现了 SqlSessionFactory 接口。通过上面解析得到的 Configuration ,调用 SqlSessionFactoryBuilder.build(Configurationconfig) 创建一个 DefaultSqlSessionFactory
publicSqlSessionFactory build(Configuration config) {returnnewDefaultSqlSessionFactory(config);} 实例化 DefaultSqlSessionFactory 的过程,就是将 Configuration 传递给 DefaultSqlSessionFactory 成员变量 configuration
publicDefaultSqlSessionFactory(Configuration configuration) {this.configuration = configuration;}

创建 SqlSession

通过调用 SqlSessionFactory.openSession() 创建 SqlSession
publicinterfaceSqlSessionFactory{// 默认创建SqlSession openSession();SqlSession openSession(boolean autoCommit);SqlSession openSession(Connection connection);SqlSession openSession(TransactionIsolationLevel level);SqlSession openSession(ExecutorType execType);SqlSession openSession(ExecutorType execType, boolean autoCommit);SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level);SqlSession openSession(ExecutorType execType, Connection connection);Configuration getConfiguration();}
  • autoCommit 是否自动提交事务,
  • level 事务隔离级别(共5个级别), 可查看相关源码
  • connection 连接
  • execType 执行器的类型: SIMPLE(不做特殊处理), REUSE(复用预处理语句), BATCH(会批量执行)
因为上面 DefaultSqlSessionFactory 实现了 SqlSessionFactory 接口,所以进入到 DefaultSqlSessionFactory 查看 openSession()
publicSqlSession openSession() {return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);} openSession() 方法最终实现代码如下:
privateSqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {Transaction tx = null;try{// 获取configuration中的加载环境finalEnvironment environment = configuration.getEnvironment();// 获取事务工厂finalTransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);// 创建一个事务 tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);// 生成一个处理器,事务保存在处理器 BaseExecutor 中finalExecutor executor = configuration.newExecutor(tx, execType);// 实例化一个 DefaultSqlSession,DefaultSqlSession实现了SqlSession接口returnnewDefaultSqlSession(configuration, executor, autoCommit);} catch(Exception e) {// 异常情况下关闭事务 closeTransaction(tx); // may have fetched a connection so lets call close()throwExceptionFactory.wrapException("Error opening session. Cause: "+ e, e);} finally{// 充值错误实例上下文ErrorContext.instance().reset();}} 生成处理器 Configuration.newExecutor(Transactiontransaction,ExecutorTypeexecutorType)
publicExecutor newExecutor(Transaction transaction, ExecutorType executorType) {// 默认为 ExecutorType.SIMPLE executorType = executorType == null? defaultExecutorType : executorType; executorType = executorType == null? ExecutorType.SIMPLE : executorType;Executor executor;if(ExecutorType.BATCH == executorType) { executor = newBatchExecutor(this, transaction);} elseif(ExecutorType.REUSE == executorType) { executor = newReuseExecutor(this, transaction);} else{ executor = newSimpleExecutor(this, transaction);}if(cacheEnabled) { executor = newCachingExecutor(executor);} executor = (Executor) interceptorChain.pluginAll(executor);return executor;}ExecutorType.SIMPLE 为例, BatchExecutor , ReuseExecutor 同理: 9e9f2b98668ae22a15f2a6636278df64.png 至此,mybatis的启动流程大致简单的介绍到这里,对mybatis的启动初始化有个大致了解。接下将会针对单独模块进行详细分析。

51e4254077dd4de5354558d2b482e5b7.png

近期精彩内容推荐:  

84ae006bb79cd4eb7bf40cc5ad71ea23.png 直播界要哭了!罗永浩进军电商直播

84ae006bb79cd4eb7bf40cc5ad71ea23.png 人家裁员我加薪,他凭什么身价1200亿?

84ae006bb79cd4eb7bf40cc5ad71ea23.png 什么?你还在使用fastjson,性能太差了

84ae006bb79cd4eb7bf40cc5ad71ea23.png 2020年抖音用户画像报告

9d667d772e66ee7f932061689df91006.png

fac47f3f138325a69b786bc5ce901361.png

在看点这里178fdd4d18827c1a665b9efc6d0a39e5.gif好文分享给更多人↓↓

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值