Mybatis 中 NodeHandler 的详解析

本篇需要与 Mybatis 中 SqlNode 接口的详解析同看,因为两篇的知识点互通。总体来说比较简单,就是使用不同的处理器解析不同的标签对。

  • 在故事的开头,我们无非就是想要创建一个 SqlSource,那就从方法开始。
@Override
public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
   
  XMLScriptBuilder builder = new XMLScriptBuilder(configuration, script, parameterType);
  return builder.parseScriptNode();
}
  • 可以看到,首先创建 Builder 类,与解析 mybatis-config.xml 时的步骤一样,那就先来看 XMLScriptBuilder 类吧。

1. XMLScriptBuilder

public class XMLScriptBuilder extends BaseBuilder {
   
  private final XNode context;
  private boolean isDynamic;
  private final Class<?> parameterType;
  private final Map<String, NodeHandler> nodeHandlerMap = new HashMap<>();
public XMLScriptBuilder(Configuration configuration, XNode context) {
   
    this(configuration, context, null);
  }
  /* configuration: 配置类
   * context: 解析的节点
   * parameterType: 参数类型
   * */
  public XMLScriptBuilder(Configuration configuration, XNode context, Class<?> parameterType) {
   
    super(configuration);
    this.context = context;
    this.parameterType = parameterType;
    // 初始化各种 Handler 类
    initNodeHandlerMap();
  }

  /* 可以看到,公有 Handler 八个处理类,九个标签节点 */
  private void initNodeHandlerMap() {
   
    nodeHandlerMap.put("trim", new TrimHandler());
    nodeHandlerMap.put("where", new WhereHandler());
    nodeHandlerMap.put("set", new SetHandler());
    nodeHandlerMap.put("foreach", new ForEachHandler());
    nodeHandlerMap.put("if", new IfHandler());
    nodeHandlerMap.put("choose", new ChooseHandler());
    nodeHandlerMap.put("when", new IfHandler());
    nodeHandlerMap.put("otherwise", new OtherwiseHandler());
    nodeHandlerMap.put("bind", new BindHandler())
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值