MyBatis(3)-执行流程

SQL执行流程

MyBatis总是通过SqlSessionFactoryBuilder创建SqlSessionFactory,然后用SqlSessionFactory创建SqlSession,再用SqlSession获取Mapper接口来进行SQL操作。

通过SqlSession.getMapper -> this.configuration.getMapper(type, this) -> this.mapperRegistry.getMapper(type, sqlSession) -> mapperProxyFactory.newInstance(sqlSession) -> Proxy.newProxyInstance(this.mapperInterface.getClassLoader(), new Class[]{ this.mapperInterface}, mapperProxy)获得了一个Mapper接口类型的反射代理类。其中MapperProxy类用来处理Mapper接口函数的请求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    try {
        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(this, args);
        }

        if (this.isDefaultMethod(method)) {
            return this.invokeDefaultMethod(proxy, method, args);
        }
    } catch (Throwable var5) {
        throw ExceptionUtil.unwrapThrowable(var5);
    }

    MapperMethod mapperMethod = this.cachedMapperMethod(method);
    return mapperMethod.execute(this.sqlSession, args);
}
public Object execute(SqlSession sqlSession, Object[] args) {
    Object param;
    Object result;
    switch(this.command.getType()) {
    case INSERT:
        param = this.method.convertArgsToSqlCommandParam(args);
        result = this.rowCountResult(sqlSession.insert(this.command.getName(), param));
        break;
    case UPDATE:
        param = this.method.convertArgsToSqlCommandParam(args);
        result = this.rowCountResult(sqlSession.update(this.command.getName(), param));
        break;
    case DELETE:
        param = this.method.convertArgsToSqlCommandParam(args);
        result = this.rowCountResult(sqlSession.delete(this.command.getName(), param));
        break;
    case SELECT:
        if (this.method.returnsVoid() && this.method.hasResultHandler()) {
            this.executeWithResultHandler(sqlSession, args);
            result = null;
        } else if (this.method.returnsMany()) {
            result = this.executeForMany(sqlSession, args);
        } else if (this.method.returnsMap()) {
            result = this.executeForMap(sqlSession, args);
        } else if (this.method.returnsCursor()) {
            result = this.executeForCursor(sqlSession, args);
        } else {
            param = this.method.convertArgsToSqlCommandParam(args);
            result = sqlSession.selectOne(this.command.getName(), param);
        }
        break;
    case FLUSH:
        result = sqlSession.flushStatements();
        break;
    default:
        throw new BindingException("Unknown execution method for: " + this.command.getName());
    }

    if (result == null && this.method.getReturnType().isPrimitive() && !this.method.returnsVoid()) {
        throw new BindingException("Mapper method '" + this.command.getName() + " attempted to return null from a method with a primitive return type (" + this.method.getReturnType() + ").");
    } else {
        return result;
    }
}

最终请求被转回SqlSession进行处理,而SqlSession底层都是依赖Executor类进行SQL操作。Executor依赖Transaction进行getConnection(持有DataSource进行连续获取MyBatis(2)-连接管理)、commit、rollback、close等事务控制,如果是JdbcTransaction则直接依赖java.sql.Connection的方法。Executor同时依赖StatementHandler调用java.sql.Statement进行SQL执行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? this.defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Object 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 (this.cacheEnabled) {
        executor = new CachingExecutor((Executor)executor);
    }

    Executor executor = (Executor)this.interceptorChain.pluginAll(executor);
    return executor;
}

缓存管理

一级缓存

默认值为SESSION,这种情况下会缓存一个会话中执行的所有查询。若设置值为STATEMENT,本地会话仅用在语句执行上,对相同SqlSession的不同调用将不会共享数据。若设置为SESSION,同一个session内的insert/delete/update操作会令缓存失效,但另外一个session的insert/delete/update操作不会,所以会一起脏读,建议设置为STATEMENT。缓存存在BaseExecutor中PerpetualCache的HashMap里。

1
<setting name="localCacheScope" value="SESSION"/>

二级缓存

利用cacheEnabled全局地开启或关闭配置文件中的所有映射器已经配置的任何缓存,默认为true。由CachingExecutor装饰某一个其他的BaseExecutor来实现,可以在SqlSession之间共享缓存数据,但要commit之后才能生效。由于缓存是以Mapper名区分存储的,一个Mapper的更新操作不会去刷新另一个Mapper的缓存,所以多表查询时会出现脏读。

1
<setting name="cacheEnabled" value="true"/>

聊聊MyBatis缓存机制

MyBatis(1)-DataSource

MyBatis(2)-连接管理