{"id":2002,"date":"2019-10-25T08:08:24","date_gmt":"2019-10-25T00:08:24","guid":{"rendered":"https:\/\/coderbee.net\/?p=2002"},"modified":"2019-10-27T09:23:45","modified_gmt":"2019-10-27T01:23:45","slug":"mybatis-%e4%ba%8b%e5%8a%a1%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/framework\/20191025\/2002","title":{"rendered":"MyBatis \u4e8b\u52a1\u7ba1\u7406"},"content":{"rendered":"<h1>1. \u8fd0\u884c\u73af\u5883 Enviroment<\/h1>\n<p>\u5f53 MyBatis \u4e0e\u4e0d\u540c\u7684\u5e94\u7528\u7ed3\u5408\u65f6\uff0c\u9700\u8981\u4e0d\u540c\u7684\u4e8b\u52a1\u7ba1\u7406\u673a\u5236\u3002\u4e0e Spring \u7ed3\u5408\u65f6\uff0c\u7531 Spring \u6765\u7ba1\u7406\u4e8b\u52a1\uff1b\u5355\u72ec\u4f7f\u7528\u65f6\u9700\u8981\u81ea\u884c\u7ba1\u7406\u4e8b\u52a1\uff0c\u5728\u5bb9\u5668\u91cc\u8fd0\u884c\u65f6\u53ef\u80fd\u7531\u5bb9\u5668\u8fdb\u884c\u7ba1\u7406\u3002<\/p>\n<p>MyBatis \u7528 Enviroment \u6765\u8868\u793a\u8fd0\u884c\u73af\u5883\uff0c\u5176\u5c01\u88c5\u4e86\u4e09\u4e2a\u5c5e\u6027\uff1a<\/p>\n<pre><code class=\"java\">public class Configuration {\n    \/\/ \u4e00\u4e2a MyBatis \u7684\u914d\u7f6e\u53ea\u5bf9\u5e94\u4e00\u4e2a\u73af\u5883\n    protected Environment environment;\n    \/\/ \u5176\u4ed6\u5c5e\u6027 .....\n}\n\npublic final class Environment {\n    private final String id;\n    private final TransactionFactory transactionFactory;\n    private final DataSource dataSource;\n}\n<\/code><\/pre>\n<h1>2. \u4e8b\u52a1\u62bd\u8c61<\/h1>\n<p>MyBatis \u628a\u4e8b\u52a1\u7ba1\u7406\u62bd\u8c61\u51fa <code>Transaction<\/code> \u63a5\u53e3\uff0c\u7531 <code>TransactionFactory<\/code> \u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\u8d1f\u8d23\u521b\u5efa\u3002<\/p>\n<pre><code class=\"java\">public interface Transaction {\n    Connection getConnection() throws SQLException;\n    void commit() throws SQLException;\n    void rollback() throws SQLException;\n    void close() throws SQLException;\n    Integer getTimeout() throws SQLException;\n}\n\npublic interface TransactionFactory {\n    void setProperties(Properties props);\n    Transaction newTransaction(Connection conn);\n    Transaction newTransaction(DataSource dataSource, TransactionIsolationLevel level, boolean autoCommit);\n}\n<\/code><\/pre>\n<p>Executor \u7684\u5b9e\u73b0\u6301\u6709\u4e00\u4e2a SqlSession \u5b9e\u73b0\uff0c\u4e8b\u52a1\u63a7\u5236\u662f\u59d4\u6258\u7ed9 SqlSession \u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u7684\u3002<\/p>\n<pre><code class=\"java\">public abstract class BaseExecutor implements Executor {\n    protected Transaction transaction;\n\n    public void commit(boolean required) throws SQLException {\n        if (closed) {\n            throw new ExecutorException(\"Cannot commit, transaction is already closed\");\n        }\n        clearLocalCache();\n        flushStatements();\n        if (required) {\n            transaction.commit();\n        }\n    }\n\n    public void rollback(boolean required) throws SQLException {\n        if (!closed) {\n            try {\n                clearLocalCache();\n                flushStatements(true);\n            } finally {\n                if (required) {\n                    transaction.rollback();\n                }\n            }\n        }\n    }\n\n    \/\/ \u7701\u7565\u5176\u4ed6\u65b9\u6cd5\u3001\u5c5e\u6027\n}\n<\/code><\/pre>\n<p><!--more--><\/p>\n<h1>3. \u4e0e Spring \u96c6\u6210\u7684\u4e8b\u52a1\u7ba1\u7406<\/h1>\n<h2>3.1 \u914d\u7f6e TransactionFactory<\/h2>\n<p>\u4e0e Spring \u96c6\u6210\u65f6\uff0c\u901a\u8fc7 SqlSessionFactoryBean \u6765\u521d\u59cb\u5316 MyBatis \u3002<\/p>\n<pre><code class=\"java\">protected SqlSessionFactory buildSqlSessionFactory() throws IOException {\n    Configuration configuration;\n\n    XMLConfigBuilder xmlConfigBuilder = null;\n    if (this.configuration != null) {\n        configuration = this.configuration;\n        if (configuration.getVariables() == null) {\n            configuration.setVariables(this.configurationProperties);\n        } else if (this.configurationProperties != null) {\n            configuration.getVariables().putAll(this.configurationProperties);\n        }\n    } else if (this.configLocation != null) {\n        xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties);\n        configuration = xmlConfigBuilder.getConfiguration();\n    } else {\n        configuration = new Configuration();\n        configuration.setVariables(this.configurationProperties);\n    }\n\n    if (this.objectFactory != null) {\n        configuration.setObjectFactory(this.objectFactory);\n    }\n\n    if (this.objectWrapperFactory != null) {\n        configuration.setObjectWrapperFactory(this.objectWrapperFactory);\n    }\n\n    if (this.vfs != null) {\n        configuration.setVfsImpl(this.vfs);\n    }\n\n    if (hasLength(this.typeAliasesPackage)) {\n        String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage,\n        ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);\n        for (String packageToScan : typeAliasPackageArray) {\n            configuration.getTypeAliasRegistry().registerAliases(packageToScan,\n            typeAliasesSuperType == null ? Object.class : typeAliasesSuperType);\n        }\n    }\n\n    if (!isEmpty(this.typeAliases)) {\n        for (Class&lt;?&gt; typeAlias : this.typeAliases) {\n            configuration.getTypeAliasRegistry().registerAlias(typeAlias);\n        }\n    }\n\n    if (!isEmpty(this.plugins)) {\n        for (Interceptor plugin : this.plugins) {\n            configuration.addInterceptor(plugin);\n        }\n    }\n\n    if (hasLength(this.typeHandlersPackage)) {\n        String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage,\n        ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);\n        for (String packageToScan : typeHandlersPackageArray) {\n            configuration.getTypeHandlerRegistry().register(packageToScan);\n        }\n    }\n\n    if (!isEmpty(this.typeHandlers)) {\n        for (TypeHandler&lt;?&gt; typeHandler : this.typeHandlers) {\n            configuration.getTypeHandlerRegistry().register(typeHandler);\n        }\n    }\n\n    if (this.databaseIdProvider != null) {\/\/fix #64 set databaseId before parse mapper xmls\n        try {\n            configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));\n        } catch (SQLException e) {\n            throw new NestedIOException(\"Failed getting a databaseId\", e);\n        }\n    }\n\n    if (this.cache != null) {\n        configuration.addCache(this.cache);\n    }\n\n    if (xmlConfigBuilder != null) {\n        try {\n            xmlConfigBuilder.parse();\n        } catch (Exception ex) {\n            throw new NestedIOException(\"Failed to parse config resource: \" + this.configLocation, ex);\n        } finally {\n            ErrorContext.instance().reset();\n        }\n    }\n\n    \/\/ \u521b\u5efa SpringManagedTransactionFactory\n    if (this.transactionFactory == null) {\n        this.transactionFactory = new SpringManagedTransactionFactory();\n    }\n\n    \/\/ \u5c01\u88c5\u6210 Environment\n    configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));\n\n    if (!isEmpty(this.mapperLocations)) {\n        for (Resource mapperLocation : this.mapperLocations) {\n            if (mapperLocation == null) {\n                continue;\n            }\n\n            try {\n                XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(),\n                configuration, mapperLocation.toString(), configuration.getSqlFragments());\n                xmlMapperBuilder.parse();\n            } catch (Exception e) {\n                throw new NestedIOException(\"Failed to parse mapping resource: '\" + mapperLocation + \"'\", e);\n            } finally {\n                ErrorContext.instance().reset();\n            }\n        }\n    } else {\n    }\n\n    return this.sqlSessionFactoryBuilder.build(configuration);\n}\n<\/code><\/pre>\n<pre><code class=\"java\">public class SqlSessionFactoryBuilder {\n    public SqlSessionFactory build(Configuration config) {\n        return new DefaultSqlSessionFactory(config);\n    }\n}\n<\/code><\/pre>\n<p>\u91cd\u70b9\u662f\u5728\u6784\u5efa MyBatis Configuration \u5bf9\u8c61\u65f6\uff0c\u628a transactionFactory \u914d\u7f6e\u6210 SpringManagedTransactionFactory\uff0c\u518d\u5c01\u88c5\u6210 Environment \u5bf9\u8c61\u3002<\/p>\n<h2>3.2 \u8fd0\u884c\u65f6\u4e8b\u52a1\u7ba1\u7406<\/h2>\n<p>Mapper \u7684\u4ee3\u7406\u5bf9\u8c61\u6301\u6709\u7684\u662f <code>SqlSessionTemplate<\/code>\uff0c\u5176\u5b9e\u73b0\u4e86 <code>SqlSession<\/code> \u63a5\u53e3\u3002<\/p>\n<p><code>SqlSessionTemplate<\/code> \u7684\u65b9\u6cd5\u5e76\u4e0d\u76f4\u63a5\u8c03\u7528\u5177\u4f53\u7684 <code>SqlSession<\/code> \u7684\u65b9\u6cd5\uff0c\u800c\u662f\u59d4\u6258\u7ed9\u4e00\u4e2a\u52a8\u6001\u4ee3\u7406\uff0c\u901a\u8fc7\u4ee3\u7406 <code>SqlSessionInterceptor<\/code> \u5bf9\u65b9\u6cd5\u8c03\u7528\u8fdb\u884c\u62e6\u622a\u3002<\/p>\n<p><code>SqlSessionInterceptor<\/code> \u8d1f\u8d23\u83b7\u53d6\u771f\u5b9e\u7684\u4e0e\u6570\u636e\u5e93\u5173\u8054\u7684 <code>SqlSession<\/code> \u5b9e\u73b0\uff0c\u5e76\u5728\u65b9\u6cd5\u6267\u884c\u5b8c\u540e\u51b3\u5b9a\u63d0\u4ea4\u6216\u56de\u6eda\u4e8b\u52a1\u3001\u5173\u95ed\u4f1a\u8bdd\u3002<\/p>\n<pre><code class=\"java\">public class SqlSessionTemplate implements SqlSession, DisposableBean {\n    private final SqlSessionFactory sqlSessionFactory;\n    private final ExecutorType executorType;\n    private final SqlSession sqlSessionProxy;\n    private final PersistenceExceptionTranslator exceptionTranslator;\n\n    public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,\n        PersistenceExceptionTranslator exceptionTranslator) {\n\n        notNull(sqlSessionFactory, \"Property 'sqlSessionFactory' is required\");\n        notNull(executorType, \"Property 'executorType' is required\");\n\n        this.sqlSessionFactory = sqlSessionFactory;\n        this.executorType = executorType;\n        this.exceptionTranslator = exceptionTranslator;\n\n        \/\/ \u56e0\u4e3a SqlSession \u63a5\u53e3\u58f0\u660e\u7684\u65b9\u6cd5\u4e5f\u4e0d\u5c11\uff0c\n        \/\/ \u5728\u6bcf\u4e2a\u65b9\u6cd5\u91cc\u6dfb\u52a0\u4e8b\u52a1\u76f8\u5173\u7684\u62e6\u622a\u6bd4\u8f83\u9ebb\u70e6\uff0c\n        \/\/ \u4e0d\u5982\u521b\u5efa\u4e00\u4e2a\u5185\u90e8\u7684\u4ee3\u7406\u5bf9\u8c61\u8fdb\u884c\u7edf\u4e00\u5904\u7406\u3002\n        this.sqlSessionProxy = (SqlSession) newProxyInstance(\n            SqlSessionFactory.class.getClassLoader(),\n            new Class[] { SqlSession.class },\n            new SqlSessionInterceptor());\n    }\n\n    public int update(String statement) {\n        \/\/ \u5728\u4ee3\u7406\u5bf9\u8c61\u4e0a\u6267\u884c\u65b9\u6cd5\u8c03\u7528\n        return this.sqlSessionProxy.update(statement);\n    }\n\n    \/\/ \u5bf9\u65b9\u6cd5\u8c03\u7528\u8fdb\u884c\u62e6\u622a\uff0c\u52a0\u5165\u4e8b\u52a1\u63a7\u5236\u903b\u8f91\n    private class SqlSessionInterceptor implements InvocationHandler {\n        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\n            \/\/ \u83b7\u53d6\u4e0e\u6570\u636e\u5e93\u5173\u8054\u7684\u4f1a\u8bdd\n            SqlSession sqlSession = SqlSessionUtils.getSqlSession(\n                SqlSessionTemplate.this.sqlSessionFactory,\n                SqlSessionTemplate.this.executorType,\n                SqlSessionTemplate.this.exceptionTranslator);\n            try {\n                \/\/ \u6267\u884c SQL \u64cd\u4f5c\n                Object result = method.invoke(sqlSession, args);\n                if (!SqlSessionUtils.isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) {\n                    \/\/ \u5982\u679c sqlSession \u4e0d\u662f Spring \u7ba1\u7406\u7684\uff0c\u5219\u8981\u81ea\u884c\u63d0\u4ea4\u4e8b\u52a1\n                    sqlSession.commit(true);\n                }\n                return result;\n            } catch (Throwable t) {\n                Throwable unwrapped = unwrapThrowable(t);\n                if (SqlSessionTemplate.this.exceptionTranslator != null &amp;&amp; unwrapped instanceof PersistenceException) {\n\n                    SqlSessionUtils.closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);\n                    sqlSession = null;\n                    Throwable translated = SqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException) unwrapped);\n                    if (translated != null) {\n                        unwrapped = translated;\n                    }\n                }\n                throw unwrapped;\n            } finally {\n                if (sqlSession != null) {\n                    SqlSessionUtils.closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);\n                }\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<p><code>SqlSessionUtils<\/code> \u5c01\u88c5\u4e86\u5bf9 Spring \u4e8b\u52a1\u7ba1\u7406\u673a\u5236\u7684\u8bbf\u95ee\u3002<\/p>\n<pre><code class=\"java\">\/\/ SqlSessionUtils\npublic static SqlSession getSqlSession(SqlSessionFactory sessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) {\n    \/\/ \u4ece Spring \u7684\u4e8b\u52a1\u7ba1\u7406\u673a\u5236\u90a3\u91cc\u83b7\u53d6\u5f53\u524d\u4e8b\u52a1\u5173\u8054\u7684\u4f1a\u8bdd\n    SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);\n\n    SqlSession session = sessionHolder(executorType, holder);\n    if (session != null) {\n        \/\/ \u5df2\u7ecf\u6709\u4e00\u4e2a\u4f1a\u8bdd\u5219\u590d\u7528\n        return session;\n    }\n\n    \/\/ \u521b\u5efa\u65b0\u7684 \u4f1a\u8bdd\n    session = sessionFactory.openSession(executorType);\n\n    \/\/ \u6ce8\u518c\u5230 Spring \u7684\u4e8b\u52a1\u7ba1\u7406\u673a\u5236\u91cc\n    registerSessionHolder(sessionFactory, executorType, exceptionTranslator, session);\n\n    return session;\n}\n\nprivate static void registerSessionHolder(SqlSessionFactory sessionFactory, ExecutorType executorType,\n        PersistenceExceptionTranslator exceptionTranslator, SqlSession session) {\n    SqlSessionHolder holder;\n    if (TransactionSynchronizationManager.isSynchronizationActive()) {\n        Environment environment = sessionFactory.getConfiguration().getEnvironment();\n\n        if (environment.getTransactionFactory() instanceof SpringManagedTransactionFactory) {\n            holder = new SqlSessionHolder(session, executorType, exceptionTranslator);\n            TransactionSynchronizationManager.bindResource(sessionFactory, holder);\n\n            \/\/ \u91cd\u70b9\uff1a\u6ce8\u518c\u4f1a\u8bdd\u7ba1\u7406\u7684\u56de\u8c03\u94a9\u5b50\uff0c\u771f\u6b63\u7684\u5173\u95ed\u52a8\u4f5c\u662f\u5728\u56de\u8c03\u91cc\u5b8c\u6210\u7684\u3002\n            TransactionSynchronizationManager.registerSynchronization(new SqlSessionSynchronization(holder, sessionFactory));\n            holder.setSynchronizedWithTransaction(true);\n\n            \/\/ \u7ef4\u62a4\u4f1a\u8bdd\u7684\u5f15\u7528\u8ba1\u6570\n            holder.requested();\n        } else {\n            if (TransactionSynchronizationManager.getResource(environment.getDataSource()) == null) {\n            } else {\n                throw new TransientDataAccessResourceException(\n                    \"SqlSessionFactory must be using a SpringManagedTransactionFactory in order to use Spring transaction synchronization\");\n            }\n        }\n    } else {\n    }\n}\n\npublic static void closeSqlSession(SqlSession session, SqlSessionFactory sessionFactory) {\n    \/\/ \u4ece\u7ebf\u7a0b\u672c\u5730\u53d8\u91cf\u91cc\u83b7\u53d6 Spring \u7ba1\u7406\u7684\u4f1a\u8bdd\n    SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);\n    if ((holder != null) &amp;&amp; (holder.getSqlSession() == session)) {\n        \/\/ Spring \u7ba1\u7406\u7684\u4e0d\u76f4\u63a5\u5173\u95ed\uff0c\u7531\u56de\u8c03\u94a9\u5b50\u6765\u5173\u95ed\n        holder.released();\n    } else {\n        \/\/ \u975e Spring \u7ba1\u7406\u7684\u76f4\u63a5\u5173\u95ed\n        session.close();\n    }\n}\n<\/code><\/pre>\n<p><code>SqlSessionSynchronization<\/code> \u662f <code>SqlSessionUtils<\/code> \u7684\u5185\u90e8\u79c1\u6709\u7c7b\uff0c\u7528\u4e8e\u4f5c\u4e3a\u56de\u8c03\u94a9\u5b50\u4e0e Spring \u7684\u4e8b\u52a1\u7ba1\u7406\u673a\u5236\u534f\u8c03\u5de5\u4f5c\uff0c<code>TransactionSynchronizationManager<\/code> \u5728\u9002\u5f53\u7684\u65f6\u5019\u56de\u8c03\u5176\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"java\">private static final class SqlSessionSynchronization extends TransactionSynchronizationAdapter {\n    private final SqlSessionHolder holder;\n    private final SqlSessionFactory sessionFactory;\n    private boolean holderActive = true;\n\n    public SqlSessionSynchronization(SqlSessionHolder holder, SqlSessionFactory sessionFactory) {\n        this.holder = holder;\n        this.sessionFactory = sessionFactory;\n    }\n\n    public int getOrder() {\n        return DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 1;\n    }\n\n    public void suspend() {\n        if (this.holderActive) {\n            TransactionSynchronizationManager.unbindResource(this.sessionFactory);\n        }\n    }\n\n    public void resume() {\n        if (this.holderActive) {\n            TransactionSynchronizationManager.bindResource(this.sessionFactory, this.holder);\n        }\n    }\n\n    public void beforeCommit(boolean readOnly) {\n        if (TransactionSynchronizationManager.isActualTransactionActive()) {\n            try {\n                this.holder.getSqlSession().commit();\n            } catch (PersistenceException p) {\n                if (this.holder.getPersistenceExceptionTranslator() != null) {\n                    DataAccessException translated = this.holder\n                        .getPersistenceExceptionTranslator()\n                        .translateExceptionIfPossible(p);\n                    if (translated != null) {\n                        throw translated;\n                    }\n                }\n                throw p;\n            }\n        }\n    }\n\n    public void beforeCompletion() {\n        if (!this.holder.isOpen()) {\n            TransactionSynchronizationManager.unbindResource(sessionFactory);\n            this.holderActive = false;\n\n            \/\/ \u771f\u6b63\u5173\u95ed\u6570\u636e\u5e93\u4f1a\u8bdd\n            this.holder.getSqlSession().close();\n        }\n    }\n\n    public void afterCompletion(int status) {\n        if (this.holderActive) {\n            TransactionSynchronizationManager.unbindResourceIfPossible(sessionFactory);\n            this.holderActive = false;\n\n            \/\/ \u771f\u6b63\u5173\u95ed\u6570\u636e\u5e93\u4f1a\u8bdd\n            this.holder.getSqlSession().close();\n        }\n        this.holder.reset();\n    }\n}\n<\/code><\/pre>\n<h2>3.3 \u521b\u5efa\u65b0\u4f1a\u8bdd<\/h2>\n<pre><code class=\"java\">\/\/ DefaultSqlSessionFactory\nprivate SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {\n    Transaction tx = null;\n    try {\n        final Environment environment = configuration.getEnvironment();\n\n        \/\/ \u83b7\u53d6\u4e8b\u52a1\u5de5\u5382\u5b9e\u73b0\n        final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);\n\n        tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);\n\n        final Executor executor = configuration.newExecutor(tx, execType);\n        return new DefaultSqlSession(configuration, executor, autoCommit);\n    } catch (Exception e) {\n        closeTransaction(tx); \/\/ may have fetched a connection so lets call close()\n        throw ExceptionFactory.wrapException(\"Error opening session.  Cause: \" + e, e);\n    } finally {\n        ErrorContext.instance().reset();\n    }\n}\n\nprivate TransactionFactory getTransactionFactoryFromEnvironment(Environment environment) {\n    if (environment == null || environment.getTransactionFactory() == null) {\n        return new ManagedTransactionFactory();\n    }\n    return environment.getTransactionFactory();\n}\n<\/code><\/pre>\n<h1>4. \u5c0f\u7ed3<\/h1>\n<ol>\n<li>MyBatis \u7684\u6838\u5fc3\u7ec4\u4ef6 Executor \u901a\u8fc7 Transaction \u63a5\u53e3\u6765\u8fdb\u884c\u4e8b\u52a1\u63a7\u5236\u3002<\/li>\n<li>\u4e0e Spring \u96c6\u6210\u65f6\uff0c\u521d\u59cb\u5316 Configuration \u65f6\u4f1a\u628a transactionFactory \u8bbe\u7f6e\u4e3a SpringManagedTransactionFactory \u7684\u5b9e\u4f8b\u3002<\/li>\n<li>\u6bcf\u4e2a Mapper \u4ee3\u7406\u91cc\u6ce8\u5165\u7684 SqlSession \u662f SqlSessionTemplate \u7684\u5b9e\u4f8b\uff0c\u5176\u5b9e\u73b0\u4e86 SqlSession \u63a5\u53e3\uff1b<\/li>\n<li>SqlSessionTemplate \u628a\u5bf9 SqlSession \u63a5\u53e3\u91cc\u58f0\u660e\u7684\u65b9\u6cd5\u8c03\u7528\u59d4\u6258\u7ed9\u5185\u90e8\u7684\u4e00\u4e2a\u52a8\u6001\u4ee3\u7406\uff0c\u8be5\u4ee3\u7406\u7684\u65b9\u6cd5\u5904\u7406\u5668\u4e3a\u5185\u90e8\u7c7b SqlSessionInterceptor \u3002<\/li>\n<li>SqlSessionInterceptor \u63a5\u6536\u5230\u65b9\u6cd5\u8c03\u7528\u65f6\uff0c\u901a\u8fc7 SqlSessionUtil \u8bbf\u95ee Spring \u7684\u4e8b\u52a1\u8bbe\u65bd\uff0c\u5982\u679c\u6709\u4e0e Spring \u5f53\u524d\u4e8b\u52a1\u5173\u8054\u7684 SqlSession \u5219\u590d\u7528\uff1b\u6ca1\u6709\u5219\u521b\u5efa\u4e00\u4e2a\u3002<\/li>\n<li>SqlSessionInterceptor \u6839\u636e Spring \u5f53\u524d\u4e8b\u52a1\u7684\u72b6\u6001\u6765\u51b3\u5b9a\u662f\u5426\u63d0\u4ea4\u6216\u56de\u6eda\u4e8b\u52a1\u3002\u4f1a\u8bdd\u7684\u771f\u6b63\u5173\u95ed\u662f\u901a\u8fc7\u6ce8\u518c\u5728 TransactionSynchronizationManager \u4e0a\u7684\u56de\u8c03\u94a9\u5b50\u5b9e\u73b0\u7684\u3002<\/li>\n<\/ol>\n<p><a href=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/10\/mybatis-mapper-call.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/10\/mybatis-mapper-call.png\" alt=\"\" width=\"643\" height=\"484\" class=\"alignnone size-full wp-image-2021\" \/><\/a><\/p>\n<p>\u5982\u4e0a\u56fe\uff1a<br \/>\n1. \u6b65\u9aa4 1 \u662f Spring AOP \u6dfb\u52a0\u7684\u5207\u9762\u7684\u6267\u884c\uff0c\u4e8b\u52a1\u662f\u5176\u4e2d\u4e00\u4e2a\u5207\u9762\u3002<br \/>\n2. \u6b65\u9aa4 2 \u662f Spring \u4e8b\u52a1\u7ba1\u7406\u673a\u5236\u7684\u90e8\u5206\u3002<br \/>\n3. \u6b65\u9aa4 3 \u662f\u4e1a\u52a1\u4ee3\u7801\uff0c\u8c03\u7528 Mapper \u4ee3\u7406\u4e0a\u7684\u65b9\u6cd5\u3002<br \/>\n4. \u6b65\u9aa4 4 \u662f\u4ee3\u7406\u4e0a\u7684\u65b9\u6cd5\u8c03\u7528\u88ab MapperProxy.invoke \u62e6\u622a\u3002<br \/>\n5. \u6b65\u9aa4 5\u30016 \u662f\u56e0\u4e3a MapperProxy \u6301\u6709\u7684 sqlSession \u662f SqlSessionTemplate\uff0c\u8c03\u7528\u5230 template \u4e0a\u7684\u65b9\u6cd5\uff0c\u53c8\u88ab\u8f6c\u53d1\u7ed9\u5185\u90e8\u7c7b SqlSessionInterceptor \uff0c\u8be5\u7c7b\u83b7\u5f97 Spring \u4e8b\u52a1\u7ba1\u7406\u6301\u6709\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u7528\u4ee5\u521b\u5efa Executor h\u548c DefaultSqlSession\u3002<br \/>\n6. \u6b65\u9aa4 7 \u7528 DefaultSqlSession \u53d1\u8d77\u6570\u636e\u5e93\u8c03\u7528\u3002<\/p>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u8fd0\u884c\u73af\u5883 Enviroment \u5f53 MyBatis \u4e0e\u4e0d\u540c\u7684\u5e94\u7528\u7ed3\u5408\u65f6\uff0c\u9700 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/framework\/20191025\/2002\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255],"tags":[228,208],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2002"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=2002"}],"version-history":[{"count":5,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2002\/revisions"}],"predecessor-version":[{"id":2023,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2002\/revisions\/2023"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=2002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=2002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=2002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}