2-mybatis-启动

启动相关的类主要位于session包下。

参照mybatis官方文档,从xml中构建SqlSessionFactory后,可以获取SqlSession,然后使用SqlSession查询数据库得到结果。

String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
try (SqlSession session = sqlSessionFactory.openSession()) {
  Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
}

SqlSessionFactoryBuilder做为程序的入口,没有提供任何构造方法,所以可以直接使用无参构造。

  public SqlSessionFactory build(Reader reader)

  public SqlSessionFactory build(Reader reader, String environment)

  public SqlSessionFactory build(Reader reader, Properties properties)

  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(InputStream inputStream)

  public SqlSessionFactory build(InputStream inputStream, String environment) 

  public SqlSessionFactory build(InputStream inputStream, Properties properties)

  public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {
    try {
      XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);
      return build(parser.parse());
    } catch (Exception e) {
      throw ExceptionFactory.wrapException("Error building SqlSession.", e);
    } finally {
      ErrorContext.instance().reset();
      try {
        inputStream.close();
      } catch (IOException e) {
        // Intentionally ignore. Prefer previous error.
      }
    }
  }
    
  public SqlSessionFactory build(Configuration config) {
    return new DefaultSqlSessionFactory(config); 
  }

 主要实现了通过reader或者builder来读取并解析配置文件,其中解析配置文件的类为 XMLConfigBuilder(位于builder包下)。生成Cconfiguration类,然后初始化DefaultSqlSessionFactory.

xml格式如下:

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> </configuration>

如果你调用了参数有environment的build方法,那么MyBatis将会使用configuration对象一来配置这个environment。当然,如果指定了一个不合法的environment,就会得到错误的提示。如果你调用了不带environment参数的build方法,那么就使用默认的environment(在上南的示命名中指这来devault='develemtn"的代码)。

如果你调用了参数有properties实例的方法,那么MyBatis就会加载那些properties(属性配置文件),并在配置中可用。那些属性可以用${propName}语法多次用在配置文件中。

从3.4.2开始,可以为占位符指定一个默认值,如 

<property name="username" value="${username:ut_user}" />

该特性默认是关闭的,需要指定开启:

<properties resource="org/mybatis/example/config.properties"> <!-- ... --> <property name="org.apache.ibatis.parsing.PropertyParser.enable-default-value" value="true"/> <!-- 启用默认值特性 --> </properties>

如果一个属性存在于这些位置,那么 MyBatis 将会按照下面的顺序来加载它们:

  • 首先读取在 properties 元素体中指定的属性;
  • 其次,读取从 properties 元素的类路径 resource 或 url 指定的属性,且会覆盖已经指定了的重复属性;
  • 最后,读取作为方法参数传递的属性,且会覆盖已经从 properties 元素体和 resource 或 url 属性中加载了的重复属性。

因此,通过方法参数传递的属性的优先级最高,resource 或 url 指定的属性优先级中等,在 properties 元素体中指定的属性优先级最低。

builder包下的类主要功能是解析mybatis的配置文件。

org.apache.ibatis.builder.xml包下主要有三个builder 都 extend abstract class BaseBuilder:

  • XMLConfigBuilder 主要解析xml配置文件,全局配置
  • XMLMapperBuilder 主要解析mapper xml
  • XMLStatementBuilder

XMLConfigBuilder解析mybatis的配置文件,配置文件的层次如下:

configuration(配置)

猜你喜欢

转载自www.cnblogs.com/zyzl/p/11397601.html
今日推荐