{"id":10006287,"date":"2022-08-23T08:59:55","date_gmt":"2022-08-23T00:59:55","guid":{"rendered":"https:\/\/ityet.com\/?p=10006287"},"modified":"2022-08-23T08:59:55","modified_gmt":"2022-08-23T00:59:55","slug":"%e4%bb%8e%e9%9b%b6%e5%bc%80%e5%a7%8b%e5%ae%9e%e7%8e%b0%e4%b8%80%e4%b8%aamybatis%e5%8a%a0%e8%a7%a3%e5%af%86%e6%8f%92%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006287","title":{"rendered":"\u4ece\u96f6\u5f00\u59cb\u5b9e\u73b0\u4e00\u4e2aMyBatis\u52a0\u89e3\u5bc6\u63d2\u4ef6"},"content":{"rendered":"<blockquote><p>\u4f5c\u8005\uff1avivo \u4e92\u8054\u7f51\u670d\u52a1\u5668\u56e2\u961f- Li Gang<\/p><\/blockquote>\n<p>\u672c\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4f7f\u7528MyBatis\u63d2\u4ef6\u6765\u5b9e\u73b0\u6570\u636e\u5e93\u5b57\u6bb5\u52a0\u89e3\u5bc6\u7684\u8fc7\u7a0b\u3002<\/p>\n<h2>\u4e00\u3001\u9700\u6c42\u80cc\u666f<\/h2>\n<p>\u516c\u53f8\u51fa\u4e8e\u5b89\u5168\u5408\u89c4\u7684\u8003\u8651\uff0c\u9700\u8981\u5bf9\u660e\u6587\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\u7684\u90e8\u5206\u5b57\u6bb5\u8fdb\u884c\u52a0\u5bc6\uff0c\u9632\u6b62\u672a\u7ecf\u6388\u6743\u7684\u8bbf\u95ee\u4ee5\u53ca\u4e2a\u4eba\u4fe1\u606f\u6cc4\u6f0f\u3002<\/p>\n<p>\u7531\u4e8e\u9879\u76ee\u5df2\u505c\u6b62\u8fed\u4ee3\uff0c\u6539\u9020\u7684\u6210\u672c\u592a\u5927\uff0c\u56e0\u6b64\u6211\u4eec\u9009\u7528\u4e86MyBatis\u63d2\u4ef6\u6765\u5b9e\u73b0\u6570\u636e\u5e93\u52a0\u89e3\u5bc6\uff0c\u4fdd\u8bc1\u5f80\u6570\u636e\u5e93\u5199\u5165\u6570\u636e\u65f6\u80fd\u5bf9\u6307\u5b9a\u5b57\u6bb5\u52a0\u5bc6\uff0c\u8bfb\u53d6\u6570\u636e\u65f6\u80fd\u5bf9\u6307\u5b9a\u5b57\u6bb5\u89e3\u5bc6\u3002<\/p>\n<h2>\u4e8c\u3001\u601d\u8def\u89e3\u6790<\/h2>\n<h3>2.1 \u7cfb\u7edf\u67b6\u6784<\/h3>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042329721\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<ol>\n<li>\u5bf9\u6bcf\u4e2a\u9700\u8981\u52a0\u5bc6\u7684\u5b57\u6bb5\u65b0\u589e\u5bc6\u6587\u5b57\u6bb5\uff08\u5bf9\u4e1a\u52a1\u6709\u4fb5\u5165\uff09\uff0c\u4fee\u6539\u6570\u636e\u5e93\u3001mapper.xml\u4ee5\u53caDO\u5bf9\u8c61\uff0c\u901a\u8fc7\u63d2\u4ef6\u7684\u65b9\u5f0f\u628a\u9488\u5bf9\u660e\u6587\/\u5bc6\u6587\u5b57\u6bb5\u7684\u52a0\u89e3\u5bc6\u8fdb\u884c\u6536\u53e3\u3002<\/li>\n<li>\u81ea\u5b9a\u4e49Executor\u5bf9SELECT\/UPDATE\/INSERT\/DELETE\u7b49\u64cd\u4f5c\u7684\u660e\u6587\u5b57\u6bb5\u8fdb\u884c\u52a0\u5bc6\u5e76\u8bbe\u7f6e\u5230\u5bc6\u6587\u5b57\u6bb5\u3002<\/li>\n<li>\u81ea\u5b9a\u4e49\u63d2\u4ef6ResultSetHandler\u8d1f\u8d23\u9488\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u89e3\u5bc6\uff0c\u8d1f\u8d23\u5bf9SELECT\u7b49\u64cd\u4f5c\u7684\u5bc6\u6587\u5b57\u6bb5\u8fdb\u884c\u89e3\u5bc6\u5e76\u8bbe\u7f6e\u5230\u660e\u6587\u5b57\u6bb5\u3002<\/li>\n<\/ol>\n<h3>2.2 \u7cfb\u7edf\u6d41\u7a0b<\/h3>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042329722\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<ol>\n<li>\u65b0\u589e\u52a0\u89e3\u5bc6\u6d41\u7a0b\u63a7\u5236\u5f00\u5173\uff0c\u5206\u522b\u63a7\u5236\u5199\u5165\u65f6\u662f\u53ea\u5199\u539f\u5b57\u6bb5\/\u53cc\u5199\/\u53ea\u5199\u52a0\u5bc6\u540e\u7684\u5b57\u6bb5\uff0c\u4ee5\u53ca\u8bfb\u53d6\u65f6\u662f\u8bfb\u539f\u5b57\u6bb5\u8fd8\u662f\u52a0\u5bc6\u540e\u7684\u5b57\u6bb5\u3002<\/li>\n<li>\u65b0\u589e\u5386\u53f2\u6570\u636e\u52a0\u5bc6\u4efb\u52a1\uff0c\u5bf9\u5386\u53f2\u6570\u636e\u6279\u91cf\u8fdb\u884c\u52a0\u5bc6\uff0c\u5199\u5165\u5230\u52a0\u5bc6\u540e\u5b57\u6bb5\u3002<\/li>\n<li>\u51fa\u4e8e\u5b89\u5168\u4e0a\u7684\u8003\u8651\uff0c\u6d41\u7a0b\u91cc\u8fd8\u4f1a\u6709\u4e00\u4e9b\u6821\u9a8c\/\u8865\u507f\u7684\u4efb\u52a1\uff0c\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\u3002<\/li>\n<\/ol>\n<h2>\u4e09\u3001\u65b9\u6848\u5236\u5b9a<\/h2>\n<h3>3.1 MyBatis\u63d2\u4ef6\u7b80\u4ecb<\/h3>\n<p>MyBatis \u9884\u7559\u4e86 org.apache.ibatis.plugin.Interceptor \u63a5\u53e3\uff0c\u901a\u8fc7\u5b9e\u73b0\u8be5\u63a5\u53e3\uff0c\u6211\u4eec\u80fd\u5bf9MyBatis\u7684\u6267\u884c\u6d41\u7a0b\u8fdb\u884c\u62e6\u622a\uff0c\u63a5\u53e3\u7684\u5b9a\u4e49\u5982\u4e0b:<\/p>\n<pre><code>public interface Interceptor {\n  Object intercept(Invocation invocation) throws Throwable;\n  Object plugin(Object target);\n  void setProperties(Properties properties);\n}<\/code><\/pre>\n<p>\u5176\u4e2d\u6709\u4e09\u4e2a\u65b9\u6cd5:<\/p>\n<ul>\n<li><strong>\u3010intercept\u3011<\/strong>\uff1a\u63d2\u4ef6\u6267\u884c\u7684\u5177\u4f53\u6d41\u7a0b\uff0c\u4f20\u5165\u7684Invocation\u662fMyBatis\u5bf9\u88ab\u4ee3\u7406\u7684\u65b9\u6cd5\u7684\u5c01\u88c5\u3002<\/li>\n<li><strong>\u3010plugin\u3011<\/strong>\uff1a\u4f7f\u7528\u5f53\u524d\u7684Interceptor\u521b\u5efa\u4ee3\u7406\uff0c\u901a\u5e38\u7684\u5b9e\u73b0\u90fd\u662f Plugin.wrap(target, this)\uff0cwrap\u65b9\u6cd5\u5185\u4f7f\u7528 jdk \u521b\u5efa\u52a8\u6001\u4ee3\u7406\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u3010setProperties\u3011<\/strong>\uff1a\u53c2\u8003\u4e0b\u65b9\u4ee3\u7801\uff0c\u5728MyBatis\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u63d2\u4ef6\u65f6\u53ef\u4ee5\u8bbe\u7f6e\u53c2\u6570\uff0c\u5728setProperties\u51fd\u6570\u4e2d\u8c03\u7528 Properties.getProperty(&#8220;param1&#8221;) \u65b9\u6cd5\u53ef\u4ee5\u5f97\u5230\u914d\u7f6e\u7684\u503c\u3002<\/li>\n<\/ul>\n<pre><code>&lt;plugins&gt;\n    &lt;plugin interceptor=&quot;com.xx.xx.xxxInterceptor&quot;&gt;\n        &lt;property name=&quot;param1&quot; value=&quot;value1&quot;\/&gt;\n    &lt;\/plugin&gt;\n&lt;\/plugins&gt;<\/code><\/pre>\n<p>\u5728\u5b9e\u73b0intercept\u51fd\u6570\u5bf9MyBatis\u7684\u6267\u884c\u6d41\u7a0b\u8fdb\u884c\u62e6\u622a\u524d\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528@Intercepts\u6ce8\u89e3\u6307\u5b9a\u62e6\u622a\u7684\u65b9\u6cd5\u3002<\/p>\n<pre><code>@Intercepts({ @Signature(type = Executor.class, method = &quot;update&quot;, args = { MappedStatement.class, Object.class }),\n        @Signature(type = Executor.class, method = &quot;query&quot;, args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) })<\/code><\/pre>\n<p>\u53c2\u8003\u4e0a\u65b9\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9a\u9700\u8981\u62e6\u622a\u7684\u7c7b\u548c\u65b9\u6cd5\u3002\u5f53\u7136\u6211\u4eec\u4e0d\u80fd\u5bf9\u4efb\u610f\u7684\u5bf9\u8c61\u505a\u62e6\u622a\uff0cMyBatis\u4ef6\u53ef\u62e6\u622a\u7684\u7c7b\u4e3a\u4ee5\u4e0b\u56db\u4e2a\u3002<\/p>\n<ol>\n<li>Executor<\/li>\n<li>StatementHandler<\/li>\n<li>ParameterHandler<\/li>\n<li>ResultSetHandler<\/li>\n<\/ol>\n<p>\u56de\u5230\u6570\u636e\u5e93\u52a0\u5bc6\u7684\u9700\u6c42\uff0c\u6211\u4eec\u9700\u8981\u4ece\u4e0a\u9762\u56db\u4e2a\u7c7b\u91cc\u9009\u62e9\u80fd\u7528\u6765\u5b9e\u73b0\u5165\u53c2\u52a0\u5bc6\u548c\u51fa\u53c2\u89e3\u5bc6\u7684\u7c7b\u3002\u5728\u4ecb\u7ecd\u8fd9\u56db\u4e2a\u7c7b\u4e4b\u524d\uff0c\u9700\u8981\u5bf9MyBatis\u7684\u6267\u884c\u6d41\u7a0b\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\u3002<\/p>\n<h3>3.2 Spring-MyBatis\u6267\u884c\u6d41\u7a0b<\/h3>\n<p>\uff081\uff09Spring\u901a\u8fc7sqlSessionFactoryBean\u521b\u5efasqlSessionFactory\uff0c\u5728\u4f7f\u7528sqlSessionFactoryBean\u65f6\uff0c\u6211\u4eec\u901a\u5e38\u4f1a\u6307\u5b9aconfigLocation\u548cmapperLocations\uff0c\u6765\u544a\u8bc9sqlSessionFactoryBean\u53bb\u54ea\u91cc\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u4ee5\u53ca\u53bb\u54ea\u91cc\u8bfb\u53d6mapper\u6587\u4ef6\u3002<\/p>\n<p>\uff082\uff09\u5f97\u5230\u914d\u7f6e\u6587\u4ef6\u548cmapper\u6587\u4ef6\u7684\u4f4d\u7f6e\u540e\uff0c\u5206\u522b\u8c03\u7528XmlConfigBuilder.parse()\u548cXmlMapperBuilder.parse()\u521b\u5efaConfiguration\u548cMappedStatement\uff0cConfiguration\u7c7b\u987e\u540d\u601d\u4e49\uff0c\u5b58\u653e\u7684\u662fMyBatis\u6240\u6709\u7684\u914d\u7f6e\uff0c\u800cMappedStatement\u7c7b\u5b58\u653e\u7684\u662f\u6bcf\u6761SQL\u8bed\u53e5\u7684\u5c01\u88c5\uff0cMappedStatement\u4ee5map\u7684\u5f62\u5f0f\u5b58\u653e\u5230Configuration\u5bf9\u8c61\u4e2d\uff0ckey\u4e3a\u5bf9\u5e94\u65b9\u6cd5\u7684\u5168\u8def\u5f84\u3002<\/p>\n<p>\uff083\uff09Spring\u901a\u8fc7ClassPathMapperScanner\u626b\u63cf\u6240\u6709\u7684Mapper\u63a5\u53e3\uff0c\u4e3a\u5176\u521b\u5efaBeanDefinition\u5bf9\u8c61\uff0c\u4f46\u7531\u4e8e\u4ed6\u4eec\u672c\u8d28\u4e0a\u90fd\u662f\u6ca1\u6709\u88ab\u5b9e\u73b0\u7684\u63a5\u53e3\uff0c\u6240\u4ee5Spring\u4f1a\u5c06\u4ed6\u4eec\u7684BeanDefinition\u7684beanClass\u5c5e\u6027\u4fee\u6539\u4e3aMapperFactorybean\u3002<\/p>\n<p>\uff084\uff09MapperFactoryBean\u4e5f\u5b9e\u73b0\u4e86FactoryBean\u63a5\u53e3\uff0cSpring\u5728\u521b\u5efaBean\u65f6\u4f1a\u8c03\u7528FactoryBean.getObject()\u65b9\u6cd5\u83b7\u53d6Bean\uff0c\u6700\u7ec8\u662f\u901a\u8fc7mapperProxyFactory\u7684newInstance\u65b9\u6cd5\u4e3amapper\u63a5\u53e3\u521b\u5efa\u4ee3\u7406\uff0c\u521b\u5efa\u4ee3\u7406\u7684\u65b9\u5f0f\u662fJDK\uff0c\u6700\u7ec8\u751f\u6210\u7684\u4ee3\u7406\u5bf9\u8c61\u662fMapperProxy\u3002<\/p>\n<p>\uff085\uff09\u8c03\u7528mapper\u7684\u6240\u6709\u63a5\u53e3\u672c\u8d28\u4e0a\u8c03\u7528\u7684\u90fd\u662fMapperProxy.invoke\u65b9\u6cd5\uff0c\u5185\u90e8\u8c03\u7528sqlSession\u7684insert\/update\/delete\u7b49\u5404\u79cd\u65b9\u6cd5\u3002<\/p>\n<pre><code>MapperMethod.java\npublic Object execute(SqlSession sqlSession, Object[] args) {\n  Object result;\n  if (SqlCommandType.INSERT == command.getType()) {\n    Object param = method.convertArgsToSqlCommandParam(args);\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  } else if (SqlCommandType.DELETE == command.getType()) {\n    Object param = method.convertArgsToSqlCommandParam(args);\n    result = rowCountResult(sqlSession.delete(command.getName(), param));\n  } else if (SqlCommandType.SELECT == command.getType()) {\n    if (method.returnsVoid() &amp;&amp; method.hasResultHandler()) {\n      executeWithResultHandler(sqlSession, args);\n      result = null;\n    } else if (method.returnsMany()) {\n      result = executeForMany(sqlSession, args);\n    } else if (method.returnsMap()) {\n      result = executeForMap(sqlSession, args);\n    } else {\n      Object param = method.convertArgsToSqlCommandParam(args);\n      result = sqlSession.selectOne(command.getName(), param);\n    }\n  } else if (SqlCommandType.FLUSH == command.getType()) {\n      result = sqlSession.flushStatements();\n  } else {\n    throw new BindingException(&quot;Unknown execution method for: &quot; + command.getName());\n  }\n  if (result == null &amp;&amp; method.getReturnType().isPrimitive() &amp;&amp; !method.returnsVoid()) {\n    throw new BindingException(&quot;Mapper method '&quot; + command.getName()\n        + &quot; attempted to return null from a method with a primitive return type (&quot; + method.getReturnType() + &quot;).&quot;);\n  }\n  return result;\n}<\/code><\/pre>\n<p>\uff086\uff09SqlSession\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u6b21\u4f1a\u8bdd\uff0cSqlSession\u4f1a\u4eceConfiguration\u4e2d\u83b7\u53d6\u5bf9\u5e94MappedStatement\uff0c\u4ea4\u7ed9Executor\u6267\u884c\u3002<\/p>\n<pre><code>DefaultSqlSession.java\n@Override\npublic &lt;E&gt; List&lt;E&gt; selectList(String statement, Object parameter, RowBounds rowBounds) {\n  try {\n    \/\/ \u4ececonfiguration\u5bf9\u8c61\u4e2d\u4f7f\u7528\u88ab\u8c03\u7528\u65b9\u6cd5\u7684\u5168\u8def\u5f84\uff0c\u83b7\u53d6\u5bf9\u5e94\u7684MappedStatement\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(&quot;Error querying database.  Cause: &quot; + e, e);\n  } finally {\n    ErrorContext.instance().reset();\n  }\n}<\/code><\/pre>\n<p>\uff087\uff09Executor\u4f1a\u5148\u521b\u5efaStatementHandler\uff0cStatementHandler\u53ef\u4ee5\u7406\u89e3\u4e3a\u662f\u4e00\u6b21\u8bed\u53e5\u7684\u6267\u884c\u3002<\/p>\n<p>\uff088\uff09\u7136\u540eExecutor\u4f1a\u83b7\u53d6\u8fde\u63a5\uff0c\u5177\u4f53\u83b7\u53d6\u8fde\u63a5\u7684\u65b9\u5f0f\u53d6\u51b3\u4e8eDatasource\u7684\u5b9e\u73b0\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fde\u63a5\u6c60\u7b49\u65b9\u5f0f\u83b7\u53d6\u8fde\u63a5\u3002<\/p>\n<p>\uff089\uff09\u4e4b\u540e\u8c03\u7528StatementHandler.prepare\u65b9\u6cd5\uff0c\u5bf9\u5e94\u5230JDBC\u6267\u884c\u6d41\u7a0b\u4e2d\u7684Connection.prepareStatement\u8fd9\u4e00\u6b65\u3002<\/p>\n<p>\uff0810\uff09Executor\u518d\u8c03\u7528StatementHandler\u7684parameterize\u65b9\u6cd5\uff0c\u8bbe\u7f6e\u53c2\u6570\uff0c\u5bf9\u5e94\u5230JDBC\u6267\u884c\u6d41\u7a0b\u7684StatementHandler.setXXX()\u8bbe\u7f6e\u53c2\u6570\uff0c\u5185\u90e8\u4f1a\u521b\u5efaParameterHandler\u65b9\u6cd5\u3002<\/p>\n<pre><code>SimpleExecutor.java\n@Override\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    \/\/ \u521b\u5efaStatementHandler\uff0c\u5bf9\u5e94\u7b2c7\u6b65\n    StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);\n    \/\/ \u83b7\u53d6\u8fde\u63a5\uff0c\u518d\u8c03\u7528conncetion.prepareStatement\u521b\u5efaprepareStatement\uff0c\u8bbe\u7f6e\u53c2\u6570\n    stmt = prepareStatement(handler, ms.getStatementLog());\n    \/\/ \u6267\u884cprepareStatement\n    return handler.&lt;E&gt;query(stmt, resultHandler);\n  } finally {\n    closeStatement(stmt);\n  }\n}<\/code><\/pre>\n<p>\uff0811\uff09\u518d\u7531ResultSetHandler\u5904\u7406\u8fd4\u56de\u7ed3\u679c\uff0c\u5904\u7406JDBC\u7684\u8fd4\u56de\u503c\uff0c\u5c06\u5176\u8f6c\u6362\u4e3aJava\u7684\u5bf9\u8c61\u3002<\/p>\n<h3>3.3 MyBatis\u63d2\u4ef6\u7684\u521b\u5efa\u65f6\u673a<\/h3>\n<p>\u5728Configuration\u7c7b\u4e2d\uff0c\u6211\u4eec\u80fd\u770b\u5230newExecutor\u3001newStatementHandler\u3001newParameterHandler\u3001newResultSetHandler\u8fd9\u56db\u4e2a\u65b9\u6cd5\uff0c\u63d2\u4ef6\u7684\u4ee3\u7406\u7c7b\u5c31\u662f\u5728\u8fd9\u56db\u4e2a\u65b9\u6cd5\u4e2d\u521b\u5efa\u7684\uff0c\u6211\u4ee5StatementHandeler\u7684\u521b\u5efa\u4e3a\u4f8b\uff1a<\/p>\n<pre><code>Configuration.java\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  \/\/ \u4f7f\u7528\u8d23\u4efb\u94fe\u7684\u5f62\u5f0f\u521b\u5efa\u4ee3\u7406\n  statementHandler = (StatementHandler) interceptorChain.pluginAll(statementHandler);\n  return statementHandler;\n}\n \nInterceptorChain.java\npublic Object pluginAll(Object target) {\n  for (Interceptor interceptor : interceptors) {\n    target = interceptor.plugin(target);\n  }\n  return target;\n}<\/code><\/pre>\n<p>interceptor.plugin\u5bf9\u5e94\u5230\u6211\u4eec\u81ea\u5df1\u5b9e\u73b0\u7684interceptor\u91cc\u7684\u65b9\u6cd5\uff0c\u901a\u5e38\u7684\u5b9e\u73b0\u662f Plugin.wrap(target, this); \uff0c\u8be5\u65b9\u6cd5\u5185\u90e8\u521b\u5efa\u4ee3\u7406\u7684\u65b9\u5f0f\u4e3aJDK\u3002<\/p>\n<h3>3.4 MyBatis\u63d2\u4ef6\u53ef\u62e6\u622a\u7c7b\u9009\u62e9<\/h3>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042329723\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>Mybatis\u672c\u8d28\u4e0a\u662f\u5bf9JDBC\u6267\u884c\u6d41\u7a0b\u7684\u5c01\u88c5\u3002\u7ed3\u5408\u4e0a\u56fe\u6211\u4eec\u7b80\u8981\u6982\u62ec\u4e0bMybatis\u8fd9\u51e0\u4e2a\u53ef\u88ab\u4ee3\u7406\u7c7b\u7684\u804c\u80fd\u3002<\/p>\n<ul>\n<li><strong>\u3010Executor\u3011<\/strong>: \u771f\u6b63\u6267\u884cSQL\u8bed\u53e5\u7684\u5bf9\u8c61\uff0c\u8c03\u7528sqlSession\u7684\u65b9\u6cd5\u65f6\uff0c\u672c\u8d28\u4e0a\u90fd\u662f\u8c03\u7528executor\u7684\u65b9\u6cd5\uff0c\u8fd8\u8d1f\u8d23\u83b7\u53d6connection\uff0c\u521b\u5efaStatementHandler\u3002<\/li>\n<li><strong>\u3010StatementHandler\u3011<\/strong>: \u521b\u5efa\u5e76\u6301\u6709ParameterHandler\u548cResultSetHandler\u5bf9\u8c61\uff0c\u64cd\u4f5cJDBC\u7684statement\u4e0e\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/li>\n<li>\u3010ParameterHandler\u3011: \u5904\u7406\u5165\u53c2\uff0c\u5c06Java\u65b9\u6cd5\u4e0a\u7684\u53c2\u6570\u8bbe\u7f6e\u5230\u88ab\u6267\u884c\u8bed\u53e5\u4e2d\u3002<\/li>\n<li><strong>\u3010ResultSetHandler\u3011<\/strong>: \u5904\u7406SQL\u8bed\u53e5\u7684\u6267\u884c\u7ed3\u679c\uff0c\u5c06\u8fd4\u56de\u503c\u8f6c\u6362\u4e3aJava\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u5bf9\u4e8e\u5165\u53c2\u7684\u52a0\u5bc6\uff0c\u6211\u4eec\u9700\u8981\u5728ParameterHandler\u8c03\u7528prepareStatement.setXXX()\u65b9\u6cd5\u8bbe\u7f6e\u53c2\u6570\u524d\uff0c\u5c06\u53c2\u6570\u503c\u4fee\u6539\u4e3a\u52a0\u5bc6\u540e\u7684\u53c2\u6570\uff0c\u8fd9\u6837\u4e00\u770b\u597d\u50cf\u62e6\u622aExecutor\/StatementHandler\/ParameterHandler\u90fd\u53ef\u4ee5\u3002<\/p>\n<p>\u4f46\u5b9e\u9645\u4e0a\u5462\uff1f\u7531\u4e8e\u6211\u4eec\u7684\u5e76\u4e0d\u662f\u5728\u539f\u59cb\u5b57\u6bb5\u4e0a\u505a\u52a0\u5bc6\uff0c\u800c\u662f\u65b0\u589e\u4e86\u4e00\u4e2a\u52a0\u5bc6\u540e\u5b57\u6bb5\uff0c\u8fd9\u4f1a\u5e26\u6765\u4ec0\u4e48\u95ee\u9898\uff1f\u8bf7\u770b\u4e0b\u9762\u8fd9\u6761mapper.xml\u6587\u4ef6\u4e2d\u52a0\u4e86\u52a0\u5bc6\u540e\u5b57\u6bb5\u7684\u52a8\u6001SQL\uff1a<\/p>\n<pre><code>&lt;select id=&quot;selectUserList&quot; resultMap=&quot;BaseResultMap&quot; parameterType=&quot;com.xxx.internet.demo.entity.UserInfo&quot;&gt;\n        SELECT\n        *\n        FROM\n        `t_user_info`\n        &lt;where&gt;\n            &lt;if test=&quot;phone != null&quot;&gt;\n                `phone` = #{phone}\n            &lt;\/if&gt;\n&lt;!--            \u660e\u6587\u5b57\u6bb5--&gt;\n            &lt;if test=&quot;secret != null&quot;&gt;\n                AND `secret` = #{secret}\n            &lt;\/if&gt;\n&lt;!--            \u52a0\u5bc6\u540e\u5b57\u6bb5--&gt;\n            &lt;if test=&quot;secretCiper != null&quot;&gt;\n                AND `secret_ciper` = #{secretCiper}\n            &lt;\/if&gt;\n            &lt;if test=&quot;name&quot;&gt;\n                AND `name` = #{name}\n            &lt;\/if&gt;\n        &lt;\/where&gt;\n        ORDER BY `update_time` DESC\n    &lt;\/select&gt;<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u8fd9\u6761\u8bed\u53e5\u5e26\u4e86\u52a8\u6001\u6807\u7b7e\uff0c\u90a3\u80af\u5b9a\u4e0d\u80fd\u76f4\u63a5\u4ea4\u7ed9JDBC\u521b\u5efaprepareStatement\uff0c\u9700\u8981\u5148\u5c06\u5176\u89e3\u6790\u6210\u9759\u6001SQL\uff0c\u800c\u8fd9\u4e00\u6b65\u662f\u5728Executor\u5728\u8c03\u7528StatementHandler.parameterize()\u524d\u505a\u7684\uff0c\u7531MappedStatementHandler.getBoundSql(Object parameterObject)\u51fd\u6570\u89e3\u6790\u52a8\u6001\u6807\u7b7e\uff0c\u751f\u6210\u9759\u6001SQL\u8bed\u53e5\uff0c\u8fd9\u91cc\u7684parameterObject\u6211\u4eec\u53ef\u4ee5\u6682\u65f6\u5148\u5c06\u5176\u770b\u6210\u4e00\u4e2aMap\uff0c\u952e\u503c\u5206\u522b\u4e3a\u53c2\u6570\u540d\u548c\u53c2\u6570\u503c\u3002<\/p>\n<p>\u90a3\u4e48\u6211\u4eec\u6765\u770b\u4e0b\u7528StatementHandler\u548cParameterHandler\u505a\u53c2\u6570\u52a0\u5bc6\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\uff0c\u5728\u6267\u884cMappedStatementHandler.getBoundSql\u65f6\uff0cparameterObject\u4e2d\u5e76\u6ca1\u6709\u5199\u5165\u52a0\u5bc6\u540e\u7684\u53c2\u6570\uff0c\u5728\u5224\u65ad\u6807\u7b7e\u65f6\u5fc5\u5b9a\u4e3a\u5426\uff0c\u6700\u540e\u751f\u6210\u7684\u9759\u6001SQL\u5fc5\u7136\u4e0d\u5305\u542b\u52a0\u5bc6\u540e\u7684\u5b57\u6bb5\uff0c\u540e\u7eed\u4e0d\u7ba1\u6211\u4eec\u5728StatementHandler\u548cParameterHandler\u4e2d\u600e\u4e48\u5904\u7406parameterObject\uff0c\u90fd\u65e0\u6cd5\u5b9e\u73b0\u5165\u53c2\u7684\u52a0\u5bc6\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u5728\u5165\u53c2\u7684\u52a0\u5bc6\u4e0a\u6211\u4eec\u53ea\u80fd\u9009\u62e9\u62e6\u622aExecutor\u7684update\u548cquery\u65b9\u6cd5\u3002<\/p>\n<p>\u90a3\u4e48\u8fd4\u56de\u503c\u7684\u89e3\u5bc6\u5462\uff1f\u53c2\u8003\u6d41\u7a0b\u56fe\uff0c\u6211\u4eec\u80fd\u5bf9ResultSetHandler\u548cExecutor\u505a\u62e6\u622a\uff0c\u4e8b\u5b9e\u4e5f\u786e\u5b9e\u5982\u6b64\uff0c\u5728\u5904\u7406\u8fd4\u56de\u503c\u8fd9\u4e00\u70b9\u4e0a\uff0c\u8fd9\u4e24\u8005\u662f\u7b49\u4ef7\u7684\uff0cResultSetHandler.handleResultSet()\u7684\u8fd4\u56de\u503c\u76f4\u63a5\u900f\u4f20\u7ed9Executor\uff0c\u518d\u7531Executor\u900f\u4f20\u7ed9SqlSession\uff0c\u6240\u4ee5\u4e24\u8005\u4efb\u9009\u5176\u4e00\u5c31\u53ef\u4ee5\u3002<\/p>\n<h2>\u56db\u3001\u65b9\u6848\u5b9e\u65bd<\/h2>\n<p>\u5728\u77e5\u9053\u9700\u8981\u62e6\u622a\u7684\u5bf9\u8c61\u540e\uff0c\u5c31\u53ef\u4ee5\u5f00\u59cb\u5b9e\u73b0\u52a0\u89e3\u5bc6\u63d2\u4ef6\u4e86\u3002\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\u7ef4\u5ea6\u7684\u6ce8\u89e3\u3002<\/p>\n<pre><code>\/**\n * \u901a\u8fc7\u6ce8\u89e3\u6765\u8868\u660e\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u90a3\u4e2a\u5b57\u6bb5\u8fdb\u884c\u52a0\u5bc6\n *\/\n@Target({ ElementType.METHOD })\n@Retention(RetentionPolicy.RUNTIME)\n@Inherited\n@Documented\npublic @interface TEncrypt {\n    \/**\n     * \u52a0\u5bc6\u65f6\u4ecesrcKey\u5230destKey\n     * @return\n     *\/\n    String[] srcKey() default {};\n \n    \/**\n     * \u89e3\u5bc6\u65f6\u4ecedestKey\u5230srcKey\n     * @return\n     *\/\n    String[] destKey() default {};\n}<\/code><\/pre>\n<p>\u5c06\u8be5\u6ce8\u89e3\u6253\u5728\u9700\u8981\u52a0\u89e3\u5bc6\u7684DAO\u5c42\u65b9\u6cd5\u4e0a\u3002<\/p>\n<pre><code>UserMapper.java\npublic interface UserMapper {\n    @TEncrypt(srcKey = {&quot;secret&quot;}, destKey = {&quot;secretCiper&quot;})\n    List&lt;UserInfo&gt; selectUserList(UserInfo userInfo);\n    }<\/code><\/pre>\n<p>\u4fee\u6539xxxMapper.xml\u6587\u4ef6<\/p>\n<pre><code>&lt;mapper namespace=&quot;com.xxx.internet.demo.mapper.UserMapper&quot;&gt;\n    &lt;resultMap id=&quot;BaseResultMap&quot; type=&quot;com.xxx.internet.demo.entity.UserInfo&quot;&gt;\n        &lt;id column=&quot;id&quot; jdbcType=&quot;BIGINT&quot; property=&quot;id&quot; \/&gt;\n        &lt;id column=&quot;phone&quot; jdbcType=&quot;VARCHAR&quot; property=&quot;phone&quot;\/&gt;\n        &lt;id column=&quot;secret&quot; jdbcType=&quot;VARCHAR&quot; property=&quot;secret&quot;\/&gt;\n&lt;!--        \u52a0\u5bc6\u540e\u6620\u5c04--&gt;\n        &lt;id column=&quot;secret_ciper&quot; jdbcType=&quot;VARCHAR&quot; property=&quot;secretCiper&quot;\/&gt;\n        &lt;id column=&quot;name&quot; jdbcType=&quot;VARCHAR&quot; property=&quot;name&quot; \/&gt;\n    &lt;\/resultMap&gt;\n \n    &lt;select id=&quot;selectUserList&quot; resultMap=&quot;BaseResultMap&quot; parameterType=&quot;com.xxx.internet.demo.entity.UserInfo&quot;&gt;\n        SELECT\n        *\n        FROM\n        `t_user_info`\n        &lt;where&gt;\n            &lt;if test=&quot;phone != null&quot;&gt;\n                `phone` = #{phone}\n            &lt;\/if&gt;\n&lt;!--            \u660e\u6587\u5b57\u6bb5--&gt;\n            &lt;if test=&quot;secret != null&quot;&gt;\n                AND `secret` = #{secret}\n            &lt;\/if&gt;\n&lt;!--            \u52a0\u5bc6\u540e\u5b57\u6bb5--&gt;\n            &lt;if test=&quot;secretCiper != null&quot;&gt;\n                AND `secret_ciper` = #{secretCiper}\n            &lt;\/if&gt;\n            &lt;if test=&quot;name&quot;&gt;\n                AND `name` = #{name}\n            &lt;\/if&gt;\n        &lt;\/where&gt;\n        ORDER BY `update_time` DESCv\n    &lt;\/select&gt;\n&lt;\/mapper&gt;<\/code><\/pre>\n<p>\u505a\u5b8c\u4e0a\u9762\u7684\u4fee\u6539\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7f16\u5199\u52a0\u5bc6\u63d2\u4ef6\u4e86<\/p>\n<pre><code>@Intercepts({ @Signature(type = Executor.class, method = &quot;update&quot;, args = { MappedStatement.class, Object.class }),\n        @Signature(type = Executor.class, method = &quot;query&quot;, args = { MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class }) })\npublic class ExecutorEncryptInterceptor implements Interceptor {\n    private static final ObjectFactory        DEFAULT_OBJECT_FACTORY         = new DefaultObjectFactory();\n \n    private static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory();\n \n    private static final ReflectorFactory     REFLECTOR_FACTORY              = new DefaultReflectorFactory();\n \n    private static final List&lt;String&gt;         COLLECTION_NAME  = Arrays.asList(&quot;list&quot;);\n \n    private static final String               COUNT_SUFFIX                   = &quot;_COUNT&quot;;\n \n    @Override\n    public Object intercept(Invocation invocation) throws Throwable {\n \n        \/\/ \u83b7\u53d6\u62e6\u622a\u5668\u62e6\u622a\u7684\u8bbe\u7f6e\u53c2\u6570\u5bf9\u8c61DefaultParameterHandler\n        final Object[] args = invocation.getArgs();\n        MappedStatement mappedStatement = (MappedStatement) args[0];\n        Object parameterObject = args[1];\n \n        \/\/ id\u5b57\u6bb5\u5bf9\u5e94\u6267\u884c\u7684SQL\u7684\u65b9\u6cd5\u7684\u5168\u8def\u5f84\uff0c\u5305\u542b\u7c7b\u540d\u548c\u65b9\u6cd5\u540d\n        String id = mappedStatement.getId();\n        String className = id.substring(0, id.lastIndexOf(&quot;.&quot;));\n        String methodName = id.substring(id.lastIndexOf(&quot;.&quot;) + 1);\n \n        \/\/ \u5206\u9875\u63d2\u4ef6\u4f1a\u751f\u6210\u4e00\u4e2acount\u8bed\u53e5\uff0c\u8fd9\u4e2a\u8bed\u53e5\u7684\u53c2\u6570\u4e5f\u8981\u505a\u5904\u7406\n        if (methodName.endsWith(COUNT_SUFFIX)) {\n            methodName = methodName.substring(0, methodName.lastIndexOf(COUNT_SUFFIX));\n        }\n \n        \/\/ \u52a8\u6001\u52a0\u8f7d\u7c7b\u5e76\u83b7\u53d6\u7c7b\u4e2d\u7684\u65b9\u6cd5\n        final Method[] methods = Class.forName(className).getMethods();\n \n        \/\/ \u904d\u5386\u7c7b\u7684\u6240\u6709\u65b9\u6cd5\u5e76\u627e\u5230\u6b64\u6b21\u8c03\u7528\u7684\u65b9\u6cd5\n        for (Method method : methods) {\n            if (method.getName().equalsIgnoreCase(methodName) &amp;&amp; method.isAnnotationPresent(TEncrypt.class)) {\n \n                \/\/ \u83b7\u53d6\u65b9\u6cd5\u4e0a\u7684\u6ce8\u89e3\u4ee5\u53ca\u6ce8\u89e3\u5bf9\u5e94\u7684\u53c2\u6570\n                TEncrypt paramAnnotation = method.getAnnotation(TEncrypt.class);\n \n                \/\/ \u652f\u6301\u52a0\u5bc6\u7684\u64cd\u4f5c\uff0c\u8fd9\u91cc\u53ea\u4fee\u6539\u53c2\u6570\n                if (parameterObject instanceof Map) {\n                    List&lt;String&gt; paramAnnotations = findParams(method);\n                    parameterMapHandler((Map) parameterObject, paramAnnotation, mappedStatement.getSqlCommandType(), paramAnnotations);\n                } else {\n                    encryptParam(parameterObject, paramAnnotation, mappedStatement.getSqlCommandType());\n                }\n            }\n        }\n \n        return invocation.proceed();\n    }\n}<\/code><\/pre>\n<p>\u52a0\u5bc6\u7684\u4e3b\u4f53\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5224\u65ad\u672c\u6b21\u8c03\u7528\u7684\u65b9\u6cd5\u4e0a\u662f\u5426\u6ce8\u89e3\u4e86@TEncrypt\u3002<\/li>\n<li>\u83b7\u53d6\u6ce8\u89e3\u4ee5\u53ca\u5728\u6ce8\u89e3\u4e0a\u914d\u7f6e\u7684\u53c2\u6570\u3002<\/li>\n<li>\u904d\u5386parameterObject\uff0c\u627e\u5230\u9700\u8981\u52a0\u5bc6\u7684\u5b57\u6bb5\u3002<\/li>\n<li>\u8c03\u7528\u52a0\u5bc6\u65b9\u6cd5\uff0c\u5f97\u5230\u52a0\u5bc6\u540e\u7684\u503c\u3002<\/li>\n<li>\u5c06\u52a0\u5bc6\u540e\u7684\u5b57\u6bb5\u548c\u503c\u5199\u5165parameterObject\u3002<\/li>\n<\/ol>\n<p>\u96be\u70b9\u4e3b\u8981\u5728parameterObject\u7684\u89e3\u6790\uff0c\u5230\u4e86Executor\u8fd9\u4e00\u5c42\uff0cparameterObject\u5df2\u7ecf\u4e0d\u518d\u662f\u7b80\u5355\u7684Object[]\uff0c\u800c\u662f\u7531MapperMethod.convertArgsToSqlCommandParam(Object[] args)\u65b9\u6cd5\u521b\u5efa\u7684\u4e00\u4e2a\u5bf9\u8c61\uff0c\u65e2\u7136\u8981\u5bf9\u8fd9\u4e2a\u5bf9\u8c61\u505a\u5904\u7406\uff0c\u6211\u4eec\u80af\u5b9a\u5f97\u5148\u77e5\u9053\u5b83\u7684\u521b\u5efa\u8fc7\u7a0b\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042329724\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u53c2\u8003\u4e0a\u56feparameterObject\u7684\u521b\u5efa\u8fc7\u7a0b\uff0c\u52a0\u5bc6\u63d2\u4ef6\u5bf9parameterObject\u7684\u5904\u7406\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u9006\u5411\u7684\u8fc7\u7a0b\u3002\u5982\u679c\u662flist\uff0c\u6211\u4eec\u5c31\u904d\u5386list\u91cc\u7684\u6bcf\u4e00\u4e2a\u503c\uff0c\u5982\u679c\u662fmap\uff0c\u6211\u4eec\u5c31\u904d\u5386map\u91cc\u7684\u6bcf\u4e00\u4e2a\u503c\u3002<\/p>\n<p>\u5f97\u5230\u9700\u8981\u5904\u7406\u7684Object\u540e\uff0c\u518d\u904d\u5386Object\u91cc\u7684\u6bcf\u4e2a\u5c5e\u6027\uff0c\u5224\u65ad\u662f\u5426\u5728@TEncrypt\u6ce8\u89e3\u7684srcKeys\u53c2\u6570\u4e2d\uff0c\u5982\u679c\u662f\uff0c\u5219\u52a0\u5bc6\u518d\u8bbe\u7f6e\u5230Object\u4e2d\u3002<\/p>\n<p>\u89e3\u5bc6\u63d2\u4ef6\u7684\u903b\u8f91\u548c\u52a0\u5bc6\u63d2\u4ef6\u57fa\u672c\u4e00\u81f4\uff0c\u8fd9\u91cc\u4e0d\u518d\u8d58\u8ff0\u3002<\/p>\n<h2>\u4e94\u3001\u95ee\u9898\u6311\u6218<\/h2>\n<h3>5.1 \u5206\u9875\u63d2\u4ef6\u81ea\u52a8\u751f\u6210count\u8bed\u53e5<\/h3>\n<p>\u4e1a\u52a1\u4ee3\u7801\u91cc\u5f88\u591a\u5730\u65b9\u90fd\u7528\u4e86 com.github.pagehelper \u8fdb\u884c\u7269\u7406\u5206\u9875\uff0c\u53c2\u8003\u4e0b\u9762\u7684demo\uff0c\u5728\u4f7f\u7528PageRowBounds\u65f6\uff0cpagehelper\u63d2\u4ef6\u4f1a\u5e2e\u6211\u4eec\u83b7\u53d6\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\u603b\u6570\u5e76\u8bbe\u7f6e\u5230rowBounds\u5bf9\u8c61\u7684total\u5c5e\u6027\u4e2d\u3002<\/p>\n<pre><code>PageRowBounds rowBounds = new PageRowBounds(0, 10);\nList&lt;User&gt; list = userMapper.selectIf(1, rowBounds);\nlong total = rowBounds.getTotal();<\/code><\/pre>\n<p>\u90a3\u4e48\u95ee\u9898\u6765\u4e86\uff0c\u8868\u9762\u4e0a\u770b\uff0c\u6211\u4eec\u53ea\u6267\u884c\u4e86userMapper.selectIf(1, rowBounds)\u8fd9\u4e00\u6761\u8bed\u53e5\uff0c\u800cpagehelper\u662f\u901a\u8fc7\u6539\u5199SQL\u589e\u52a0limit\u3001offset\u5b9e\u73b0\u7684\u7269\u7406\u5206\u9875\uff0c\u5728\u6574\u4e2a\u8bed\u53e5\u7684\u6267\u884c\u8fc7\u7a0b\u4e2d\u6ca1\u6709\u4ece\u6570\u636e\u5e93\u91cc\u628a\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\u8bfb\u51fa\u6765\uff0c\u90a3\u4e48pagehelper\u662f\u600e\u4e48\u5f97\u5230\u6570\u636e\u7684\u603b\u6570\u7684\u5462\uff1f<\/p>\n<p>\u7b54\u6848\u662fpagehelper\u4f1a\u518d\u6267\u884c\u4e00\u6761count\u8bed\u53e5\u3002\u5148\u4e0d\u8bf4\u989d\u5916\u4e00\u6761\u6267\u884ccount\u8bed\u53e5\u7684\u539f\u7406\uff0c\u6211\u4eec\u5148\u770b\u770b\u52a0\u4e86\u4e00\u6761count\u8bed\u53e5\u4f1a\u5bfc\u81f4\u4ec0\u4e48\u95ee\u9898\u3002<\/p>\n<p>\u53c2\u8003\u4e4b\u524d\u7684selectUserList\u63a5\u53e3\uff0c\u5047\u8bbe\u6211\u4eec\u60f3\u9009\u62e9secret\u4e3a\u67d0\u4e2a\u503c\u7684\u6570\u636e\uff0c\u90a3\u4e48\u7ecf\u8fc7\u52a0\u5bc6\u63d2\u4ef6\u7684\u5904\u7406\u540e\u6700\u7ec8\u6267\u884c\u7684\u5927\u81f4\u662f\u8fd9\u6837\u4e00\u6761\u8bed\u53e5 &#8220;select * from t\/_user\/_info where secret\/_ciper = ? order by update\/_time limit ?, ?&#8221;\u3002<\/p>\n<p>\u4f46\u7531\u4e8epagehelper\u8fd8\u4f1a\u518d\u6267\u884c\u4e00\u6761\u8bed\u53e5\uff0c\u800c\u7531\u4e8e\u8be5\u8bed\u53e5\u5e76\u6ca1\u6709 @TEncrypt \u6ce8\u89e3\uff0c\u6240\u4ee5\u662f\u4e0d\u4f1a\u88ab\u52a0\u5bc6\u63d2\u4ef6\u62e6\u622a\u7684\uff0c\u6700\u7ec8\u6267\u884c\u7684count\u8bed\u53e5\u662f\u7c7b\u4f3c\u8fd9\u6837\u7684: &#8220;select count(*) from t\/_user\/_info where secret = ? order by update_time&#8221;\u3002<\/p>\n<p>\u53ef\u4ee5\u660e\u663e\u7684\u770b\u5230\u7b2c\u4e00\u6761\u8bed\u53e5\u662f\u4f7f\u7528secret_ciper\u4f5c\u4e3a\u67e5\u8be2\u6761\u4ef6\uff0c\u800ccount\u8bed\u53e5\u662f\u4f7f\u7528secret\u4f5c\u4e3a\u67e5\u8be2\u6761\u4ef6\uff0c\u4f1a\u5bfc\u81f4\u6700\u7ec8\u5f97\u5230\u7684\u6570\u636e\u603b\u91cf\u548c\u5b9e\u9645\u7684\u6570\u636e\u603b\u91cf\u4e0d\u4e00\u81f4\u3002<\/p>\n<p>\u56e0\u6b64\u6211\u4eec\u5728\u52a0\u5bc6\u63d2\u4ef6\u7684\u4ee3\u7801\u91cc\u5bf9count\u8bed\u53e5\u505a\u4e86\u7279\u6b8a\u5904\u7406\uff0c\u7531\u4e8epagehelper\u65b0\u589e\u7684count\u8bed\u53e5\u5bf9\u5e94\u7684mappedStatement\u7684id\u56fa\u5b9a\u4ee5&#8221;\/_COUNT&#8221;\u7ed3\u5c3e\uff0c\u800c\u8fd9\u4e2aid\u5c31\u662f\u5bf9\u5e94\u7684mapper\u91cc\u7684\u65b9\u6cd5\u7684\u5168\u8def\u5f84\uff0c\u4e3e\u4f8b\u6765\u8bf4\u539f\u59cb\u8bed\u53e5\u7684id\u662f&#8221;com.xxx.internet.demo.entity.UserInfo.selectUserList&#8221;\uff0c\u90a3\u4e48count\u8bed\u53e5\u7684id\u5c31\u662f&#8221;com.xxx.internet.demo.entity.UserInfo.selectUserList\/_COUNT&#8221;\uff0c\u53bb\u6389&#8221;_COUNT&#8221;\u540e\u6211\u4eec\u518d\u5224\u65ad\u5bf9\u5e94\u7684\u65b9\u6cd5\u4e0a\u6709\u6ca1\u6709\u6ce8\u89e3\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u4f7f\u7528 MyBatis \u63d2\u4ef6\u5b9e\u73b0\u6570\u636e\u5e93\u5b57\u6bb5\u52a0\u89e3\u5bc6\u7684\u63a2\u7d22\u8fc7\u7a0b\uff0c\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u9700\u8981\u6ce8\u610f\u7684\u7ec6\u8282\u6bd4\u8f83\u591a\uff0c\u6574\u4e2a\u6d41\u7a0b\u4e0b\u6765\u6211\u5bf9 MyBatis \u7684\u7406\u89e3\u4e5f\u52a0\u6df1\u4e86\u3002\u603b\u7684\u6765\u8bf4\uff0c\u8fd9\u4e2a\u65b9\u6848\u6bd4\u8f83\u8f7b\u91cf\uff0c\u867d\u7136\u5bf9\u4e1a\u52a1\u4ee3\u7801\u6709\u4fb5\u5165\uff0c\u4f46\u80fd\u628a\u5f71\u54cd\u9762\u63a7\u5236\u5230\u6700\u5c0f\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042329719\">https:\/\/segmentfault.com\/a\/1190000042329719<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1avivo \u4e92\u8054\u7f51\u670d\u52a1\u5668\u56e2\u961f- Li Gang \u672c\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4f7f\u7528MyBatis\u63d2\u4ef6\u6765\u5b9e\u73b0\u6570\u636e\u5e93\u5b57\u6bb5\u52a0\u89e3\u5bc6\u7684 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006287","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006287"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006287\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}