{"id":2006,"date":"2019-10-25T18:44:11","date_gmt":"2019-10-25T10:44:11","guid":{"rendered":"https:\/\/coderbee.net\/?p=2006"},"modified":"2019-10-25T18:44:11","modified_gmt":"2019-10-25T10:44:11","slug":"mybatis-mapper-%e4%bb%a3%e7%90%86%e5%88%9b%e5%bb%ba%e8%bf%87%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/framework\/20191025\/2006","title":{"rendered":"MyBatis Mapper \u4ee3\u7406\u521b\u5efa\u8fc7\u7a0b"},"content":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u5173\u6ce8\u4e0e SpringBoot \u96c6\u6210\u65f6\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\u3002<\/p>\n<h1>1. \u6838\u5fc3\u7ec4\u4ef6<\/h1>\n<ul>\n<li>Configuration\uff1aMyBatis\u6240\u6709\u7684\u914d\u7f6e\u4fe1\u606f\u90fd\u4fdd\u5b58\u5728Configuration\u5bf9\u8c61\u4e4b\u4e2d\uff0c\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u5927\u90e8\u5206\u914d\u7f6e\u90fd\u4f1a\u5b58\u50a8\u5230\u8be5\u7c7b\u4e2d\u3002<\/li>\n<li>SqlSession\uff1aMyBatis \u7684\u9876\u5c42 API\uff0c\u8868\u793a\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u4f1a\u8bdd\uff0c\u5b8c\u6210\u6570\u636e\u5e93\u589e\u5220\u6539\u67e5\u64cd\u4f5c\u3002<\/li>\n<li>Executor\uff1a\u6267\u884c\u5668\u662f MyBatis \u8c03\u5ea6\u7684\u6838\u5fc3\uff0c\u8d1f\u8d23 SQL \u8bed\u53e5\u7684\u751f\u6210\u548c\u67e5\u8be2\u7f13\u5b58\u7684\u7ef4\u62a4\u3002<\/li>\n<li>StatementHandler\uff1a\u5c01\u88c5\u4e86 JDBC Statement \u64cd\u4f5c\uff0c\u5982\u8bbe\u7f6e\u53c2\u6570\u7b49\u3002<\/li>\n<li>ParameterHandler\uff1a\u8d1f\u8d23\u5c06\u7528\u6237\u4f20\u9012\u7684\u53c2\u6570\u8f6c\u6362\u6210 JDBC Statement \u6240\u5bf9\u5e94\u7684\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li>ResultSetHandler\uff1a\u8d1f\u8d23\u5c06 JDBC \u8fd4\u56de\u7684 ResultSet \u7ed3\u679c\u96c6\u8f6c\u6362\u6210 List \u7c7b\u578b\u7684\u96c6\u5408\u3002<\/li>\n<li>TypeHandler\uff1a\u8d1f\u8d23\u5c06 Java \u6570\u636e\u7c7b\u578b\u548c jdbc \u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u6620\u5c04\u4e0e\u8f6c\u6362\u3002<\/li>\n<\/ul>\n<hr \/>\n<ul>\n<li>MapperFactoryBean\uff1a\u4e0e Spring \u96c6\u6210\u65f6\u8868\u793a\u4e00\u4e2a Mapper \u539f\u578b\u7684\u5de5\u5382 bean\uff0c\u7528\u4ee5\u521b\u5efa\u6700\u7ec8\u7684\u4ee3\u7406\u3002<\/li>\n<li>MapperProxy\uff1a\u4e0e\u4e00\u4e2a mapperInterface \u5bf9\u5e94\uff0c\u7ef4\u62a4\u4e86 Map&lt;Method, MapperMethod> \u6620\u5c04\u3002<\/li>\n<li>MapperAnnotationBuilder\uff1a\u57fa\u4e8e\u6ce8\u89e3\u9a71\u52a8\u7684 MappedStatement \u89e3\u6790\u5668\uff0c\u89e3\u6790\u63a5\u53e3\u7c7b\u7684\u6bcf\u4e2a\u65b9\u6cd5\uff0c\u5c01\u88c5\u6210 MappedStatement\u3002<\/li>\n<li>MappedStatement\uff1a\u7ef4\u62a4\u4e86\u4e00\u6761 &lt;select|update|delete|insert> \u8282\u70b9\u7684\u5c01\u88c5\u3002<\/li>\n<li>SqlSource\uff1a\u8d1f\u8d23\u6839\u636e\u7528\u6237\u4f20\u9012\u7684 parameterObject \u52a8\u6001\u751f\u6210 SQL \u8bed\u53e5\uff0c\u5c06\u4fe1\u606f\u5c01\u88c5\u6210 BoundSql \u5bf9\u8c61\u3002<\/li>\n<li>BoundSql\uff1a\u8868\u793a\u52a8\u6001\u751f\u6210\u7684 SQL \u8bed\u53e5\u4ee5\u53ca\u76f8\u5e94\u7684\u53c2\u6570\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p>\u6574\u4f53\u67b6\u6784\u5982\u4e0b\u56fe\uff1a<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"390\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/10\/mybatis-arch.png\" alt=\"mybatis-\u6574\u4f53\u67b6\u6784\" class=\"alignnone size-full wp-image-2008\" \/><\/p>\n<p><!--more--><\/p>\n<h1>2. \u4e0e SpringBoot \u96c6\u6210\u7684\u521d\u59cb\u5316<\/h1>\n<h2>2.1 \u6982\u89c8<\/h2>\n<p>SpringBoot \u81ea\u52a8\u7ec4\u88c5 MyBatis \u7684\u5c5e\u6027\uff08\u524d\u7f00\u4e3a &#8220;mybatis&#8221;\uff09\u914d\u7f6e\u6210 <code>MybatisProperties<\/code> \u5bf9\u8c61\u3002<\/p>\n<p>SpringBoot \u7684\u81ea\u52a8\u914d\u7f6e\u673a\u5236\u4f1a\u89e3\u6790\u914d\u7f6e\u7c7b <code>MybatisAutoConfiguration<\/code>\uff0c\u5176\u5185\u90e8\u65b9\u6cd5\u5b9a\u4e49\u4e86\u4e24\u4e2a Bean \u3002<\/p>\n<pre><code class=\"java\">@Bean\n@ConditionalOnMissingBean\npublic SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {...}\n\n@Bean\n@ConditionalOnMissingBean\npublic SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {...}\n<\/code><\/pre>\n<p>\u5728 <code>sqlSessionFactory(dataSource)<\/code> \u65b9\u6cd5\u91cc\u5229\u7528 <code>MybatisProperties<\/code> \u521d\u59cb\u5316 <code>SqlSessionFactoryBean<\/code>\uff0c\u901a\u8fc7 <code>SqlSessionFactoryBean.buildSqlSessionFactory()<\/code> \u6765\u521b\u5efa <code>Configuration<\/code>\uff0c\u8be5\u65b9\u6cd5\u7684\u4e3b\u8981\u903b\u8f91\u662f\u521b\u5efa\u5e76\u914d\u7f6e <code>Configuration<\/code>\uff0c\u7136\u540e\u7528 <code>Configuration<\/code> \u521b\u5efa <code>SqlSessionFactory<\/code>\u3002<\/p>\n<p>\u518d\u7528 <code>SqlSessionFactory<\/code> \u521b\u5efa <code>SqlSessionTemplate<\/code>\u3002<\/p>\n<p>SpringBoot \u4f1a\u89e3\u6790\u5904\u7406\u914d\u7f6e\u7c7b\u4e0a\u7684 <code>@MapperScan(\"package.to.scan\")<\/code> \u6ce8\u89e3\uff0c\u8c03\u7528\u8be5\u6ce8\u89e3\u4e0a\u7684\u5bfc\u5165\u7c7b <code>MapperScannerRegistrar<\/code>\uff1b <code>MapperScannerRegistrar.registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry)<\/code> \u65b9\u6cd5\u521b\u5efa\u4e00\u4e2a <code>ClassPathMapperScanner<\/code>\uff0c\u5229\u7528 <code>@MapperScan<\/code> \u4e0a\u7684\u4fe1\u606f\u521d\u59cb\u5316\u8fd9\u4e2a\u626b\u63cf\u5668\uff0c\u4ee5\u9650\u5b9a\u626b\u63cf\u7684\u63a5\u53e3\u8303\u56f4\uff0c\u5bf9\u4e8e\u626b\u63cf\u5230\u7684\u6bcf\u4e2a bean \u5b9a\u4e49\uff0c\u8bbe\u7f6e\u5176 <code>beanClass<\/code> \u4e3a <code>MapperFactoryBean<\/code>\uff0c\u5982\u679c\u6709\u6307\u5b9a <code>sqlSessionFactory\/sqlSessionTemplate<\/code> bean \u7684\u5f15\u7528\u5219\u76f4\u63a5\u4f7f\u7528\uff0c\u5426\u5219\u91c7\u7528\u6309\u7c7b\u578b\u81ea\u52a8\u6ce8\u5165\u3002<\/p>\n<p><code>ClassPathMapperScanner<\/code> \u9ed8\u8ba4\u626b\u63cf\u6240\u6709\u7684\u63a5\u53e3\u5e76\u8fdb\u884c\u6ce8\u518c\u3002<\/p>\n<h2>2.2. SqlSessionTemplate<\/h2>\n<p>SqlSessionTemplate \u662f<strong>\u7ebf\u7a0b\u5b89\u5168\u3001Spring \u7ba1\u7406\u7684 SqlSession<\/strong>\uff0c\u53ef\u4ee5\u6ce8\u5165\u5230\u5176\u4ed6 Spring \u7ba1\u7406\u7684 bean \u91cc\u53bb\u4f7f\u7528\u3002\u4e0e Spring \u4e8b\u52a1\u7ba1\u7406\u673a\u5236\u4e00\u8d77\u5de5\u4f5c\u4ee5\u4fdd\u8bc1\u5b9e\u9645\u4f7f\u7528\u7684 SqlSession \u662f\u4e0e\u5f53\u524d\u7684 Spring \u4e8b\u52a1\u5173\u8054\u7684\u3002\u53e6\u5916\uff0c\u5b83\u7ba1\u7406\u7740\u4f1a\u8bdd\u7684\u751f\u547d\u5468\u671f\uff0c\u5305\u62ec\u5173\u95ed\u3001\u63d0\u4ea4\u6216\u56de\u6eda\u4f1a\u8bdd\uff0c\u57fa\u4e8e Spring \u7684\u4e8b\u52a1\u914d\u7f6e\u3002<\/p>\n<p>\u8be5\u6a21\u677f\u9ed8\u8ba4\u4f7f\u7528 <code>MyBatisExceptionTranslator<\/code> \u628a MyBatis <code>PersistenceExceptions<\/code> \u8f6c\u6362\u4e3a\u4e0d\u53d7\u68c0\u67e5\u7684\u5f02\u5e38 <code>DataAccessExceptions<\/code> \u3002<\/p>\n<p>\u56e0\u4e3a <code>SqlSessionTemplate<\/code> \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u4e00\u4e2a\u5b9e\u4f8b\u53ef\u4ee5\u88ab\u6240\u6709\u7684 DAOs \u5171\u4eab\uff0c\u8fd9\u6837\u4e5f\u53ef\u4ee5\u8282\u7701\u70b9\u5185\u5b58\u3002<\/p>\n<pre><code class=\"java\">public class SqlSessionTemplate implements SqlSession, DisposableBean {\n    \/\/ \u7528\u4e8e\u521b\u5efa SqlSession\n    private final SqlSessionFactory sqlSessionFactory;\n\n    \/\/ \u6267\u884c\u5668\u7684\u7c7b\u578b\uff0c\u9ed8\u8ba4\u53d6 sqlSessionFactory \u91cc\u7684\n    private final ExecutorType executorType;\n\n    \/\/ \u62e6\u622a\u5bf9 SqlSession \u63a5\u53e3\u91cc\u58f0\u660e\u7684\u65b9\u6cd5\u7684\u8c03\u7528\uff0c\u4ee5\u4fbf\u4e0e Spring \u4e8b\u52a1\u7ba1\u7406\u96c6\u6210\n    private final SqlSession sqlSessionProxy;\n\n    \/\/ \u5f02\u5e38\u8f6c\u6362\u5668\n    private final PersistenceExceptionTranslator exceptionTranslator;\n}\n<\/code><\/pre>\n<h2>2.3 Configuration \u7684\u6784\u5efa\u8fc7\u7a0b<\/h2>\n<p>\u4e0b\u9762\u662f\u521b\u5efa <code>Configuration<\/code> \u5bf9\u8c61\u7684\u8fc7\u7a0b\uff1a<\/p>\n<pre><code class=\"java\">\/\/ SqlSessionFactoryBean\nprotected 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, 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    if (this.transactionFactory == null) {\n        \/\/ \u8bbe\u7f6e\u4e8b\u52a1\u5de5\u5382\u4e3a SpringManagedTransactionFactory\uff0c\u4ee5\u4fbf\u4e0e Spring \u7ba1\u7406\u7684\u4e8b\u52a1\u8fdb\u884c\u534f\u4f5c\n        this.transactionFactory = new SpringManagedTransactionFactory();\n    }\n\n    configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));\n\n    \/\/ \u9002\u7528\u4e8e Mapper \u7684 XML \u6587\u4ef6\u96c6\u4e2d\u5b58\u653e\u7684\u573a\u666f\n    if (!isEmpty(this.mapperLocations)) {\n        \/\/ \u5982\u679c\u6307\u5b9a\u4e86 Mapper \u5b58\u653e\u7684\u8def\u5f84\u5219\u89e3\u6790\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    }\n\n    return this.sqlSessionFactoryBuilder.build(configuration);\n}\n\n\/\/ SqlSessionFactoryBuilder\npublic SqlSessionFactory build(Configuration config) {\n    return new DefaultSqlSessionFactory(config);\n}\n<\/code><\/pre>\n<h2>2.4 Mapper \u5b9a\u4e49\u626b\u63cf<\/h2>\n<p>Mapper \u7684\u626b\u63cf\u4e00\u662f\u53ef\u4ee5\u901a\u8fc7\u5c5e\u6027 <code>mybatis.mapperLocations<\/code> \u6765\u6307\u5b9a\u5176\u5b58\u653e\u8def\u5f84\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7 <code>@MapperScan(\"package.to.scan\")<\/code> \u6ce8\u89e3\u6765\u914d\u7f6e\u3002<\/p>\n<p>\u8be5\u6ce8\u89e3\u626b\u63cf\u6307\u5b9a\u5305\u4e0b\u7684\u6240\u6709\u63a5\u53e3\u3001\u5e76\u4e3a\u5176\u751f\u6210\u4ee3\u7406\u6ce8\u518c\u5230 Spring \u7684\u5bb9\u5668\u91cc\u3002<\/p>\n<p><code>MapperScannerRegistrar<\/code> \u4ece <code>@MapperScan<\/code> \u6ce8\u89e3\u83b7\u53d6\u626b\u63cf\u914d\u7f6e\uff0c\u7528\u4ee5\u521d\u59cb\u5316 <code>ClassPathMapperScanner<\/code> \u5e76\u5b8c\u6210\u6700\u7ec8\u7684\u626b\u63cf\u5de5\u4f5c\u3002<\/p>\n<pre><code class=\"java\">public Set&lt;BeanDefinitionHolder&gt; doScan(String... basePackages) {\n    Set&lt;BeanDefinitionHolder&gt; beanDefinitions = super.doScan(basePackages);\n\n    if (beanDefinitions.isEmpty()) {\n        logger.warn(\"No MyBatis mapper was found in '\" + Arrays.toString(basePackages) + \"' package. Please check your configuration.\");\n    } else {\n        processBeanDefinitions(beanDefinitions);\n    }\n\n    return beanDefinitions;\n}\n\nprivate void processBeanDefinitions(Set&lt;BeanDefinitionHolder&gt; beanDefinitions) {\n    GenericBeanDefinition definition;\n    for (BeanDefinitionHolder holder : beanDefinitions) {\n        definition = (GenericBeanDefinition) holder.getBeanDefinition();\n\n        definition.getConstructorArgumentValues().addGenericArgumentValue(definition.getBeanClassName()); \/\/ issue #59\n        \/\/ \u8bbe\u7f6ebean\u5b9a\u4e49\u7684\u7c7b\u4e3a MapperFactoryBean\n        definition.setBeanClass(this.mapperFactoryBean.getClass());\n\n        definition.getPropertyValues().add(\"addToConfig\", this.addToConfig);\n\n        boolean explicitFactoryUsed = false;\n        if (StringUtils.hasText(this.sqlSessionFactoryBeanName)) {\n            definition.getPropertyValues().add(\"sqlSessionFactory\", new RuntimeBeanReference(this.sqlSessionFactoryBeanName));\n            explicitFactoryUsed = true;\n        } else if (this.sqlSessionFactory != null) {\n            definition.getPropertyValues().add(\"sqlSessionFactory\", this.sqlSessionFactory);\n            explicitFactoryUsed = true;\n        }\n\n        if (StringUtils.hasText(this.sqlSessionTemplateBeanName)) {\n            if (explicitFactoryUsed) {\n                logger.warn(\"Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.\");\n            }\n            definition.getPropertyValues().add(\"sqlSessionTemplate\", new RuntimeBeanReference(this.sqlSessionTemplateBeanName));\n            explicitFactoryUsed = true;\n        } else if (this.sqlSessionTemplate != null) {\n            if (explicitFactoryUsed) {\n                logger.warn(\"Cannot use both: sqlSessionTemplate and sqlSessionFactory together. sqlSessionFactory is ignored.\");\n            }\n            definition.getPropertyValues().add(\"sqlSessionTemplate\", this.sqlSessionTemplate);\n            explicitFactoryUsed = true;\n        }\n\n        if (!explicitFactoryUsed) {\n            \/\/ \u5982\u679c\u6ca1\u6709\u663e\u5f0f\u8bbe\u7f6e sqlSessionTemplate \u6216 sqlSessionFactory \u5219\u6309\u7c7b\u578b\u6ce8\u5165\n            definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u6700\u91cd\u8981\u7684\u662f \u8bbe\u7f6e bean \u5b9a\u4e49\u7684\u7c7b\u4e3a <code>MapperFactoryBean<\/code>\u3002<\/p>\n<h2>2.5 Mapper \u5b9e\u4f8b\u5316<\/h2>\n<p>Mapper \u7684 Bean \u5b9a\u4e49\u88ab\u626b\u63cf\u540e\u4f1a\u6ce8\u518c\u5230 Spring \u5bb9\u5668\u91cc\uff0c\u518d\u7531 Spring \u5b9e\u4f8b\u5316\u65f6\u8c03\u7528 <code>MapperFactoryBean.getObject()<\/code> \u65b9\u6cd5\u5f97\u5230\u8be5\u7c7b\u578b\u7684 Bean \u5b9e\u4f8b\uff0c\u6ce8\u518c\u5230\u5bb9\u5668\u91cc\uff0c\u4f9b\u5e94\u7528\u4f7f\u7528\u3002<\/p>\n<pre><code class=\"java\">public abstract class SqlSessionDaoSupport extends DaoSupport {\n    private SqlSession sqlSession;\n    private boolean externalSqlSession;\n\n    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {\n        if (!this.externalSqlSession) {\n          this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);\n        }\n    }\n\n    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {\n        this.sqlSession = sqlSessionTemplate;\n        this.externalSqlSession = true;\n    }\n\n    public SqlSession getSqlSession() {\n        return this.sqlSession;\n    }\n\n    \/\/ \u7701\u7565\u5176\u4ed6\n}\n\npublic class MapperFactoryBean&lt;T&gt; extends SqlSessionDaoSupport implements FactoryBean&lt;T&gt; {\n    \/\/ \u8be5\u65b9\u6cd5\u5728\u7236\u7c7b\u7684 afterPropertiesSet \u91cc\u8c03\u7528\n    \/\/ \u7528\u4e8e\u628a\u63a5\u53e3\u7c7b\u578b\u6ce8\u518c\u5230 Configuration \u91cc\u3002\u4f9b\u540e\u9762\u521b\u5efa\u4ee3\u7406\u65f6\u4f7f\u7528\n    protected void checkDaoConfig() {\n        super.checkDaoConfig();\n\n        notNull(this.mapperInterface, \"Property 'mapperInterface' is required\");\n\n        Configuration configuration = getSqlSession().getConfiguration();\n        if (this.addToConfig &amp;&amp; !configuration.hasMapper(this.mapperInterface)) {\n            try {\n                configuration.addMapper(this.mapperInterface);\n            } catch (Exception e) {\n                logger.error(\"Error while adding the mapper '\" + this.mapperInterface + \"' to configuration.\", e);\n                throw new IllegalArgumentException(e);\n            } finally {\n                ErrorContext.instance().reset();\n            }\n        }\n    }\n\n    public T getObject() throws Exception {\n        return getSqlSession().getMapper(this.mapperInterface);\n    }\n\n    \/\/ \u7701\u7565\u5176\u4ed6\n}\n<\/code><\/pre>\n<p><code>SqlSessionDaoSupport<\/code> \u7684\u4e3b\u8981\u4f5c\u7528\u662f\u628a <code>sqlSession<\/code> \u8bbe\u7f6e\u4e3a <code>SqlSessionTemplate<\/code>\uff0c<code>DaoSupport<\/code> \u63d0\u4f9b\u4e86\u5c5e\u6027\u8bbe\u7f6e\u540e\u56de\u8c03\u7684\u521d\u59cb\u5316\u94a9\u5b50\uff0c\u89e6\u53d1\u8c03\u7528 <code>MapperFactoryBean.checkDaoConfig<\/code> \u65b9\u6cd5\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7406\u7684\u521b\u5efa\u8c03\u7528\u94fe\uff1a<\/p>\n<pre><code class=\"java\">\/\/ SqlSessionTemplate\npublic &lt;T&gt; T getMapper(Class&lt;T&gt; type) {\n    return getConfiguration().getMapper(type, this);\n}\n\n\/\/ Configuration\npublic &lt;T&gt; T getMapper(Class&lt;T&gt; type, SqlSession sqlSession) {\n    return mapperRegistry.getMapper(type, sqlSession);\n}\n\n\/\/ MapperRegistry\npublic &lt;T&gt; T getMapper(Class&lt;T&gt; type, SqlSession sqlSession) {\n    final MapperProxyFactory&lt;T&gt; mapperProxyFactory = (MapperProxyFactory&lt;T&gt;) knownMappers.get(type);\n    if (mapperProxyFactory == null) {\n        throw new BindingException(\"Type \" + type + \" is not known to the MapperRegistry.\");\n    }\n    try {\n        return mapperProxyFactory.newInstance(sqlSession);\n    } catch (Exception e) {\n        throw new BindingException(\"Error getting mapper instance. Cause: \" + e, e);\n    }\n}\n\n\/\/ MapperProxyFactory\nprotected T newInstance(MapperProxy&lt;T&gt; mapperProxy) {\n    return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);\n}\n\npublic T newInstance(SqlSession sqlSession) {\n    final MapperProxy&lt;T&gt; mapperProxy = new MapperProxy&lt;T&gt;(sqlSession, mapperInterface, methodCache);\n    return newInstance(mapperProxy);\n}\n<\/code><\/pre>\n<p>\u6700\u7ec8\u521b\u5efa\u4e86\u4e00\u4e2a JDK \u7684\u52a8\u6001\u4ee3\u7406\uff0c\u4ee3\u7406\u7684\u65b9\u6cd5\u5904\u7406\u5668\u4e3a <code>MapperProxy<\/code> \uff0c\u5176\u6301\u6709\u7684 <code>SqlSession<\/code> \u4ecd\u7136\u662f <code>SqlSessionTemplate<\/code> \u7684\u5b9e\u4f8b\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>\u672c\u6587\u4e3b\u8981\u5173\u6ce8\u4e0e SpringBoot \u96c6\u6210\u65f6\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\u3002 1. \u6838\u5fc3\u7ec4\u4ef6 Co &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/framework\/20191025\/2006\">\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\/2006"}],"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=2006"}],"version-history":[{"count":7,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2006\/revisions"}],"predecessor-version":[{"id":2014,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/2006\/revisions\/2014"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=2006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=2006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=2006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}