千家信息网

如何理解SqlSession技术

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,这篇文章主要介绍"如何理解SqlSession技术",在日常操作中,相信很多人在如何理解SqlSession技术问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"如何理解S
千家信息网最后更新 2026年11月02日如何理解SqlSession技术

这篇文章主要介绍"如何理解SqlSession技术",在日常操作中,相信很多人在如何理解SqlSession技术问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"如何理解SqlSession技术"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

老规矩,先上案例代码,我们按照这个案例一步一步的搞定Mybatis源码。

public class MybatisApplication {     public static final String URL = "jdbc:mysql://localhost:3306/mblog";     public static final String USER = "root";     public static final String PASSWORD = "123456";      public static void main(String[] args) {         String resource = "mybatis-config.xml";         InputStream inputStream = null;         SqlSession sqlSession = null;         try {             inputStream = Resources.getResourceAsStream(resource);             SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);             sqlSession = sqlSessionFactory.openSession();             UserMapper userMapper = sqlSession.getMapper(UserMapper.class);             System.out.println(userMapper.selectById(1));          } catch (Exception e) {             e.printStackTrace();         } finally {             try {                 inputStream.close();             } catch (IOException e) {                 e.printStackTrace();             }             sqlSession.close();         }     }

由于很多小伙伴在催,说Mybatis源码系列好像何时才有下文了,为此老田熬夜写了这篇。

继续开撸~~

SqlSession sqlSession = sqlSessionFactory.openSession();

前面那篇文章已经分析了,这里的sqlSessionFactory其实就是DefaultSqlSessionFactory。

所以这里,我们就从DefaultSqlSessionFactory里的openSession方法开始。

public class DefaultSqlSessionFactory implements SqlSessionFactory {    private final Configuration configuration;    public DefaultSqlSessionFactory(Configuration configuration) {     this.configuration = configuration;   }   //创建session,这个方法直接调用本类中的另外一个方法   @Override   public SqlSession openSession() {     return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);   }   //其实是调用这个方法   private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {     Transaction tx = null;     try {       //对应xml标签 ,这个在配置文件解析的时候就已经存放到configuration中了。       final Environment environment = configuration.getEnvironment();       final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);       tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);       //创建一个executor来执行SQL         final Executor executor = configuration.newExecutor(tx, execType);       //这里也说明了,为什么我们代码里的SqlSession是DefaultSqlSession       return new DefaultSqlSession(configuration, executor, autoCommit);     } catch (Exception e) {       closeTransaction(tx); // may have fetched a connection so lets call close()       throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);     } finally {       ErrorContext.instance().reset();     }   }        private TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {     if (environment == null || environment.getTransactionFactory() == null) {       return new ManagedTransactionFactory();     }     return environment.getTransactionFactory();   }

这个方法中的主要内容有:

下面我们就来逐个攻破。

创建事务Transaction

事务工厂类型可以配置为JDBC类型或者MANAGED类型。

JdbcTransactionFactory生产JdbcTransaction。

ManagedTransactionFactory生产ManagedTransaction。

如果配置的JDBC,则会使用Connection对象的commit()、rollback()、close()方法来管理事务。

如果我们配置的是MANAGED,会把事务交给容器来管理,比如JBOSS,Weblogic。因为我们是本地跑的程序,如果配置成MANAGED就会不有任何事务。

但是,如果我们项目中是Spring集成Mybatis,则没有必要配置事务,因为我们会直接在applicationContext.xml里配置数据源和事务管理器,从而覆盖Mybatis的配置。

创建执行器Executor

调用configuration的newExecutor方法创建Executor。

final Executor executor = configuration.newExecutor(tx, execType); //Configuration中 public Executor newExecutor(Transaction transaction, ExecutorType executorType) {     executorType = executorType == null ? defaultExecutorType : executorType;     executorType = executorType == null ? ExecutorType.SIMPLE : executorType;     Executor executor;     //第一步     if (ExecutorType.BATCH == executorType) {       executor = new BatchExecutor(this, transaction);     } else if (ExecutorType.REUSE == executorType) {       executor = new ReuseExecutor(this, transaction);     } else {       executor = new SimpleExecutor(this, transaction);     }     //第二步     if (cacheEnabled) {       executor = new CachingExecutor(executor);     }     //第三步     executor = (Executor) interceptorChain.pluginAll(executor);     return executor;   }

此方法分三个步骤。

第一步:创建执行器

Executor的基本类型有三种:

public enum ExecutorType {   SIMPLE, REUSE, BATCH }

SIMPLE为默认类型。

为什么要让抽象类BaseExecutor实现Executor接口,然后让具体实现类继承抽象类呢?

这就是模板方法模式的实现。

模板方法模式就是定义一个算法骨架,并允许子类为一个或者多个步骤提供实现。模板方法是得子类可以再不改变算法结构的情况下,重新定义算法的某些步骤。

抽象方法是在子类汇总实现的,每种执行器自己实现自己的逻辑,BaseExecutor最终会调用到具体的子类中。

抽象方法

protected abstract int doUpdate(MappedStatement ms, Object parameter) throws SQLException;  protected abstract List doFlushStatements(boolean isRollback) throws SQLException;  protected abstract  List doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException;  protected abstract  Cursor doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql)  throws SQLException;

第二步:缓存装饰

在上面代码中的第二步

if (cacheEnabled) {       executor = new CachingExecutor(executor); }

如果cacheEnabled=true,会用装饰器设计模式对Executor进行装饰。

第三步:插件代理缓存装饰完后,就会执行

executor = (Executor) interceptorChain.pluginAll(executor);

这里会对Executor植入插件逻辑。

比如:分页插件中就需要把插件植入的Executor

好了,到此,执行器创建的就搞定了。

创建DefaultSqlSession对象

把前面解析配置文件创建的Configuration对象和创建的执行器Executor赋给DefaultSqlSession中的属性。

public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {   this.configuration = configuration;   this.executor = executor;   this.dirty = false;   this.autoCommit = autoCommit; }

到这里,SqlSession(DefaultSqlSession)对象就创建完毕。

到此,关于"如何理解SqlSession技术"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

方法 配置 事务 类型 执行器 技术 子类 对象 插件 学习 代码 就是 模式 模板 步骤 算法 管理 文件 更多 案例 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 广东万易互联网科技有限公司惠州分公司 互联网和科技技术 科技企业和互联网企业 科技服务与互联网 远信互联网科技 华中科技大学互联网 奇士达互联网科技 奔汉互联网科技 科技互联网创业计划书 金融科技与互联网金融相同和不同 宁夏科技产业互联网案例 语蝶互联网科技有限公司 浙南科技城互联网大厦建设工程 互联网金融科技元年 江苏苏星互联网科技有限公司 互联网风格科技公司名称 唯美互联网科技 北京互联网科技类企业分布 北京天晋互联网科技有限公司 北京品多多互联网科技有限公司 重庆互联网络科技 西安互联网科技 齐家互联网科技 银优互联网科技有限公司 滨湖区互联网智能科技产品供应 银川新华互联网科技职业技术培训学校图片 互联网保险科技创业 东莞新华互联网科技学校 中科明德北京互联网科技 互联网科技创新创业项目
0