{"id":2015,"date":"2019-10-26T08:06:32","date_gmt":"2019-10-26T00:06:32","guid":{"rendered":"https:\/\/coderbee.net\/?p=2015"},"modified":"2019-10-26T07:36:49","modified_gmt":"2019-10-25T23:36:49","slug":"mybatis-mapper-%e4%bb%a3%e7%90%86%e5%ae%9e%e7%8e%b0%e6%95%b0%e6%8d%ae%e5%ba%93%e8%b0%83%e7%94%a8%e5%8e%9f%e7%90%86","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/framework\/20191026\/2015","title":{"rendered":"MyBatis Mapper \u4ee3\u7406\u5b9e\u73b0\u6570\u636e\u5e93\u8c03\u7528\u539f\u7406"},"content":{"rendered":"<h1>1. Mapper \u4ee3\u7406\u5c42\u6267\u884c<\/h1>\n<p>Mapper \u4ee3\u7406\u4e0a\u6267\u884c\u65b9\u6cd5\u8c03\u7528\u65f6\uff0c\u8c03\u7528\u88ab\u59d4\u6d3e\u7ed9 MapperProxy \u6765\u5904\u7406\u3002<\/p>\n<pre><code class=\"java\">public class MapperProxy&lt;T&gt; implements InvocationHandler, Serializable {\n    private final SqlSession sqlSession;\n    private final Class&lt;T&gt; mapperInterface;\n    private final Map&lt;Method, MapperMethod&gt; methodCache;\n\n    public MapperProxy(SqlSession sqlSession, Class&lt;T&gt; mapperInterface, Map&lt;Method, MapperMethod&gt; methodCache) {\n        this.sqlSession = sqlSession;\n        this.mapperInterface = mapperInterface;\n        this.methodCache = methodCache;\n    }\n\n    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\n        if (Object.class.equals(method.getDeclaringClass())) {\n            try {\n                return method.invoke(this, args);\n            } catch (Throwable t) {\n                throw ExceptionUtil.unwrapThrowable(t);\n            }\n        }\n        \/\/ \u63a5\u53e3\u91cc\u58f0\u660e\u7684\u65b9\u6cd5\uff0c\u8f6c\u6362\u4e3a MapperMethod \u6765\u8c03\u7528\n        final MapperMethod mapperMethod = cachedMapperMethod(method);\n\n        \/\/ \u4e0e Spring \u96c6\u6210\u65f6\u6b64\u5904\u7684 sqlSession \u4ecd\u7136 SqlSessionTemplate\n        return mapperMethod.execute(sqlSession, args);\n    }\n\n    private MapperMethod cachedMapperMethod(Method method) {\n        MapperMethod mapperMethod = methodCache.get(method);\n        if (mapperMethod == null) {\n            mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());\n            methodCache.put(method, mapperMethod);\n        }\n        return mapperMethod;\n    }\n}\n<\/code><\/pre>\n<p>MapperMethod \u6839\u636e <code>mapperInterface.getName() + \".\" + method.getName()<\/code> \u4ece Configuration \u5bf9\u8c61\u91cc\u627e\u5230\u5bf9\u5e94\u7684 <code>MappedStatement<\/code>\uff0c\u4ece\u800c\u5f97\u5230\u8981\u6267\u884c\u7684 SQL \u64cd\u4f5c\u7c7b\u578b\uff08insert\/delete\/update\/select\/flush\uff09\uff0c\u7136\u540e\u8c03\u7528\u4f20\u5165\u7684 sqlSession \u5b9e\u4f8b\u4e0a\u7684\u76f8\u5e94\u7684\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"java\">public Object execute(SqlSession sqlSession, Object[] args) {\n    Object result;\n    if (SqlCommandType.INSERT == command.getType()) {\n        \/\/ \u628a\u53c2\u6570\u8f6c\u6362\u4e3a SqlSession \u80fd\u5904\u7406\u7684\u683c\u5f0f\n        Object param = method.convertArgsToSqlCommandParam(args);\n\n        \/\/ \u5728 sqlSession \u4e0a\u6267\u884c\u5e76\u5904\u7406\u7ed3\u679c\n        result = rowCountResult(sqlSession.insert(command.getName(), param));\n    } else if (SqlCommandType.UPDATE == command.getType()) {\n        Object param = method.convertArgsToSqlCommandParam(args);\n        result = rowCountResult(sqlSession.update(command.getName(), param));\n    ...\u7701\u7565\n\n<\/code><\/pre>\n<p>\u5982\u679c\u4e0a\u8ff0\u65b9\u6cd5\u4f20\u5165\u7684\u662f <code>SqlSessionTemplate<\/code>\uff0c\u90a3\u4e48\u8fd9\u4e9b\u65b9\u6cd5\u8c03\u7528\u4f1a\u88ab <code>SqlSessionInterceptor<\/code> \u62e6\u622a\uff0c\u52a0\u5165\u4e0e Spring \u4e8b\u52a1\u7ba1\u7406\u673a\u5236\u534f\u4f5c\u7684\u903b\u8f91\uff0c\u5177\u4f53\u53ef\u4ee5\u770b\u8fd9\u7bc7\u6587\u7ae0<a href=\"https:\/\/coderbee.net\/?p=2002\">MyBatis \u4e8b\u52a1\u7ba1\u7406<\/a>\uff0c\u8fd9\u91cc\u4e0d\u518d\u5c55\u5f00\uff0c\u6700\u7ec8\u4f1a\u8c03\u7528\u5230 <code>DefaultSqlSession<\/code> \u5b9e\u4f8b\u4e0a\u7684\u65b9\u6cd5\u3002<\/p>\n<h1>2. \u4f1a\u8bdd\u5c42\u7684\u6267\u884c\u8fc7\u7a0b<\/h1>\n<p>SqlSession \u91cc\u58f0\u660e\u7684\u6240\u6709\u65b9\u6cd5\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u5982\u679c\u662f <code>String statement<\/code>\uff0c\u5219\u90fd\u662f <code>mapperInterface.getName() + \".\" + method.getName()<\/code>\uff0c\u8868\u793a\u8981\u8c03\u7528\u7684 SQL \u8bed\u53e5\u7684\u6807\u8bc6\u7b26\u3002\u901a\u8fc7\u5b83\u4ece configuration \u627e\u5230 <code>MappedStatement<\/code> \u3002<\/p>\n<p>\u4f1a\u8bdd\u5c42\u6700\u4e3b\u8981\u7684\u903b\u8f91\u662f\u8fdb\u884c\u53c2\u6570\u7684\u5305\u88c5\uff0c\u83b7\u53d6\u5bf9\u5e94\u7684 <code>MappedStatement<\/code>\uff0c\u7136\u540e\u8c03\u7528\u6301\u6709\u7684 <code>Executor<\/code> \u7684\u65b9\u6cd5\u53bb\u6267\u884c\u3002<\/p>\n<p><!--more--><\/p>\n<pre><code class=\"java\">public class DefaultSqlSession implements SqlSession {\n    private Configuration configuration;\n    private Executor executor;\n\n    private boolean autoCommit;\n    private boolean dirty;\n    private List&lt;Cursor&lt;?&gt;&gt; cursorList;\n\n    public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {\n        this.configuration = configuration;\n        this.executor = executor;\n        this.dirty = false;\n        this.autoCommit = autoCommit;\n    }\n\n    public &lt;E&gt; List&lt;E&gt; selectList(String statement, Object parameter, RowBounds rowBounds) {\n        try {\n            MappedStatement ms = configuration.getMappedStatement(statement);\n            return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);\n        } catch (Exception e) {\n            throw ExceptionFactory.wrapException(\"Error querying database.  Cause: \" + e, e);\n        } finally {\n            ErrorContext.instance().reset();\n        }\n    }\n<\/code><\/pre>\n<h1>3. <code>Executor<\/code> \u6267\u884c\u7684\u8fc7\u7a0b<\/h1>\n<p>\u6211\u4eec\u77e5\u9053 JDBC \u91cc\u6709\u4e09\u79cd\u6570\u636e\u5e93\u8bed\u53e5\uff1a<code>java.sql.Statement\/PreparedStatement\/CallableStatement<\/code>\uff0c\u6bcf\u79cd\u8bed\u53e5\u7684\u6267\u884c\u65b9\u5f0f\u662f\u4e0d\u4e00\u6837\u7684\uff0cMyBatis \u521b\u5efa\u4e86 <code>StatementHandler<\/code> \u62bd\u8c61\u6765\u8868\u793a\u6570\u636e\u5e93\u8bed\u53e5\u7684\u5904\u7406\u903b\u8f91\uff0c\u6709\u5bf9\u5e94\u7684\u4e09\u79cd\u5177\u4f53\u5b9e\u73b0\uff1a<code>SimpleStatementHandler\/PreparedStatementHandler\/CallableStatementHandler<\/code>\u3002<\/p>\n<p><code>RoutingStatementHandler<\/code> \u662f\u4e2a\u95e8\u9762\u6a21\u5f0f\uff0c\u6784\u5efa\u65f6\u6839\u636e\u8981\u6267\u884c\u7684\u6570\u636e\u5e93\u8bed\u53e5\u7c7b\u578b\u5b9e\u4f8b\u5316 <code>SimpleStatementHandler\/PreparedStatementHandler\/CallableStatementHandler<\/code> \u4e2d\u7684\u4e00\u4e2a\u7c7b\u4f5c\u4e3a\u76ee\u6807 delegate\uff0c\u5e76\u628a\u8c03\u7528\u90fd\u8f6c\u7ed9\u8fd9\u4e2a delegate \u7684\u65b9\u6cd5\u3002<\/p>\n<p>\u4e0d\u540c\u7684 handler \u5b9e\u73b0\u5b9e\u73b0\u4e86\u5bf9\u5e94\u7684\uff1a\u6570\u636e\u5e93\u8bed\u53e5\u7684\u521b\u5efa\u3001\u53c2\u6570\u5316\u8bbe\u7f6e\u3001\u6267\u884c\u8bed\u53e5\u3002<\/p>\n<p>\u901a\u8fc7\u8fd9\u5c42\u62bd\u8c61\uff0cMyBatis \u7edf\u4e00\u4e86 Executor \u91cc\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u4ee5\u4e0b\u4ee5 SimpleExecutor \u7684\u6d41\u7a0b\u4e3a\u4f8b\uff1a<br \/>\n1. \u5bf9\u4e8e\u4f20\u5165\u7684 <code>MappedStatement ms<\/code>\uff0c\u5f97\u5230 <code>Configuration configuration<\/code>\u3002<br \/>\n2. <code>configuration<\/code> \u901a\u8fc7 <code>ms<\/code> \u7684\u8bed\u53e5\u7c7b\u578b\u5f97\u5230\u4e00\u4e2a <code>RoutingStatementHandler<\/code> \u7684\u5b9e\u4f8b\uff08\u5185\u90e8\u6709\u4e2a delegate \u53ef\u4ee5\u59d4\u6d3e\uff09 <code>handler<\/code>\uff0c\u5e76\u7528 <code>InterceptorChain<\/code> \u5bf9 <code>handler<\/code> \u5b9e\u4f8b\u8fdb\u884c\u88c5\u9970\u3002<br \/>\n3. \u901a\u8fc7 <code>SimpleExecutor<\/code> \u6301\u6709\u7684 <code>Transaction<\/code> \u5b9e\u4f8b\u83b7\u53d6\u5bf9\u5e94\u7684\u6570\u636e\u5e93\u8fde\u63a5 connection\u3002<br \/>\n4. <code>handler<\/code> \u901a\u8fc7\u6570\u636e\u5e93\u8fde\u63a5\u521d\u59cb\u5316\u6570\u636e\u5e93\u8bed\u53e5 <code>java.sql.Statement<\/code>\u6216\u5176\u5b50\u7c7b <code>stmt<\/code>\uff0c\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\u548c fetchSize \u3002<br \/>\n5. \u7528 <code>handler<\/code> \u5bf9 <code>stmt<\/code> \u8fdb\u884c\u53c2\u6570\u5316\u5904\u7406\uff08\u6bd4\u5982 <code>PreparedStatement<\/code> \u8bbe\u7f6e\u9884\u7f16\u8bd1\u8bed\u53e5\u7684\u53c2\u6570\u503c\uff09\u3002<br \/>\n6. <code>handler<\/code> \u6267\u884c\u76f8\u5e94\u7684 <code>stmt<\/code> \u5b8c\u6210\u6570\u636e\u5e93\u64cd\u4f5c\u3002<br \/>\n7. \u7528 <code>ResultSetHandler<\/code> \u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5904\u7406\u3002<code>ResultSetHandler<\/code> \u4f1a\u8c03\u7528 <code>TypeHandler<\/code> \u6765\u8fdb\u884c Java \u7c7b\u578b\u4e0e\u6570\u636e\u5e93\u5217\u7c7b\u578b\u4e4b\u95f4\u8f6c\u6362\u3002<\/p>\n<pre><code class=\"java\">\/\/ SimpleExecutor\npublic &lt;E&gt; List&lt;E&gt; doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {\n    Statement stmt = null;\n    try {\n        Configuration configuration = ms.getConfiguration();\n\n        \/\/ \u521b\u5efa handler \u6765\u8d1f\u8d23\u5177\u4f53\u7684\u6267\u884c\n        StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);\n\n        \/\/ \u521b\u5efa\u6570\u636e\u5e93\u8bed\u53e5\n        stmt = prepareStatement(handler, ms.getStatementLog());\n\n        \/\/ \u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\n        return handler.&lt;E&gt;query(stmt, resultHandler);\n    } finally {\n        closeStatement(stmt);\n    }\n}\n\n\/\/ Configuration\npublic StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {\n    StatementHandler statementHandler = new RoutingStatementHandler(executor, mappedStatement, parameterObject, rowBounds, resultHandler, boundSql);\n    statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);\n    return statementHandler;\n}\n\n\/\/ RoutingStatementHandler\npublic RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {\n    \/\/ \u6839\u636eSQL\u8bed\u53e5\u7684\u6267\u884c\u65b9\u5f0f\u521b\u5efa\u5bf9\u5e94\u7684 handler \u5b9e\u4f8b\n    switch (ms.getStatementType()) {\n      case STATEMENT:\n        delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);\n        break;\n      case PREPARED:\n        delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);\n        break;\n      case CALLABLE:\n        delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);\n        break;\n      default:\n        throw new ExecutorException(\"Unknown statement type: \" + ms.getStatementType());\n    }\n}\n\nprivate Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {\n    \/\/ \u521b\u5efa\u6570\u636e\u5e93\u8fde\u63a5\n    Connection connection = getConnection(statementLog);\n\n    \/\/ \u521b\u5efa\u6570\u636e\u5e93\u8bed\u53e5\n    Statement stmt = handler.prepare(connection, transaction.getTimeout());\n\n    \/\/ \u53c2\u6570\u5316\u8bbe\u7f6e\n    handler.parameterize(stmt);\n    return stmt;\n}\n\nprotected Connection getConnection(Log statementLog) throws SQLException {\n    Connection connection = transaction.getConnection();\n    if (statementLog.isDebugEnabled()) {\n        return ConnectionLogger.newInstance(connection, statementLog, queryStack);\n    } else {\n        return connection;\n    }\n}\n\n\/\/ BaseStatementHandler\npublic Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {\n    ErrorContext.instance().sql(boundSql.getSql());\n    Statement statement = null;\n    try {\n        \/\/ \u7531\u5177\u4f53\u7684\u5b50\u7c7b\u6765\u521b\u5efa\u5bf9\u5e94\u7684 Statement \u5b9e\u4f8b\n        statement = instantiateStatement(connection);\n\n        \/\/ \u901a\u7528\u53c2\u6570\u8bbe\u7f6e\n        setStatementTimeout(statement, transactionTimeout);\n        setFetchSize(statement);\n        return statement;\n    } catch (SQLException e) {\n        closeStatement(statement);\n        throw e;\n    } catch (Exception e) {\n        closeStatement(statement);\n        throw new ExecutorException(\"Error preparing statement.  Cause: \" + e, e);\n    }\n}\n\n\/\/ PreparedStatementHandler\nprotected Statement instantiateStatement(Connection connection) throws SQLException {\n    String sql = boundSql.getSql();\n    if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) {\n        String[] keyColumnNames = mappedStatement.getKeyColumns();\n        if (keyColumnNames == null) {\n            return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);\n        } else {\n            return connection.prepareStatement(sql, keyColumnNames);\n        }\n    } else if (mappedStatement.getResultSetType() != null) {\n        return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);\n    } else {\n        return connection.prepareStatement(sql);\n    }\n}\n\n\/\/ PreparedStatementHandler\npublic void parameterize(Statement statement) throws SQLException {\n    parameterHandler.setParameters((PreparedStatement) statement);\n}\n<\/code><\/pre>\n<h1>4. \u95ee\u9898<\/h1>\n<ol>\n<li>\n<p>\u53ea\u5728 XML \u91cc\u5b9a\u4e49 SQL\u3001\u6ca1\u6709\u5bf9\u5e94\u7684 Java \u63a5\u53e3\u7c7b\u80fd\u5426\u4f7f\u7528 MyBatis \uff1f<br \/>\n\u7b54\uff1a\u53ef\u4ee5\uff0c\u901a\u8fc7 SqlSession \u7684\u65b9\u6cd5\u6765\u8c03\u7528 XML \u91cc\u7684 SQL \u8bed\u53e5\u3002<\/p>\n<\/li>\n<li>\n<p>Mapper \u63a5\u53e3\u7c7b\u91cc\u53ef\u4ee5\u8fdb\u884c\u65b9\u6cd5\u91cd\u8f7d\u5417\uff1f<br \/>\n\u7b54\uff1a\u4e0d\u80fd\uff0c\u56e0\u4e3a MyBatis \u91cc\u6839\u636e \u7c7b\u540d + &#8220;.&#8221; + \u65b9\u6cd5\u540d \u6765\u67e5\u627e SQL \u8bed\u53e5\uff0c\u91cd\u8f7d\u4f1a\u5bfc\u81f4\u8fd9\u6837\u7684\u7ec4\u5408\u51fa\u73b0\u591a\u6761\u7ed3\u679c\u3002<\/p>\n<\/li>\n<\/ol>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong> \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. Mapper \u4ee3\u7406\u5c42\u6267\u884c Mapper \u4ee3\u7406\u4e0a\u6267\u884c\u65b9\u6cd5\u8c03\u7528\u65f6\uff0c\u8c03\u7528\u88ab\u59d4\u6d3e\u7ed9 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/framework\/20191026\/2015\">\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],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2015"}],"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=2015"}],"version-history":[{"count":4,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2015\/revisions"}],"predecessor-version":[{"id":2020,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2015\/revisions\/2020"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=2015"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=2015"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=2015"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}