国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

深入淺出Mybatis系列(二)---配置簡介(mybatis源碼篇)

2019-11-11 06:27:07
字體:
供稿:網(wǎng)友

上篇文章《深入淺出Mybatis系列(一)---Mybatis入門》, 寫了一個Demo簡單體現(xiàn)了一下Mybatis的流程。本次,將簡單介紹一下Mybatis的配置文件:

上次例子中,我們以 SqlsessionFactoryBuilder 去創(chuàng)建 SqlSessionFactory,  那么,我們就先從SqlSessionFactoryBuilder入手, 咱們先看看源碼是怎么實(shí)現(xiàn)的:

SqlSessionFactoryBuilder源碼片段:

public class SqlSessionFactoryBuilder {  //Reader讀取mybatis配置文件,傳入構(gòu)造方法  //除了Reader外,其實(shí)還有對應(yīng)的inputStream作為參數(shù)的構(gòu)造方法,  //這也體現(xiàn)了mybatis配置的靈活性  public SqlSessionFactory build(Reader reader) {    return build(reader, null, null);  }  public SqlSessionFactory build(Reader reader, String environment) {    return build(reader, environment, null);  }    //mybatis配置文件 + PRoperties, 此時mybatis配置文件中可以不配置properties,也能使用${}形式  public SqlSessionFactory build(Reader reader, Properties properties) {    return build(reader, null, properties);  }    //通過xmlConfigBuilder解析mybatis配置,然后創(chuàng)建SqlSessionFactory對象  public SqlSessionFactory build(Reader reader, String environment, Properties properties) {    try {      XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);      //下面看看這個方法的源碼      return build(parser.parse());    } catch (Exception e) {      throw ExceptionFactory.wrapException("Error building SqlSession.", e);    } finally {      ErrorContext.instance().reset();      try {        reader.close();      } catch (IOException e) {        // Intentionally ignore. Prefer previous error.      }    }  }  public SqlSessionFactory build(Configuration config) {    return new DefaultSqlSessionFactory(config);  }}通過源碼,我們可以看到SqlSessionFactoryBuilder 通過XMLConfigBuilder 去解析我們傳入的mybatis的配置文件, 下面就接著看看 XMLConfigBuilder 部分源碼:

/** * mybatis 配置文件解析 */public class XMLConfigBuilder extends BaseBuilder {  public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {    this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props);  }  private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {    super(new Configuration());    ErrorContext.instance().resource("SQL Mapper Configuration");    this.configuration.setVariables(props);    this.parsed = false;    this.environment = environment;    this.parser = parser;  }    //外部調(diào)用此方法對mybatis配置文件進(jìn)行解析  public Configuration parse() {    if (parsed) {      throw new BuilderException("Each XMLConfigBuilder can only be used once.");    }    parsed = true;    //從根節(jié)點(diǎn)configuration    parseConfiguration(parser.evalNode("/configuration"));    return configuration;  }  //此方法就是解析configuration節(jié)點(diǎn)下的子節(jié)點(diǎn)  //由此也可看出,我們在configuration下面能配置的節(jié)點(diǎn)為以下10個節(jié)點(diǎn)  private void parseConfiguration(XNode root) {    try {      propertiesElement(root.evalNode("properties")); //issue #117 read properties first      typeAliasesElement(root.evalNode("typeAliases"));      pluginElement(root.evalNode("plugins"));      objectFactoryElement(root.evalNode("objectFactory"));      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));      settingsElement(root.evalNode("settings"));      environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631      databaseIdProviderElement(root.evalNode("databaseIdProvider"));      typeHandlerElement(root.evalNode("typeHandlers"));      mapperElement(root.evalNode("mappers"));    } catch (Exception e) {      throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);    }  }}

通過以上源碼,我們就能看出,在mybatis的配置文件中:

1. configuration節(jié)點(diǎn)為根節(jié)點(diǎn)。

2. 在configuration節(jié)點(diǎn)之下,我們可以配置10個子節(jié)點(diǎn), 分別為:properties、typeAliases、plugins、objectFactory、objectWrapperFactory、settings、environments、databaseIdProvider、typeHandlers、mappers。

 

本篇文章就先只介紹這些內(nèi)容,接下來的文章將依次分析解析這個10個節(jié)點(diǎn)中比較重要的幾個節(jié)點(diǎn)的源碼,看看在解析這些節(jié)點(diǎn)的時候,到底做了些什么。


上一篇:C#之MySql登錄

下一篇:POJ - 1987 樹分治

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 原平市| 田阳县| 河东区| 通榆县| 西平县| 唐山市| 盐边县| 米林县| 临城县| 平遥县| 仙桃市| 东安县| 南通市| 九江市| 九江县| 南江县| 阳原县| 壤塘县| 易门县| 稻城县| 蒙自县| 望江县| 延庆县| 黔南| 张家港市| 女性| 库尔勒市| 始兴县| 蓬安县| 东丽区| 哈密市| 商洛市| 油尖旺区| 和政县| 临海市| 类乌齐县| 黔西| 两当县| 乌恰县| 霍城县| 原阳县|