mybatis源码解析四-用到的设计模式总结

设计模式总结

工厂模式
  1. SqlSessionFactory:负责创建SqlSession

public interface SqlSessionFactory {
  // 创建openSession无参,关键参数默认
  SqlSession openSession();
  // 创建是否自动提交的SqlSession,参数属于Connection的重要属性,以下方法类似
  // 关键属性由创建者决定
  SqlSession openSession(boolean autoCommit);
  SqlSession openSession(Connection connection);
  SqlSession openSession(TransactionIsolationLevel level);
  SqlSession openSession(ExecutorType execType);
  SqlSession openSession(ExecutorType execType, boolean autoCommit);
  SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level);
  SqlSession openSession(ExecutorType execType, Connection connection);
  Configuration getConfiguration();
}
构造器模式
  1. SqlSessionFactoryBuilder负责构建SqlSessionFactory

使用心得:SqlSessionFactory实现类的构造方法public DefaultSqlSessionFactory(Configuration configuration),目前已有的资源(xml文件)需要转换,那么直接使用构造器,拿到xml,然后再build中进行转换来构造SqlSessionFactory

// 通过额外的参数转换成SqlSessionFactory真正需要的参数
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
  try {  
    XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
    // 最终是拿到Config创建SqlSessionFactory,使得SqlSessionFactory代码内的构造器更加简洁
    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.
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值