MyBatis源码分析之策略模式和模板方法模式的应用

策略模式的应用

MyBatis配置文件mybatis-config.xml中的节点settings中有一项配置如下:

<setting name="defaultExecutorType" value="SIMPLE" />

配置的是MyBatis在运行过程中默认的执行器Executor,此项配置的默认值就是SIMPLE,指向就是接口Executor的实现类SimpleExecutor。

通过查看源码可以看得到接口Executor的继承关系如下:
这里写图片描述

这些实现类为接口Executor的策略簇,实现了不同的执行器策略:

SimpleExecutor:普通的执行器
BatchExecutor:批处理执行器
ReuseExecutor:预处理语句重用执行器

MyBatis核心类Configuration类似于策略模式中的Context,区别于Context就是:Context维系是的传入的策略对象;Configuration是根据传入的策略对象类型,生产相应的策略对象,代码如下:

// 配置文件中setting属性为defaultExecutorType的对应Configuration属性
protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
// 根据默认的执行器类型,生成相应的执行器对象
public Executor newExecutor(Transaction transaction) {
    return newExecutor(transaction, defaultExecutorType);
}
// 可指定执行器类型,生成相应的执行器对象
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType &#
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值