mybatis 创建Sqlsession

 

看下执行下面这行代码的时候,都发生了什么

SqlSession sqlSession = sqlSessionFactory.openSession();

第一步:org.apache.ibatis.session.defaults.DefaultSqlSessionFactory#openSession()

public SqlSession openSession() {
	return openSessionFromDataSource(configuration.getDefaultExecutorType(), null, false);
}

第二步:org.apache.ibatis.session.defaults.DefaultSqlSessionFactory#openSessionFromDataSource

这块代码是根据配置的Environment创建Executor。再创建DefaultSqlSession返回。

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
        final Environment environment = configuration.getEnvironment();
        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
        tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
        //最主要的是这里,这里创建Executor
        final Executor executor = configuration.newExecutor(tx, execType);
        //创建SqlSession并返回
        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();
    }
}

看下DefaultSqlSession的构造函数,DefaultSqlSession有一个Executor的属性。

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

接着就看下Executor的创建过程吧

org.apache.ibatis.session.Configuration#newExecutor(Transaction, ExecutorType)

这块代码有两点

一、根据ExecutorType创建不同的类型的Executor

也不难看出,Executor一共有三种类型

  1. BatchExecutor
  2. ReuseExecutor
  3. SimpleExecutor(默认)

还有就是,这三种Executor都可以再次封装为CachingExecutor

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 = (Executor) interceptorChain.pluginAll(executor);

org.apache.ibatis.plugin.InterceptorChain#pluginAll

public Object pluginAll(Object target) {
    //遍历所有的插件,执行插件的plugin方法
    for (Interceptor interceptor : interceptors) {
        target = interceptor.plugin(target);
    }
    return target;
}

 

MyBatis使用SqlSession来执行数据库操作。SqlSessionMyBatis中的核心接口,它提供了多种方法来执行SQL语句、提交事务、获取映射器等操作。 首先,你需要通过SqlSessionFactory来创建SqlSession对象。SqlSessionFactory是由MyBatis配置文件和映射文件构建而成的,它负责创建SqlSession对象。 下面是使用SqlSessionFactory创建SqlSession的代码示例: ```java String resource = "mybatis-config.xml"; // MyBatis配置文件的路径 InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); ``` 一旦你获得了SqlSession对象,你就可以使用它来执行各种数据库操作。以下是一些常用的SqlSession方法: - `selectOne(String statement, Object parameter)`:查询单条记录。 - `selectList(String statement, Object parameter)`:查询多条记录。 - `insert(String statement, Object parameter)`:插入数据。 - `update(String statement, Object parameter)`:更新数据。 - `delete(String statement, Object parameter)`:删除数据。 - `commit()`:提交事务。 - `rollback()`:回滚事务。 其中,`statement`参数是映射文件中定义的SQL语句的唯一标识,`parameter`参数是传递给SQL语句的参数。 使用完SqlSession后,记得关闭它以释放资源: ```java sqlSession.close(); ``` 这就是MyBatis中使用SqlSession进行数据库操作的基本流程。希望能对你有所帮助!如果还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值