{"id":160995,"date":"2024-11-04T02:44:24","date_gmt":"2024-11-03T18:44:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/160995\/"},"modified":"2024-11-04T02:44:25","modified_gmt":"2024-11-03T18:44:25","slug":"%e6%8f%9b%e7%a8%ae%e6%96%b9%e5%bc%8f%ef%bc%8c%e6%89%93%e5%8d%b0-mybatis-%e5%9f%b7%e8%a1%8c-sql-%e8%aa%9e%e5%8f%a5%ef%bc%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/160995\/","title":{"rendered":"\u63db\u7a2e\u65b9\u5f0f\uff0c\u6253\u5370 MyBatis \u57f7\u884c SQL \u8a9e\u53e5\uff01"},"content":{"rendered":"<h1 id=\"%e6%8f%9b%e7%a8%ae%e6%96%b9%e5%bc%8f%ef%bc%8c%e6%89%93%e5%8d%b0-mybatis-%e5%9f%b7%e8%a1%8c-sql-%e8%aa%9e%e5%8f%a5%ef%bc%81-fLbgHDCDuY\">\u63db\u7a2e\u65b9\u5f0f\uff0c\u6253\u5370 MyBatis \u57f7\u884c SQL \u8a9e\u53e5\uff01<\/h1>\n<p>\u5728 Java \u958b\u767c\u4e2d\uff0cMyBatis \u662f\u4e00\u500b\u975e\u5e38\u6d41\u884c\u7684\u6301\u4e45\u5c64\u6846\u67b6\uff0c\u5b83\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u8f15\u9b06\u5730\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u3002\u96d6\u7136 MyBatis \u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u529f\u80fd\uff0c\u4f46\u5728\u8abf\u8a66\u548c\u6392\u932f\u904e\u7a0b\u4e2d\uff0c\u6253\u5370\u57f7\u884c\u7684 SQL \u8a9e\u53e5\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4ee5\u4e0d\u540c\u7684\u65b9\u5f0f\u6253\u5370 MyBatis \u57f7\u884c\u7684 SQL \u8a9e\u53e5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u8abf\u8a66\u4ed6\u5011\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8-mybatis-%e7%9a%84%e6%97%a5%e8%aa%8c%e5%8a%9f%e8%83%bd-fLbgHDCDuY\">1. \u4f7f\u7528 MyBatis \u7684\u65e5\u8a8c\u529f\u80fd<\/h2>\n<p>MyBatis \u672c\u8eab\u652f\u6301\u591a\u7a2e\u65e5\u8a8c\u6846\u67b6\uff0c\u5982 Log4j\u3001SLF4J \u548c Commons Logging\u3002\u901a\u904e\u914d\u7f6e\u65e5\u8a8c\u6846\u67b6\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u6253\u5370\u51fa\u57f7\u884c\u7684 SQL \u8a9e\u53e5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 Log4j \u7684\u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre><code>log4j.rootLogger=DEBUG, stdout\n\nlog4j.appender.stdout=org.apache.log4j.ConsoleAppender\nlog4j.appender.stdout.layout=org.apache.log4j.PatternLayout\nlog4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n\n\nlog4j.logger.org.apache.ibatis=DEBUG<\/code><\/pre>\n<p>\u5728\u9019\u500b\u914d\u7f6e\u4e2d\uff0c\u6211\u5011\u5c07 MyBatis \u7684\u65e5\u8a8c\u7d1a\u5225\u8a2d\u7f6e\u70ba DEBUG\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u4e2d\u770b\u5230\u6240\u6709\u57f7\u884c\u7684 SQL \u8a9e\u53e5\u3002<\/p>\n<h2 id=\"2-%e4%bd%bf%e7%94%a8-mybatis-%e7%9a%84%e6%8f%92%e4%bb%b6%e6%a9%9f%e5%88%b6-fLbgHDCDuY\">2. \u4f7f\u7528 MyBatis \u7684\u63d2\u4ef6\u6a5f\u5236<\/h2>\n<p>MyBatis \u63d0\u4f9b\u4e86\u4e00\u500b\u63d2\u4ef6\u6a5f\u5236\uff0c\u5141\u8a31\u958b\u767c\u8005\u81ea\u5b9a\u7fa9\u884c\u70ba\u3002\u901a\u904e\u5be6\u73fe <code>Interceptor<\/code> \u63a5\u53e3\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6514\u622a SQL \u8a9e\u53e5\u7684\u57f7\u884c\uff0c\u4e26\u6253\u5370\u51fa\u4f86\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u63d2\u4ef6\u793a\u4f8b\uff1a<\/p>\n<pre><code>import org.apache.ibatis.executor.Executor;\nimport org.apache.ibatis.plugin.Interceptor;\nimport org.apache.ibatis.plugin.Invocation;\nimport org.apache.ibatis.plugin.Plugin;\nimport org.apache.ibatis.plugin.Signature;\n\nimport java.util.Properties;\n\n@Signature(type = Executor.class, method = \"update\", args = {String.class, Object.class})\npublic class SqlPrintInterceptor implements Interceptor {\n    @Override\n    public Object intercept(Invocation invocation) throws Throwable {\n        String sql = (String) invocation.getArgs()[0];\n        System.out.println(\"Executing SQL: \" + sql);\n        return invocation.proceed();\n    }\n\n    @Override\n    public Object plugin(Object target) {\n        return Plugin.wrap(target, this);\n    }\n\n    @Override\n    public void setProperties(Properties properties) {\n    }\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u6514\u622a\u4e86 <code>Executor.update<\/code> \u65b9\u6cd5\uff0c\u4e26\u6253\u5370\u51fa\u57f7\u884c\u7684 SQL \u8a9e\u53e5\u3002\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u8981\u64f4\u5c55\u9019\u500b\u63d2\u4ef6\uff0c\u4ee5\u6514\u622a\u5176\u4ed6\u65b9\u6cd5\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8-mybatis-%e7%9a%84%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6-fLbgHDCDuY\">3. \u4f7f\u7528 MyBatis \u7684\u914d\u7f6e\u6587\u4ef6<\/h2>\n<p>\u9664\u4e86\u4f7f\u7528\u65e5\u8a8c\u548c\u63d2\u4ef6\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u5728 MyBatis \u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u8a2d\u7f6e <code>logImpl<\/code> \u5c6c\u6027\uff0c\u4ee5\u6307\u5b9a\u4f7f\u7528\u7684\u65e5\u8a8c\u5be6\u73fe\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>&lt;configuration&gt;\n    &lt;settings&gt;\n        &lt;setting name=\"logImpl\" value=\"LOG4J\"\/&gt;\n    &lt;\/settings&gt;\n&lt;\/configuration&gt;<\/code><\/pre>\n<p>\u9019\u6a23\u914d\u7f6e\u5f8c\uff0cMyBatis \u5c07\u4f7f\u7528 Log4j \u4f86\u6253\u5370 SQL \u8a9e\u53e5\uff0c\u958b\u767c\u8005\u53ea\u9700\u78ba\u4fdd Log4j \u7684\u914d\u7f6e\u6b63\u78ba\u5373\u53ef\u3002<\/p>\n<h2 id=\"4-%e4%bd%bf%e7%94%a8-sql-%e7%9b%a3%e6%8e%a7%e5%b7%a5%e5%85%b7-fLbgHDCDuY\">4. \u4f7f\u7528 SQL \u76e3\u63a7\u5de5\u5177<\/h2>\n<p>\u9664\u4e86\u5728\u4ee3\u78bc\u4e2d\u6253\u5370 SQL \u8a9e\u53e5\uff0c\u958b\u767c\u8005\u9084\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b SQL \u76e3\u63a7\u5de5\u5177\u4f86\u6355\u7372\u548c\u5206\u6790 SQL \u8a9e\u53e5\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 MySQL \u7684\u6162\u67e5\u8a62\u65e5\u8a8c\u529f\u80fd\uff0c\u53ef\u4ee5\u8a18\u9304\u6240\u6709\u57f7\u884c\u6642\u9593\u8d85\u904e\u4e00\u5b9a\u95be\u503c\u7684 SQL \u8a9e\u53e5\u3002\u9019\u5c0d\u65bc\u6027\u80fd\u8abf\u512a\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-fLbgHDCDuY\">\u7e3d\u7d50<\/h2>\n<p>\u6253\u5370 MyBatis \u57f7\u884c\u7684 SQL \u8a9e\u53e5\u5c0d\u65bc\u8abf\u8a66\u548c\u6027\u80fd\u512a\u5316\u81f3\u95dc\u91cd\u8981\u3002\u901a\u904e\u4f7f\u7528\u65e5\u8a8c\u529f\u80fd\u3001\u63d2\u4ef6\u6a5f\u5236\u3001\u914d\u7f6e\u6587\u4ef6\u4ee5\u53ca SQL \u76e3\u63a7\u5de5\u5177\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u9748\u6d3b\u5730\u6355\u7372\u548c\u5206\u6790 SQL \u8a9e\u53e5\u3002\u9019\u4e9b\u65b9\u6cd5\u4e0d\u50c5\u80fd\u63d0\u9ad8\u958b\u767c\u6548\u7387\uff0c\u9084\u80fd\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u904e\u7a0b\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u958b\u767c\u74b0\u5883\u9084\u662f\u751f\u7522\u74b0\u5883\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4e91\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u5982\u4f55\u4ee5\u4e0d\u540c\u65b9\u5f0f\u6253\u5370 MyBatis \u57f7\u884c\u7684 SQL \u8a9e\u53e5\uff0c\u63d0\u5347\u8abf\u8a66\u6548\u7387\uff0c\u512a\u5316\u6578\u64da\u5eab\u64cd\u4f5c\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-160995","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=160995"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160995\/revisions"}],"predecessor-version":[{"id":160996,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160995\/revisions\/160996"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=160995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=160995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=160995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}