{"id":16638,"date":"2024-11-20T10:02:32","date_gmt":"2024-11-20T02:02:32","guid":{"rendered":"https:\/\/92it.top\/?p=16638"},"modified":"2024-11-20T10:02:32","modified_gmt":"2024-11-20T02:02:32","slug":"mybatis%e6%98%af%e5%a6%82%e4%bd%95%e5%b7%a5%e4%bd%9c%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16638","title":{"rendered":"Mybatis\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247500583&amp;idx=1&amp;sn=bce07fbdbaf3a4f918d6ae2e3d13834f&amp;chksm=96c4ef1ba1b3660d9a2cf61c4b071029a690a625687deec0df0cbe03e7e47c51d1d472bce0bf&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">\u5efa\u884c2\u9762\uff1aMybatis\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f<\/a><\/p>\n\n\n\n<p>MyBatis \u662f\u4e00\u6b3e\u4f18\u79c0\u7684\u6301\u4e45\u5c42\u6846\u67b6\uff0c\u5b83\u901a\u8fc7\u7b80\u5316 JDBC\u64cd\u4f5c\u548c\u63d0\u4f9b\u7075\u6d3b\u7684 SQL\u6620\u5c04\u65b9\u5f0f\uff0c\u4f7f Java \u5f00\u53d1\u4eba\u5458\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u90a3\u4e48\uff0cMyBatis\u7684\u6267\u884c\u539f\u7406\u662f\u4ec0\u4e48\uff1f\u8fd9\u7bc7\u6587\u7ae0\u6211\u4eec\u5c06\u6df1\u5165\u5730\u5206\u6790\u3002<\/p>\n\n\n\n<p><strong>1. MyBatis \u914d\u7f6e\u89e3\u6790<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>MyBatis \u7684\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u5305\u62ec\u5168\u5c40\u914d\u7f6e\u6587\u4ef6\uff08mybatis-config.xml\uff09\u548c\u6620\u5c04\u6587\u4ef6\uff08XXXMapper.xml\uff09\u3002\u5168\u5c40\u914d\u7f6e\u6587\u4ef6\u4e3b\u8981\u7528\u4e8e\u914d\u7f6e\u6570\u636e\u6e90\u548c\u5176\u4ed6\u5168\u5c40\u6027\u7684\u4fe1\u606f\uff0c\u800c\u6620\u5c04\u6587\u4ef6\u5219\u7528\u4e8e\u5b9a\u4e49 SQL \u8bed\u53e5\u3002<\/p>\n\n\n\n<p><strong>1.1 \u5168\u5c40\u914d\u7f6e\u6587\u4ef6\u89e3\u6790<\/strong><\/p>\n\n\n\n<p>\u5168\u5c40\u914d\u7f6e\u6587\u4ef6\u5728 MyBatis \u542f\u52a8\u65f6\u88ab\u89e3\u6790\u3002<code>SqlSessionFactoryBuilder<\/code> \u662f MyBatis \u89e3\u6790\u914d\u7f6e\u6587\u4ef6\u7684\u5165\u53e3\u70b9\u3002\u5b83\u901a\u8fc7 <code>build<\/code> \u65b9\u6cd5\u63a5\u6536\u4e00\u4e2a Reader \u6216 InputStream\uff0c\u7136\u540e\u8c03\u7528 <code>XMLConfigBuilder<\/code> \u6765\u89e3\u6790 XML \u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {\n    try {\n        XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);\n        return build(parser.parse());\n    } catch (Exception e) {\n        throw ExceptionFactory.wrapException(\"Error building SqlSession.\", e);\n    } finally {\n        ErrorContext.instance().reset();\n        try {\n            inputStream.close();\n        } catch (IOException e) {\n            \/\/ Intentionally ignore. Prefer previous error.\n        }\n    }\n}<\/pre>\n\n\n\n<p><code>XMLConfigBuilder<\/code> \u89e3\u6790\u914d\u7f6e\u6587\u4ef6\u5e76\u6784\u5efa\u51fa <code>Configuration<\/code> \u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5305\u542b\u4e86 MyBatis \u7684\u6240\u6709\u914d\u7f6e\u4fe1\u606f\u3002<\/p>\n\n\n\n<p><strong>1.2 \u6620\u5c04\u6587\u4ef6\u89e3\u6790<\/strong><\/p>\n\n\n\n<p>\u6620\u5c04\u6587\u4ef6\u4e2d\u5b9a\u4e49\u4e86 SQL \u8bed\u53e5\uff0c\u901a\u8fc7 <code>XMLMapperBuilder<\/code> \u8fdb\u884c\u89e3\u6790\u3002\u6bcf\u4e2a <code>&lt;mapper><\/code> \u6807\u7b7e\u5bf9\u5e94\u4e00\u4e2a <code>MappedStatement<\/code> \u5bf9\u8c61\uff0c<code>MappedStatement<\/code> \u5305\u542b\u4e86 SQL \u8bed\u53e5\u3001\u8f93\u5165\u8f93\u51fa\u53c2\u6570\u7c7b\u578b\u3001\u7ed3\u679c\u96c6\u6620\u5c04\u7b49\u4fe1\u606f\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public void parse() {\n    if (!configuration.isResourceLoaded(resource)) {\n        configurationElement(parser.evalNode(\"\/mapper\"));\n        configuration.addLoadedResource(resource);\n        bindMapperForNamespace();\n    }\n    parsePendingResultMaps();\n    parsePendingCacheRefs();\n    parsePendingStatements();\n}<\/pre>\n\n\n\n<p><strong>2. SQL \u8bed\u53e5\u89e3\u6790<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>MyBatis \u652f\u6301\u52a8\u6001 SQL\uff0c\u901a\u8fc7 <code>&lt;if><\/code>, <code>&lt;choose><\/code>, <code>&lt;foreach><\/code> \u7b49\u6807\u7b7e\uff0c\u53ef\u4ee5\u6839\u636e\u4e0d\u540c\u7684\u6761\u4ef6\u6784\u9020 SQL\u3002\u52a8\u6001 SQL \u662f MyBatis \u7684\u4e00\u5927\u7279\u8272\uff0c\u901a\u8fc7 <code>SqlSource<\/code> \u63a5\u53e3\u5b9e\u73b0\u3002<code>SqlSource<\/code> \u7684\u4e3b\u8981\u5b9e\u73b0\u7c7b\u6709 <code>StaticSqlSource<\/code>, <code>DynamicSqlSource<\/code>, <code>RawSqlSource<\/code> \u7b49\u3002<\/p>\n\n\n\n<p><strong>2.1 \u52a8\u6001 SQL \u89e3\u6790<\/strong><\/p>\n\n\n\n<p><code>DynamicSqlSource<\/code> \u662f\u5904\u7406\u52a8\u6001 SQL \u7684\u6838\u5fc3\u7c7b\u3002\u5b83\u901a\u8fc7 <code>SqlNode<\/code> \u6811\u6765\u8868\u793a SQL \u8bed\u53e5\u7684\u7ed3\u6784\uff0c<code>SqlNode<\/code> \u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u5e38\u7528\u7684\u5b9e\u73b0\u7c7b\u6709 <code>IfSqlNode<\/code>, <code>ChooseSqlNode<\/code>, <code>WhereSqlNode<\/code> \u7b49\u3002\u6bcf\u4e2a <code>SqlNode<\/code> \u7684 <code>apply<\/code> \u65b9\u6cd5\u8d1f\u8d23\u5c06\u8282\u70b9\u8f6c\u6362\u4e3a SQL \u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class DynamicSqlSource implements SqlSource {\n    private final Configuration configuration;\n    private final SqlNode rootSqlNode;\n\n    public DynamicSqlSource(Configuration configuration, SqlNode rootSqlNode) {\n        this.configuration = configuration;\n        this.rootSqlNode = rootSqlNode;\n    }\n    \n    @Override\n    public BoundSql getBoundSql(Object parameterObject) {\n        DynamicContext context = new DynamicContext(configuration, parameterObject);\n        rootSqlNode.apply(context);\n        SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);\n        Class&lt;?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();\n        SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());\n        return sqlSource.getBoundSql(parameterObject);\n    }\n}<\/pre>\n\n\n\n<p><strong>3. \u53c2\u6570\u8bbe\u7f6e<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5728\u83b7\u5f97\u6700\u7ec8\u7684 SQL \u8bed\u53e5\u540e\uff0cMyBatis \u9700\u8981\u5c06\u53c2\u6570\u4f20\u9012\u7ed9 SQL \u8bed\u53e5\u3002<code>ParameterHandler<\/code> \u63a5\u53e3\u8d1f\u8d23\u8fd9\u9879\u5de5\u4f5c\uff0c\u9ed8\u8ba4\u5b9e\u73b0\u662f <code>DefaultParameterHandler<\/code>\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class DefaultParameterHandler implements ParameterHandler {\n    private final TypeHandlerRegistry typeHandlerRegistry;\n    private final MappedStatement mappedStatement;\n    private final Object parameterObject;\n    private final BoundSql boundSql;\n    private final Configuration configuration;\n\n    public DefaultParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {\n        this.mappedStatement = mappedStatement;\n        this.configuration = mappedStatement.getConfiguration();\n        this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();\n        this.parameterObject = parameterObject;\n        this.boundSql = boundSql;\n    }\n\n    @Override\n    public void setParameters(PreparedStatement ps) throws SQLException {\n        ErrorContext.instance().activity(\"setting parameters\").object(mappedStatement.getParameterMap().getId());\n        List&lt;ParameterMapping> parameterMappings = boundSql.getParameterMappings();\n        if (parameterMappings != null) {\n            for (int i = 0; i &lt; parameterMappings.size(); i++) {\n                ParameterMapping parameterMapping = parameterMappings.get(i);\n                if (parameterMapping.getMode() != ParameterMode.OUT) {\n                    Object value;\n                    String propertyName = parameterMapping.getProperty();\n                    if (boundSql.hasAdditionalParameter(propertyName)) {\n                        value = boundSql.getAdditionalParameter(propertyName);\n                    } else if (parameterObject == null) {\n                        value = null;\n                    } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {\n                        value = parameterObject;\n                    } else {\n                        MetaObject metaObject = configuration.newMetaObject(parameterObject);\n                        value = metaObject.getValue(propertyName);\n                    }\n                    TypeHandler typeHandler = parameterMapping.getTypeHandler();\n                    JdbcType jdbcType = parameterMapping.getJdbcType();\n                    if (value == null &amp;&amp; jdbcType == null) {\n                        jdbcType = configuration.getJdbcTypeForNull();\n                    }\n                    typeHandler.setParameter(ps, i + 1, value, jdbcType);\n                }\n            }\n        }\n    }\n}<\/pre>\n\n\n\n<p><strong>4. SQL \u6267\u884c<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>SQL \u6267\u884c\u662f MyBatis \u7684\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00\u3002<code>Executor<\/code> \u63a5\u53e3\u5b9a\u4e49\u4e86\u6267\u884c\u64cd\u4f5c\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u4e3b\u8981\u7684\u5b9e\u73b0\u7c7b\u6709 <code>SimpleExecutor<\/code>, <code>ReuseExecutor<\/code>, <code>BatchExecutor<\/code>\u3002\u8fd9\u4e9b\u6267\u884c\u5668\u901a\u8fc7 <code>StatementHandler<\/code> \u6267\u884c SQL \u8bed\u53e5\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class SimpleExecutor extends BaseExecutor {\n    public SimpleExecutor(Configuration configuration, Transaction transaction) {\n        super(configuration, transaction);\n    }\n\n    @Override\n    public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {\n        Statement stmt = null;\n        try {\n            Configuration configuration = ms.getConfiguration();\n            StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);\n            stmt = prepareStatement(handler, ms.getStatementLog());\n            return handler.update(stmt);\n        } finally {\n            closeStatement(stmt);\n        }\n    }\n}<\/pre>\n\n\n\n<p><strong>5. \u7ed3\u679c\u5904\u7406<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>MyBatis \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u7ed3\u679c\u96c6\u6620\u5c04\u529f\u80fd\uff0c\u5141\u8bb8\u5c06 SQL \u67e5\u8be2\u7ed3\u679c\u6620\u5c04\u4e3a Java \u5bf9\u8c61\u3002<code>ResultSetHandler<\/code> \u63a5\u53e3\u8d1f\u8d23\u5904\u7406\u7ed3\u679c\u96c6\uff0c<code>DefaultResultSetHandler<\/code> \u662f\u5176\u4e3b\u8981\u5b9e\u73b0\u7c7b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class DefaultResultSetHandler implements ResultSetHandler {\n    private final TypeHandlerRegistry typeHandlerRegistry;\n    private final ObjectFactory objectFactory;\n    private final boolean useConstructorMappings;\n    private final ReflectorFactory reflectorFactory;\n    private final MappedStatement mappedStatement;\n    private final RowBounds rowBounds;\n    private final ParameterHandler parameterHandler;\n    private final ResultHandler&lt;?> resultHandler;\n    private final BoundSql boundSql;\n\n    public DefaultResultSetHandler(Executor executor, MappedStatement mappedStatement, ParameterHandler parameterHandler, ResultHandler&lt;?> resultHandler, BoundSql boundSql, RowBounds rowBounds) {\n        this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();\n        this.objectFactory = mappedStatement.getConfiguration().getObjectFactory();\n        this.useConstructorMappings = mappedStatement.getConfiguration().isUseConstructorMappings();\n        this.reflectorFactory = mappedStatement.getConfiguration().getReflectorFactory();\n        this.mappedStatement = mappedStatement;\n        this.rowBounds = rowBounds;\n        this.parameterHandler = parameterHandler;\n        this.resultHandler = resultHandler;\n        this.boundSql = boundSql;\n    }\n\n    @Override\n    public List&lt;Object> handleResultSets(Statement stmt) throws SQLException {\n        final List&lt;Object> multipleResults = new ArrayList&lt;>();\n        int resultSetCount = 0;\n        ResultSetWrapper rsw = getFirstResultSet(stmt);\n        List&lt;ResultMap> resultMaps = mappedStatement.getResultMaps();\n        int resultMapCount = resultMaps.size();\n        validateResultMapsCount(rsw, resultMapCount);\n        while (rsw != null &amp;&amp; resultMapCount > resultSetCount) {\n            ResultMap resultMap = resultMaps.get(resultSetCount);\n            handleResultSet(rsw, resultMap, multipleResults, null);\n            rsw = getNextResultSet(stmt);\n            cleanUpAfterHandlingResultSet();\n            resultSetCount++;\n        }\n        return collapseSingleResultList(multipleResults);\n    }\n}<\/pre>\n\n\n\n<p><strong>6. \u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u672c\u6587\uff0c\u6211\u4eec\u901a\u8fc7\u6838\u5fc3\u6e90\u7801\u5206\u6790\u4e86 MyBatis\uff0c \u5b83\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684 ORM\u6846\u67b6\uff0c\u5b83\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u548c\u6ce8\u89e3\u5c06 Java \u5bf9\u8c61\u4e0e\u6570\u636e\u5e93\u8bb0\u5f55\u6620\u5c04\u8d77\u6765\uff0c\u5176\u6838\u5fc3\u5728\u4e8e\u901a\u8fc7 XML\u548c\u6ce8\u89e3\u914d\u7f6e SQL\u8bed\u53e5\uff0c\u5229\u7528\u6267\u884c\u5668\u6267\u884c SQL\uff0c\u5e76\u901a\u8fc7\u7ed3\u679c\u96c6\u5904\u7406\u5668\u5c06\u7ed3\u679c\u6620\u5c04\u4e3a Java\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>MyBatis\u7684\u8bbe\u8ba1\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u4e13\u6ce8\u4e8e SQL\u672c\u8eab\uff0c\u800c\u4e0d\u5fc5\u5173\u5fc3\u5e95\u5c42 JDBC\u64cd\u4f5c\u7684\u7ec6\u8282\uff0c\u4e86\u89e3\u548c\u638c\u63e1\u5176\u6267\u884c\u539f\u7406\u548c\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ef\u4ee5\u5e2e\u7ec4\u6211\u4eec\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u66f4\u597d\u5730\u4f7f\u7528 MyBatis\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1a\u5efa\u884c2\u9762\uff1aMybatis\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f MyBatis \u662f\u4e00\u6b3e\u4f18\u79c0\u7684\u6301\u4e45\u5c42\u6846\u67b6\uff0c\u5b83\u901a\u8fc7\u7b80\u5316 JDBC\u64cd\u4f5c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,23],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16638"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=16638"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16638\/revisions"}],"predecessor-version":[{"id":16639,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16638\/revisions\/16639"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}