【Mybatis】MybatisV2版本基础, 基于V1提取框架

Mybatis V2版本基础, 基于V1提取框架

1.综述

  • 配置文件升级为 xml 格式
  • 使用面向过程方式优化代码
  • 使用面向对象思想构造配置类封装对象
  • 示例代码
  • 主体函数如下
public class MybatisV2 {

    private static Configuration configuration;

    public static void main(String[] args) throws Exception {
        MybatisV2 mybatisV2 = new MybatisV2();
        // 1. 加载 xml 全局配置
        mybatisV2.loadXml("v2-mybatis-config.xml");

        // 2. 执行查询
        User query = new User();
        query.setName("测试");
        query.setSex("测试");
        List<User> users = mybatisV2.selectList("userMapper.queryUserList", query);
        System.out.printf("users: %s", users);
    }
}

2.核心流程

private <R, T> List<R> selectList(String statementId, T parameter) throws Exception {
    List<R> result = new LinkedList<>();
    MappedStatement mappedStatement = configuration.getMappedStatementsById(statementId);

    // 1. 获取数据库连接
    Connection connection = getConnection();

    // 2. 获取执行 SQL
    String sql = getSql(mappedStatement);

    // 3. 创建 statement
    Statement statement = createStatement(mappedStatement, sql, connection);

    // 4. 设置参数
    setParameters(parameter, statement, mappedStatement);

    // 5. 执行 statement
    ResultSet resultSet = executeQuery(statement);

    // 6. 处理结果
    handlerResultSet(resultSet, mappedStatement, result);

    return result;
}

3.主流程核心对象解析

  1. Configuration
  • org.apache.ibatis.session.Configuration
  • Mybatis 全局配置
  • 构造时会默认注册一些别名
  • XMLLanguageDriver、RawLanguageDriver 会在构造时注册

public class Configuration {

  // 环境
  protected Environment environment;
  // 变量参数,用于解析 xml 时替换 ${}
  // 可从配置文件<properties>标签中解析出来
  protected Properties variables = new Properties();
  // 别名注册,别名:类,会注册在这里
  protected final TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
  // MappedStatement 对象解析后会存储在此
  protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>();
  // resultMaps
  protected final Map<String, ResultMap> resultMaps = new StrictMap<>("Result Maps collection");
  // parameterMaps
  protected final Map<String, ParameterMap> parameterMaps = new StrictMap<>("Parameter Maps collection");

}
  1. MappedStatement
  • org.apache.ibatis.mapping
  • 存储 Sql Statement 相关信息
public final class MappedStatement {

  private Configuration configuration;
  // namespace + "." + statementId
  private String id;
  // STATEMENT, PREPARED, CALLABLE
  private StatementType statementType;
  private ResultSetType resultSetType;
  // 存储了参数列表 List<ParameterMapping>
  private ParameterMap parameterMap;
  // 存储了 sql 相关
  private SqlSource sqlSource;

}
  1. ParameterMapping
  • org.apache.ibatis.mapping
  • 参数映射对象
public class ParameterMapping {

  private Configuration configuration;

  // 参数名
  private String property;
  // java 类型
  private Class<?> javaType = Object.class;
  // jdbc 类型
  private JdbcType jdbcType;
  private String jdbcTypeName;

}
  1. XmlConfigBuilder
  • org.apache.ibatis.builder.xml
  • xml 配置文件解析构造器
public class XMLConfigBuilder extends BaseBuilder {

  protected final Configuration configuration;
  // 别名注册器
  protected final TypeAliasRegistry typeAliasRegistry;
  // xml 解析器
  private final XPathParser parser;

  // 核心解析 xml 逻辑
  private void parseConfiguration(XNode root) {
    try {
      //issue #117 read properties first
      propertiesElement(root.evalNode("properties"));
      Properties settings = settingsAsProperties(root.evalNode("settings"));
      loadCustomVfs(settings);
      loadCustomLogImpl(settings);
      typeAliasesElement(root.evalNode("typeAliases"));
      pluginElement(root.evalNode("plugins"));
      objectFactoryElement(root.evalNode("objectFactory"));
      objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));
      reflectorFactoryElement(root.evalNode("reflectorFactory"));
      settingsElement(settings);
      // read it after objectFactory and objectWrapperFactory issue #631
      environmentsElement(root.evalNode("environments"));
      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);
    }
  }

}

// 加载 properties 文件
private void propertiesElement(XNode context) throws Exception {
    if (context != null) {
      Properties defaults = context.getChildrenAsProperties();
      Properties vars = configuration.getVariables();
      if (vars != null) {
        defaults.putAll(vars);
      }

      // 将变量放入 xml 解析器,后续解析 ${} 会自动替换变量
      parser.setVariables(defaults);
      configuration.setVariables(defaults);
    }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值