{"id":10008243,"date":"2023-09-07T20:58:06","date_gmt":"2023-09-07T12:58:06","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008243"},"modified":"2023-09-07T20:58:06","modified_gmt":"2023-09-07T12:58:06","slug":"mybatis-%e6%9e%b6%e6%9e%84%e4%b8%8e%e5%8e%9f%e7%90%86%e6%b7%b1%e5%85%a5%e8%a7%a3%e6%9e%90%ef%bc%8c%e9%9d%a2%e8%af%95%e9%9a%8f%e4%be%bf%e9%97%ae%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008243","title":{"rendered":"MyBatis \u67b6\u6784\u4e0e\u539f\u7406\u6df1\u5165\u89e3\u6790\uff0c\u9762\u8bd5\u968f\u4fbf\u95ee\uff01"},"content":{"rendered":"<p>\u4f5c\u8005\uff1a\u4e03\u5bf8\u77e5\u67b6\u6784 \/<br \/>\u94fe\u63a5\uff1a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=EAlZRzU84fO511KiPu3v4w%3D%3D.7QUcSnek4gTr7U1smMF8ItrYEKNawQEcAThpzTrMBy2rWzmXmMCiScUkv2B1yWkB\" rel=\"noopener noreferrer\">https:\/\/www.jianshu.com\/p\/ec40a82cae28<\/a><\/p>\n<h2>1 \u5f15\u8a00<\/h2>\n<p>\u672c\u6587\u4e3b\u8981\u8bb2\u89e3JDBC\u600e\u4e48\u6f14\u53d8\u5230Mybatis\u7684\u6e10\u53d8\u8fc7\u7a0b\uff0c<strong>\u91cd\u70b9\u8bb2\u89e3\u4e86\u4e3a\u4ec0\u4e48\u8981\u5c06JDBC\u5c01\u88c5\u6210Mybaits\u8fd9\u6837\u4e00\u4e2a\u6301\u4e45\u5c42\u6846\u67b6<\/strong>\u3002\u518d\u800c\u8bba\u8ff0Mybatis\u4f5c\u4e3a\u4e00\u4e2a\u6570\u636e\u6301\u4e45\u5c42\u6846\u67b6\u672c\u8eab\u6709\u5f85\u6539\u8fdb\u4e4b\u5904\u3002<\/p>\n<h2>2 JDBC\u5b9e\u73b0\u67e5\u8be2\u5206\u6790<\/h2>\n<p>\u6211\u4eec\u5148\u770b\u770b\u6211\u4eec\u6700\u719f\u6089\u4e5f\u662f\u6700\u57fa\u7840\u7684\u901a\u8fc7JDBC\u67e5\u8be2\u6570\u636e\u5e93\u6570\u636e\uff0c\u4e00\u822c\u9700\u8981\u4ee5\u4e0b\u4e03\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<blockquote>\n<ol>\n<li>\u52a0\u8f7dJDBC\u9a71\u52a8\uff1b<\/li>\n<li>\u5efa\u7acb\u5e76\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\uff1b<\/li>\n<li>\u521b\u5efa JDBC Statements \u5bf9\u8c61\uff1b<\/li>\n<li>\u8bbe\u7f6eSQL\u8bed\u53e5\u7684\u4f20\u5165\u53c2\u6570\uff1b<\/li>\n<li>\u6267\u884cSQL\u8bed\u53e5\u5e76\u83b7\u5f97\u67e5\u8be2\u7ed3\u679c\uff1b<\/li>\n<li>\u5bf9\u67e5\u8be2\u7ed3\u679c\u8fdb\u884c\u8f6c\u6362\u5904\u7406\u5e76\u5c06\u5904\u7406\u7ed3\u679c\u8fd4\u56de\uff1b<\/li>\n<li>\u91ca\u653e\u76f8\u5173\u8d44\u6e90\uff08\u5173\u95edConnection\uff0c\u5173\u95edStatement\uff0c\u5173\u95edResultSet\uff09\uff1b<\/li>\n<\/ol>\n<\/blockquote>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<\/p>\n<blockquote><p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=PYDBkOAzQr%2FtZDhex1EKWA%3D%3D.rNJO5MFLCi2mQ27n6di2prAUutW1b2PBkGZMeSom9cpma%2Fb6WIJ1hjnUmqHe2ytsrIpG8Jg6R0NSwmft0G6cPQ%3D%3D\" rel=\"noopener noreferrer\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p><\/blockquote>\n<p>\u4ee5\u4e0b\u662f\u5177\u4f53\u7684\u5b9e\u73b0\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"dart\">public static List&lt;Map&lt;String,Object&gt;&gt; queryForList(){  \n    Connection connection = null;  \n    ResultSet rs = null;  \n    PreparedStatement stmt = null;  \n    List&lt;Map&lt;String,Object&gt;&gt; resultList = new ArrayList&lt;Map&lt;String,Object&gt;&gt;();  \n          \n    try {  \n        \/\/ \u52a0\u8f7dJDBC\u9a71\u52a8  \n        Class.forName(&quot;oracle.jdbc.driver.OracleDriver&quot;).newInstance();  \n        String url = &quot;jdbc:oracle:thin:@localhost:1521:ORACLEDB&quot;;  \n              \n        String user = &quot;trainer&quot;;   \n        String password = &quot;trainer&quot;;   \n              \n        \/\/ \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5  \n        connection = DriverManager.getConnection(url,user,password);   \n              \n        String sql = &quot;select * from userinfo where user_id = ? &quot;;  \n        \/\/ \u521b\u5efaStatement\u5bf9\u8c61\uff08\u6bcf\u4e00\u4e2aStatement\u4e3a\u4e00\u6b21\u6570\u636e\u5e93\u6267\u884c\u8bf7\u6c42\uff09  \n        stmt = connection.prepareStatement(sql);  \n              \n        \/\/ \u8bbe\u7f6e\u4f20\u5165\u53c2\u6570  \n        stmt.setString(1, &quot;zhangsan&quot;);  \n              \n        \/\/ \u6267\u884cSQL\u8bed\u53e5  \n        rs = stmt.executeQuery();  \n              \n        \/\/ \u5904\u7406\u67e5\u8be2\u7ed3\u679c\uff08\u5c06\u67e5\u8be2\u7ed3\u679c\u8f6c\u6362\u6210List&lt;Map&gt;\u683c\u5f0f\uff09  \n        ResultSetMetaData rsmd = rs.getMetaData();  \n        int num = rsmd.getColumnCount();  \n              \n        while(rs.next()){  \n            Map map = new HashMap();  \n            for(int i = 0;i &lt; num;i++){  \n                String columnName = rsmd.getColumnName(i+1);  \n                map.put(columnName,rs.getString(columnName));  \n            }  \n            resultList.add(map);  \n        }  \n              \n    } catch (Exception e) {  \n        e.printStackTrace();  \n    } finally {  \n        try {  \n            \/\/ \u5173\u95ed\u7ed3\u679c\u96c6  \n            if (rs != null) {  \n                rs.close();  \n                rs = null;  \n            }  \n            \/\/ \u5173\u95ed\u6267\u884c  \n            if (stmt != null) {  \n                stmt.close();  \n                stmt = null;  \n            }  \n            if (connection != null) {  \n                connection.close();  \n                connection = null;  \n            }  \n        } catch (SQLException e) {  \n            e.printStackTrace();  \n        }  \n    }        \n    return resultList;  \n}<\/code><\/pre>\n<h2>3 JDBC\u6f14\u53d8\u5230Mybatis\u8fc7\u7a0b<\/h2>\n<p>\u4e0a\u9762\u6211\u4eec\u770b\u5230\u4e86\u5b9e\u73b0JDBC\u6709\u4e03\u4e2a\u6b65\u9aa4\uff0c\u54ea\u4e9b\u6b65\u9aa4\u662f\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5c01\u88c5\u7684\uff0c\u51cf\u5c11\u6211\u4eec\u5f00\u53d1\u7684\u4ee3\u7801\u91cf\u3002<\/p>\n<h3>3.1 \u7b2c\u4e00\u6b65\u4f18\u5316\uff1a\u8fde\u63a5\u83b7\u53d6\u548c\u91ca\u653e<\/h3>\n<ol>\n<li><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u6570\u636e\u5e93\u8fde\u63a5\u9891\u7e41\u7684\u5f00\u542f\u548c\u5173\u95ed\u672c\u8eab\u5c31\u9020\u6210\u4e86<strong>\u8d44\u6e90\u7684\u6d6a\u8d39\uff0c\u5f71\u54cd\u7cfb\u7edf\u7684\u6027\u80fd<\/strong>\u3002<\/p>\n<p><strong>\u89e3\u51b3\u95ee\u9898\uff1a<\/strong><\/p>\n<blockquote><p>\u6570\u636e\u5e93\u8fde\u63a5\u7684\u83b7\u53d6\u548c\u5173\u95ed\u6211\u4eec<strong>\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u6765\u89e3\u51b3\u8d44\u6e90\u6d6a\u8d39\u7684\u95ee\u9898<\/strong>\u3002\u901a\u8fc7\u8fde\u63a5\u6c60\u5c31\u53ef\u4ee5\u53cd\u590d\u5229\u7528\u5df2\u7ecf\u5efa\u7acb\u7684\u8fde\u63a5\u53bb\u8bbf\u95ee\u6570\u636e\u5e93\u4e86\u3002\u51cf\u5c11\u8fde\u63a5\u7684\u5f00\u542f\u548c\u5173\u95ed\u7684\u65f6\u95f4\u3002<\/p><\/blockquote>\n<ol>\n<li><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u4f46\u662f\u73b0\u5728<strong>\u8fde\u63a5\u6c60\u591a\u79cd\u591a\u6837\uff0c\u53ef\u80fd\u5b58\u5728\u53d8\u5316<\/strong>\uff0c\u6709\u53ef\u80fd\u91c7\u7528DBCP\u7684\u8fde\u63a5\u6c60\uff0c\u4e5f\u6709\u53ef\u80fd\u91c7\u7528\u5bb9\u5668\u672c\u8eab\u7684JNDI\u6570\u636e\u5e93\u8fde\u63a5\u6c60\u3002<\/p>\n<p><strong>\u89e3\u51b3\u95ee\u9898\uff1a<\/strong><\/p>\n<blockquote><p>\u6211\u4eec\u53ef\u4ee5<strong>\u901a\u8fc7DataSource\u8fdb\u884c\u9694\u79bb\u89e3\u8026<\/strong>\uff0c\u6211\u4eec\u7edf\u4e00\u4eceDataSource\u91cc\u9762\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\uff0c<strong>DataSource\u5177\u4f53\u7531DBCP\u5b9e\u73b0\u8fd8\u662f\u7531\u5bb9\u5668\u7684JNDI\u5b9e\u73b0\u90fd\u53ef\u4ee5<\/strong>\uff0c\u6240\u4ee5\u6211\u4eec\u5c06DataSource\u7684\u5177\u4f53\u5b9e\u73b0\u901a\u8fc7\u8ba9\u7528\u6237\u914d\u7f6e\u6765\u5e94\u5bf9\u53d8\u5316\u3002<\/p><\/blockquote>\n<h3>3.2 \u7b2c\u4e8c\u6b65\u4f18\u5316\uff1aSQL\u7edf\u4e00\u5b58\u53d6<\/h3>\n<ol>\n<li><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u6211\u4eec\u4f7f\u7528JDBC\u8fdb\u884c\u64cd\u4f5c\u6570\u636e\u5e93\u65f6\uff0c<strong>SQL\u8bed\u53e5\u57fa\u672c\u90fd\u6563\u843d\u5728\u5404\u4e2aJAVA\u7c7b\u4e2d<\/strong>\uff0c\u8fd9\u6837\u6709\u4e09\u4e2a\u4e0d\u8db3\u4e4b\u5904\uff1a<\/p>\n<blockquote>\n<p>\u7b2c\u4e00\uff0c\u53ef\u8bfb\u6027\u5f88\u5dee\uff0c\u4e0d\u5229\u4e8e\u7ef4\u62a4\u4ee5\u53ca\u505a\u6027\u80fd\u8c03\u4f18\u3002<\/p>\n<p>\u7b2c\u4e8c\uff0c\u6539\u52a8Java\u4ee3\u7801\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u3001\u6253\u5305\u90e8\u7f72\u3002<\/p>\n<p>\u7b2c\u4e09\uff0c\u4e0d\u5229\u4e8e\u53d6\u51faSQL\u5728\u6570\u636e\u5e93\u5ba2\u6237\u7aef\u6267\u884c\uff08\u53d6\u51fa\u540e\u8fd8\u5f97\u5220\u6389\u4e2d\u95f4\u7684Java\u4ee3\u7801\uff0c\u7f16\u5199\u597d\u7684SQL\u8bed\u53e5\u5199\u597d\u540e\u8fd8\u5f97\u901a\u8fc7\uff0b\u53f7\u5728Java\u8fdb\u884c\u62fc\u51d1\uff09\u3002<\/p>\n<\/blockquote>\n<p><strong>\u89e3\u51b3\u95ee\u9898\uff1a<\/strong><\/p>\n<blockquote>\n<p>\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u4e0d\u628aSQL\u8bed\u53e5\u5199\u5230Java\u4ee3\u7801\u4e2d\uff0c\u90a3\u4e48\u628aSQL\u8bed\u53e5\u653e\u5230\u54ea\u91cc\u5462\uff1f\u9996\u5148\u9700\u8981\u6709\u4e00\u4e2a\u7edf\u4e00\u5b58\u653e\u7684\u5730\u65b9\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u4e9b<strong>SQL\u8bed\u53e5\u7edf\u4e00\u96c6\u4e2d\u653e\u5230\u914d\u7f6e\u6587\u4ef6\u6216\u8005\u6570\u636e\u5e93\u91cc\u9762\uff08\u4ee5key-value\u7684\u683c\u5f0f\u5b58\u653e\uff09<\/strong>\u3002\u7136\u540e\u901a\u8fc7SQL\u8bed\u53e5\u7684key\u503c\u53bb\u83b7\u53d6\u5bf9\u5e94\u7684SQL\u8bed\u53e5\u3002<\/p>\n<p>\u65e2\u7136\u6211\u4eec\u5c06SQL\u8bed\u53e5\u90fd\u7edf\u4e00\u653e\u5728\u914d\u7f6e\u6587\u4ef6\u6216\u8005\u6570\u636e\u5e93\u4e2d\uff0c<strong>\u90a3\u4e48\u8fd9\u91cc\u5c31\u6d89\u53ca\u4e00\u4e2aSQL\u8bed\u53e5\u7684\u52a0\u8f7d\u95ee\u9898<\/strong>\u3002<\/p>\n<\/blockquote>\n<h3>3.3 \u7b2c\u4e09\u6b65\u4f18\u5316\uff1a\u4f20\u5165\u53c2\u6570\u6620\u5c04\u548c\u52a8\u6001SQL<\/h3>\n<ol>\n<li><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u901a\u8fc7\u5728SQL\u8bed\u53e5\u4e2d\u8bbe\u7f6e\u5360\u4f4d\u7b26\u6765\u8fbe\u5230\u4f7f\u7528\u4f20\u5165\u53c2\u6570\u7684\u76ee\u7684\uff0c\u8fd9\u79cd\u65b9\u5f0f\u672c\u8eab\u5c31\u6709\u4e00\u5b9a\u5c40\u9650\u6027\uff0c\u5b83\u662f\u6309\u7167\u4e00\u5b9a\u987a\u5e8f\u4f20\u5165\u53c2\u6570\u7684\uff0c\u8981\u4e0e\u5360\u4f4d\u7b26\u4e00\u4e00\u5339\u914d\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec<strong>\u4f20\u5165\u7684\u53c2\u6570\u662f\u4e0d\u786e\u5b9a\u7684<\/strong>\uff08\u6bd4\u5982\u5217\u8868\u67e5\u8be2\uff0c\u6839\u636e\u7528\u6237\u586b\u5199\u7684\u67e5\u8be2\u6761\u4ef6\u4e0d\u540c\uff0c\u4f20\u5165\u67e5\u8be2\u7684\u53c2\u6570\u4e5f\u662f\u4e0d\u540c\u7684\uff0c\u6709\u65f6\u662f\u4e00\u4e2a\u53c2\u6570\u3001\u6709\u65f6\u53ef\u80fd\u662f\u4e09\u4e2a\u53c2\u6570\uff09\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5f97<strong>\u5728\u540e\u53f0\u4ee3\u7801\u4e2d\u81ea\u5df1\u6839\u636e\u8bf7\u6c42\u7684\u4f20\u5165\u53c2\u6570\u53bb\u62fc\u51d1\u76f8\u5e94\u7684SQL\u8bed\u53e5<\/strong>\uff0c\u8fd9\u6837\u7684\u8bdd\u8fd8\u662f<strong>\u907f\u514d\u4e0d\u4e86\u5728Java\u4ee3\u7801\u91cc\u9762\u5199SQL\u8bed\u53e5\u7684\u547d\u8fd0<\/strong>\u3002\u65e2\u7136\u6211\u4eec\u5df2\u7ecf\u628aSQL\u8bed\u53e5\u7edf\u4e00\u5b58\u653e\u5728\u914d\u7f6e\u6587\u4ef6\u6216\u8005\u6570\u636e\u5e93\u4e2d\u4e86\uff0c<strong>\u600e\u4e48\u505a\u5230\u80fd\u591f\u6839\u636e\u524d\u53f0\u4f20\u5165\u53c2\u6570\u7684\u4e0d\u540c\uff0c\u52a8\u6001\u751f\u6210\u5bf9\u5e94\u7684SQL\u8bed\u53e5\u5462\uff1f<\/strong><\/p>\n<p><strong>\u89e3\u51b3\u95ee\u9898\uff1a<\/strong><\/p>\n<blockquote>\n<p>\u7b2c\u4e00\uff0c\u6211\u4eec\u5148\u89e3\u51b3\u8fd9\u4e2a\u52a8\u6001\u95ee\u9898\uff0c<strong>\u6309\u7167\u6211\u4eec\u6b63\u5e38\u7684\u7a0b\u5e8f\u5458\u601d\u7ef4\u662f\uff0c\u901a\u8fc7if\u548celse\u8fd9\u7c7b\u7684\u5224\u65ad\u6765\u8fdb\u884c\u662f\u6700\u76f4\u89c2\u7684<\/strong>\uff0c\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u60f3\u5230\u4e86JSTL\u4e2d\u7684&lt;if test=&quot;&quot;&gt;&lt;\/if&gt;\u8fd9\u6837\u7684\u6807\u7b7e\uff0c\u90a3\u4e48\uff0c\u80fd\u4e0d\u80fd\u5c06\u8fd9\u7c7b\u7684\u6807\u7b7e\u5f15\u5165\u5230SQL\u8bed\u53e5\u4e2d\u5462\uff1f\u5047\u8bbe\u53ef\u4ee5\uff0c\u90a3\u4e48\u6211\u4eec\u8fd9\u91cc\u5c31\u9700\u8981\u4e00\u4e2a\u4e13\u95e8\u7684SQL\u89e3\u6790\u5668\u6765\u89e3\u6790\u8fd9\u6837\u7684SQL\u8bed\u53e5\uff0c\u4f46\u662f\uff0cif\u5224\u65ad\u7684\u53d8\u91cf\u6765\u81ea\u4e8e\u54ea\u91cc\u5462\uff1f\u4f20\u5165\u7684\u503c\u672c\u8eab\u662f\u53ef\u53d8\u7684\uff0c\u90a3\u4e48\u6211\u4eec\u5f97\u4e3a\u8fd9\u4e2a\u503c\u5b9a\u4e49\u4e00\u4e2a\u4e0d\u53d8\u7684\u53d8\u91cf\u540d\u79f0\uff0c\u800c\u4e14\u8fd9\u4e2a\u53d8\u91cf\u540d\u79f0\u5fc5\u987b\u548c\u5bf9\u5e94\u7684\u503c\u8981\u6709\u5bf9\u5e94\u5173\u7cfb\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u53d8\u91cf\u540d\u79f0\u627e\u5230\u5bf9\u5e94\u7684\u503c\uff0c\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u60f3\u5230\u4e86key-value\u7684Map\u3002\u89e3\u6790\u7684\u65f6\u5019\u6839\u636e\u53d8\u91cf\u540d\u7684\u5177\u4f53\u503c\u6765\u5224\u65ad\u3002<\/p>\n<p>\u5047\u5982\u524d\u9762\u53ef\u4ee5\u5224\u65ad\u6ca1\u6709\u95ee\u9898\uff0c\u90a3\u4e48\u5047\u5982\u5224\u65ad\u7684\u7ed3\u679c\u662ftrue\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u8f93\u51fa\u7684\u6807\u7b7e\u91cc\u9762\u7684SQL\u7247\u6bb5\uff0c\u4f46\u662f\u600e\u4e48\u89e3\u51b3\u5728\u6807\u7b7e\u91cc\u9762\u4f7f\u7528\u53d8\u91cf\u540d\u79f0\u7684\u95ee\u9898\u5462\uff1f\u8fd9\u91cc\u6211\u4eec\u9700\u8981<strong>\u4f7f\u7528\u4e00\u79cd\u6709\u522b\u4e8eSQL\u7684\u8bed\u6cd5\u6765\u5d4c\u5165\u53d8\u91cf\uff08\u6bd4\u5982\u4f7f\u7528\uff03\u53d8\u91cf\u540d\uff03\uff09<\/strong>\u3002\u8fd9\u6837\uff0cSQL\u8bed\u53e5\u7ecf\u8fc7\u89e3\u6790\u540e\u5c31\u53ef\u4ee5\u52a8\u6001\u7684\u751f\u6210\u7b26\u5408\u4e0a\u4e0b\u6587\u7684SQL\u8bed\u53e5\u3002<\/p>\n<p>\u8fd8\u6709\uff0c<strong>\u600e\u4e48\u533a\u5206\u5f00\u5360\u4f4d\u7b26\u53d8\u91cf\u548c\u975e\u5360\u4f4d\u53d8\u91cf\uff1f<\/strong>\u6709\u65f6\u5019\u6211\u4eec\u5355\u5355\u4f7f\u7528\u5360\u4f4d\u7b26\u662f\u6ee1\u8db3\u4e0d\u4e86\u7684\uff0c\u5360\u4f4d\u7b26\u53ea\u80fd\u4e3a\u67e5\u8be2\u6761\u4ef6\u5360\u4f4d\uff0cSQL\u8bed\u53e5\u5176\u4ed6\u5730\u65b9\u4f7f\u7528\u4e0d\u4e86\u3002<strong>\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528#\u53d8\u91cf\u540d#\u8868\u793a\u5360\u4f4d\u7b26\u53d8\u91cf\uff0c\u4f7f\u7528\u53d8\u91cf\u540d\u8868\u793a\u975e\u5360\u4f4d\u7b26\u53d8\u91cf<\/strong>\u3002<\/p>\n<\/blockquote>\n<h3>3.4 \u7b2c\u56db\u6b65\u4f18\u5316\uff1a\u7ed3\u679c\u6620\u5c04\u548c\u7ed3\u679c\u7f13\u5b58<\/h3>\n<ol>\n<li><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u6267\u884cSQL\u8bed\u53e5\u3001\u83b7\u53d6\u6267\u884c\u7ed3\u679c\u3001\u5bf9\u6267\u884c\u7ed3\u679c\u8fdb\u884c\u8f6c\u6362\u5904\u7406\u3001\u91ca\u653e\u76f8\u5173\u8d44\u6e90\u662f\u4e00\u6574\u5957\u4e0b\u6765\u7684\u3002\u5047\u5982\u662f\u6267\u884c\u67e5\u8be2\u8bed\u53e5\uff0c\u90a3\u4e48\u6267\u884cSQL\u8bed\u53e5\u540e\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u4e2aResultSet\u7ed3\u679c\u96c6\uff0c<strong>\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u5c31\u9700\u8981\u5c06ResultSet\u5bf9\u8c61\u7684\u6570\u636e\u53d6\u51fa\u6765\uff0c\u4e0d\u7136\u7b49\u5230\u91ca\u653e\u8d44\u6e90\u65f6\u5c31\u53d6\u4e0d\u5230\u8fd9\u4e9b\u7ed3\u679c\u4fe1\u606f\u4e86<\/strong>\u3002\u6211\u4eec\u4ece\u524d\u9762\u7684\u4f18\u5316\u6765\u770b\uff0c\u4ee5\u53ca\u5c06\u83b7\u53d6\u8fde\u63a5\u3001\u8bbe\u7f6e\u4f20\u5165\u53c2\u6570\u3001\u6267\u884cSQL\u8bed\u53e5\u3001\u91ca\u653e\u8d44\u6e90\u8fd9\u4e9b\u90fd\u5c01\u88c5\u8d77\u6765\u4e86\uff0c\u53ea\u5269\u4e0b\u7ed3\u679c\u5904\u7406\u8fd9\u5757\u8fd8\u6ca1\u6709\u8fdb\u884c\u5c01\u88c5\uff0c\u5982\u679c\u80fd\u5c01\u88c5\u8d77\u6765\uff0c\u6bcf\u4e2a\u6570\u636e\u5e93\u64cd\u4f5c\u90fd\u4e0d\u7528\u81ea\u5df1\u5199\u90a3\u4e48\u4e00\u5927\u5806Java\u4ee3\u7801\uff0c\u76f4\u63a5\u8c03\u7528\u4e00\u4e2a\u5c01\u88c5\u7684\u65b9\u6cd5\u5c31\u53ef\u4ee5\u641e\u5b9a\u4e86\u3002<\/p>\n<p><strong>\u89e3\u51b3\u95ee\u9898\uff1a<\/strong><\/p>\n<blockquote>\n<p>\u6211\u4eec\u5206\u6790\u4e00\u4e0b\uff0c\u4e00\u822c\u5bf9\u6267\u884c\u7ed3\u679c\u7684\u6709\u54ea\u4e9b\u5904\u7406\uff0c<strong>\u6709\u53ef\u80fd\u5c06\u7ed3\u679c\u4e0d\u505a\u4efb\u4f55\u5904\u7406\u5c31\u76f4\u63a5\u8fd4\u56de\uff0c\u4e5f\u6709\u53ef\u80fd\u5c06\u7ed3\u679c\u8f6c\u6362\u6210\u4e00\u4e2aJavaBean\u5bf9\u8c61\u8fd4\u56de\u3001\u4e00\u4e2aMap\u8fd4\u56de\u3001\u4e00\u4e2aList\u8fd4\u56de\u7b49<\/strong>`\uff0c\u7ed3\u679c\u5904\u7406\u53ef\u80fd\u662f\u591a\u79cd\u591a\u6837\u7684\u3002\u4ece\u8fd9\u91cc\u770b\uff0c\u6211\u4eec\u5fc5\u987b\u544a\u8bc9SQL\u5904\u7406\u5668\u4e24\u70b9\uff1a<strong>\u7b2c\u4e00\uff0c\u9700\u8981\u8fd4\u56de\u4ec0\u4e48\u7c7b\u578b\u7684\u5bf9\u8c61\uff1b\u7b2c\u4e8c\uff0c\u9700\u8981\u8fd4\u56de\u7684\u5bf9\u8c61\u7684\u6570\u636e\u7ed3\u6784\u600e\u4e48\u8ddf\u6267\u884c\u7684\u7ed3\u679c\u6620\u5c04<\/strong>\uff0c\u8fd9\u6837\u624d\u80fd\u5c06\u5177\u4f53\u7684\u503ccopy\u5230\u5bf9\u5e94\u7684\u6570\u636e\u7ed3\u6784\u4e0a\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c<strong>\u6211\u4eec\u53ef\u4ee5\u8fdb\u800c\u8003\u8651\u5bf9SQL\u6267\u884c\u7ed3\u679c\u7684\u7f13\u5b58\u6765\u63d0\u5347\u6027\u80fd<\/strong>\u3002\u7f13\u5b58\u6570\u636e\u90fd\u662fkey-value\u7684\u683c\u5f0f\uff0c\u90a3\u4e48\u8fd9\u4e2akey\u600e\u4e48\u6765\u5462\uff1f\u600e\u4e48\u4fdd\u8bc1\u552f\u4e00\u5462\uff1f\u5373\u4f7f\u540c\u4e00\u6761SQL\u8bed\u53e5\u51e0\u6b21\u8bbf\u95ee\u7684\u8fc7\u7a0b\u4e2d\u7531\u4e8e\u4f20\u5165\u53c2\u6570\u7684\u4e0d\u540c\uff0c\u5f97\u5230\u7684\u6267\u884cSQL\u8bed\u53e5\u4e5f\u662f\u4e0d\u540c\u7684\u3002\u90a3\u4e48\u7f13\u5b58\u8d77\u6765\u7684\u65f6\u5019\u662f\u591a\u5bf9\u3002<strong>\u4f46\u662fSQL\u8bed\u53e5\u548c\u4f20\u5165\u53c2\u6570\u4e24\u90e8\u5206\u5408\u8d77\u6765\u53ef\u4ee5\u4f5c\u4e3a\u6570\u636e\u7f13\u5b58\u7684key\u503c<\/strong>\u3002<\/p>\n<\/blockquote>\n<h3>3.5 \u7b2c\u4e94\u6b65\u4f18\u5316\uff1a\u89e3\u51b3\u91cd\u590dSQL\u8bed\u53e5\u95ee\u9898<\/h3>\n<ol>\n<li><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u7531\u4e8e\u6211\u4eec\u5c06\u6240\u6709SQL\u8bed\u53e5\u90fd\u653e\u5230\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c<strong>\u8fd9\u4e2a\u65f6\u5019\u4f1a\u9047\u5230\u4e00\u4e2aSQL\u91cd\u590d\u7684\u95ee\u9898<\/strong>\uff0c\u51e0\u4e2a\u529f\u80fd\u7684SQL\u8bed\u53e5\u5176\u5b9e\u90fd\u5dee\u4e0d\u591a\uff0c\u6709\u4e9b\u53ef\u80fd\u662fSELECT\u540e\u9762\u90a3\u6bb5\u4e0d\u540c\u3001\u6709\u4e9b\u53ef\u80fd\u662fWHERE\u8bed\u53e5\u4e0d\u540c\u3002\u6709\u65f6\u5019\u8868\u7ed3\u6784\u6539\u4e86\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u9700\u8981\u6539\u591a\u4e2a\u5730\u65b9\uff0c\u4e0d\u5229\u4e8e\u7ef4\u62a4\u3002<\/p>\n<p><strong>\u89e3\u51b3\u95ee\u9898\uff1a<\/strong><\/p>\n<blockquote><p>\u5f53\u6211\u4eec\u7684\u4ee3\u7801\u7a0b\u5e8f\u51fa\u73b0\u91cd\u590d\u4ee3\u7801\u65f6\u600e\u4e48\u529e\uff1f<strong>\u5c06\u91cd\u590d\u7684\u4ee3\u7801\u62bd\u79bb\u51fa\u6765\u6210\u4e3a\u72ec\u7acb\u7684\u4e00\u4e2a\u7c7b\uff0c\u7136\u540e\u5728\u5404\u4e2a\u9700\u8981\u4f7f\u7528\u7684\u5730\u65b9\u8fdb\u884c\u5f15\u7528<\/strong>\u3002\u5bf9\u4e8eSQL\u91cd\u590d\u7684\u95ee\u9898\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u91c7\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u901a\u8fc7\u5c06SQL\u7247\u6bb5\u6a21\u5757\u5316\uff0c<strong>\u5c06\u91cd\u590d\u7684SQL\u7247\u6bb5\u72ec\u7acb\u6210\u4e00\u4e2aSQL\u5757\uff0c\u7136\u540e\u5728\u5404\u4e2aSQL\u8bed\u53e5\u5f15\u7528\u91cd\u590d\u7684SQL\u5757<\/strong>\uff0c\u8fd9\u6837\u9700\u8981\u4fee\u6539\u65f6\u53ea\u9700\u8981\u4fee\u6539\u4e00\u5904\u5373\u53ef\u3002<\/p><\/blockquote>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<\/p>\n<blockquote><p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=EvdnXbKjmD1KQwqEnhiFYQ%3D%3D.MOjNF59bd53AnjdcQpieMjI%2BfzNT2zncp7uXlHgRat3Sl4u4dzyUXC%2BduDFv9EasnrORyAaDC%2Brt9aSIXjdScA%3D%3D\" rel=\"noopener noreferrer\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p><\/blockquote>\n<h2>4 Mybaits\u6709\u5f85\u6539\u8fdb\u4e4b\u5904<\/h2>\n<ol>\n<li><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/li>\n<\/ol>\n<p>Mybaits\u6240\u6709\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u90fd\u662f\u57fa\u4e8eSQL\u8bed\u53e5\uff0c<strong>\u5bfc\u81f4\u4ec0\u4e48\u6837\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u90fd\u8981\u5199SQL\u8bed\u53e5<\/strong>\u3002\u4e00\u4e2a\u5e94\u7528\u7cfb\u7edf\u8981\u5199\u7684SQL\u8bed\u53e5\u5b9e\u5728\u592a\u591a\u4e86\u3002<\/p>\n<p><strong>\u6539\u8fdb\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u6211\u4eec\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u7684\u64cd\u4f5c\u5927\u90e8\u5206\u90fd\u662f\u5bf9\u8868\u6570\u636e\u7684\u589e\u5220\u6539\u67e5\uff0c\u5f88\u591a\u90fd\u662f\u5bf9\u5355\u8868\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\uff0c\u7531\u8fd9\u70b9\u6211\u4eec\u53ef\u4ee5\u60f3\u5230\u4e00\u4e2a\u95ee\u9898\uff1a<strong>\u5355\u8868\u64cd\u4f5c\u53ef\u4e0d\u53ef\u4ee5\u4e0d\u5199SQL\u8bed\u53e5\uff0c\u901a\u8fc7JavaBean\u7684\u9ed8\u8ba4\u6620\u5c04\u5668\u751f\u6210\u5bf9\u5e94\u7684SQL\u8bed\u53e5<\/strong>\uff0c\u6bd4\u5982\uff1a\u4e00\u4e2a\u7c7bUserInfo\u5bf9\u5e94\u4e8eUSER_INFO\u8868\uff0c userId\u5c5e\u6027\u5bf9\u5e94\u4e8eUSER_ID\u5b57\u6bb5\u3002<strong>\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u53cd\u5c04\u53ef\u4ee5\u83b7\u53d6\u5230\u5bf9\u5e94\u7684\u8868\u7ed3\u6784\u4e86\uff0c\u62fc\u51d1\u6210\u5bf9\u5e94\u7684SQL\u8bed\u53e5\u663e\u7136\u4e0d\u662f\u95ee\u9898<\/strong>\u3002<\/p>\n<h2>5 MyBatis\u6846\u67b6\u6574\u4f53\u8bbe\u8ba1<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"580\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9Avw\" alt=\"MyBatis\u6846\u67b6\u6574\u4f53\u8bbe\u8ba1\" title=\"MyBatis\u6846\u67b6\u6574\u4f53\u8bbe\u8ba1\"><\/p>\n<h3>5.1 \u63a5\u53e3\u5c42-\u548c\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u65b9\u5f0f<\/h3>\n<p>MyBatis\u548c\u6570\u636e\u5e93\u7684\u4ea4\u4e92\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n<blockquote>\n<ol>\n<li>\u4f7f\u7528\u4f20\u7edf\u7684MyBatis\u63d0\u4f9b\u7684API\uff1b<\/li>\n<li>\u4f7f\u7528Mapper\u63a5\u53e3\uff1b<\/li>\n<\/ol>\n<\/blockquote>\n<h4>5.1.1 \u4f7f\u7528\u4f20\u7edf\u7684MyBatis\u63d0\u4f9b\u7684API<\/h4>\n<p><strong>\u8fd9\u662f\u4f20\u7edf\u7684\u4f20\u9012Statement Id \u548c\u67e5\u8be2\u53c2\u6570\u7ed9 SqlSession \u5bf9\u8c61\uff0c\u4f7f\u7528 SqlSession\u5bf9\u8c61\u5b8c\u6210\u548c\u6570\u636e\u5e93\u7684\u4ea4\u4e92<\/strong>\uff1bMyBatis\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u548c\u7b80\u5355\u7684API\uff0c\u4f9b\u7528\u6237\u5b9e\u73b0\u5bf9\u6570\u636e\u5e93\u7684\u589e\u5220\u6539\u67e5\u6570\u636e\u64cd\u4f5c\uff0c\u4ee5\u53ca\u5bf9\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u548cMyBatis \u81ea\u8eab\u914d\u7f6e\u4fe1\u606f\u7684\u7ef4\u62a4\u64cd\u4f5c\u3002<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"284\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9Avx\" alt=\"\u4f20\u7edf\u7684MyBatis\u5de5\u4f5c\u6a21\u5f0f\" title=\"\u4f20\u7edf\u7684MyBatis\u5de5\u4f5c\u6a21\u5f0f\" loading=\"lazy\"><\/p>\n<blockquote><p>\u4e0a\u8ff0\u4f7f\u7528MyBatis \u7684\u65b9\u6cd5\uff0c\u662f<strong>\u521b\u5efa\u4e00\u4e2a\u548c\u6570\u636e\u5e93\u6253\u4ea4\u9053\u7684SqlSession\u5bf9\u8c61\uff0c\u7136\u540e\u6839\u636eStatement Id \u548c\u53c2\u6570\u6765\u64cd\u4f5c\u6570\u636e\u5e93<\/strong>\uff0c\u8fd9\u79cd\u65b9\u5f0f\u56fa\u7136\u5f88\u7b80\u5355\u548c\u5b9e\u7528\uff0c\u4f46\u662f<strong>\u5b83\u4e0d\u7b26\u5408\u9762\u5411\u5bf9\u8c61\u8bed\u8a00\u7684\u6982\u5ff5\u548c\u9762\u5411\u63a5\u53e3\u7f16\u7a0b\u7684\u7f16\u7a0b\u4e60\u60ef<\/strong>\u3002\u7531\u4e8e\u9762\u5411\u63a5\u53e3\u7684\u7f16\u7a0b\u662f\u9762\u5411\u5bf9\u8c61\u7684\u5927\u8d8b\u52bf\uff0cMyBatis \u4e3a\u4e86\u9002\u5e94\u8fd9\u4e00\u8d8b\u52bf\uff0c\u589e\u52a0\u4e86\u7b2c\u4e8c\u79cd\u4f7f\u7528MyBatis \u652f\u6301\u63a5\u53e3\uff08Interface\uff09\u8c03\u7528\u65b9\u5f0f\u3002<\/p><\/blockquote>\n<h4>5.1.2 \u4f7f\u7528Mapper\u63a5\u53e3<\/h4>\n<p><strong>MyBatis \u5c06\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u4e2a&lt;mapper&gt; \u8282\u70b9\u62bd\u8c61\u4e3a\u4e00\u4e2a Mapper \u63a5\u53e3\uff1a<\/strong><\/p>\n<blockquote><p><strong>\u8fd9\u4e2a\u63a5\u53e3\u4e2d\u58f0\u660e\u7684\u65b9\u6cd5\u548c&lt;mapper&gt; \u8282\u70b9\u4e2d\u7684&lt;select|update|delete|insert&gt; \u8282\u70b9\u9879\u5bf9\u5e94<\/strong>\uff0c\u5373&lt;select|update|delete|insert&gt; \u8282\u70b9\u7684id\u503c\u4e3aMapper \u63a5\u53e3\u4e2d\u7684\u65b9\u6cd5\u540d\u79f0\uff0c<strong>parameterType \u503c\u8868\u793aMapper \u5bf9\u5e94\u65b9\u6cd5\u7684\u5165\u53c2\u7c7b\u578b<\/strong>\uff0c\u800c<strong>resultMap \u503c\u5219\u5bf9\u5e94\u4e86Mapper \u63a5\u53e3\u8868\u793a\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u6216\u8005\u8fd4\u56de\u7ed3\u679c\u96c6\u7684\u5143\u7d20\u7c7b\u578b<\/strong>\u3002<\/p><\/blockquote>\n<p><img decoding=\"async\" width=\"723\" height=\"354\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9Avy\" alt=\"Mapper\u63a5\u53e3\u548cMapper.xml\u914d\u7f6e\u6587\u4ef6\u4e4b\u95f4\u7684\u5bf9\u5e94\u5173\u7cfb\" title=\"Mapper\u63a5\u53e3\u548cMapper.xml\u914d\u7f6e\u6587\u4ef6\u4e4b\u95f4\u7684\u5bf9\u5e94\u5173\u7cfb\" loading=\"lazy\"><\/p>\n<blockquote><p><strong>\u6839\u636eMyBatis \u7684\u914d\u7f6e\u89c4\u8303\u914d\u7f6e\u597d\u540e\uff0c\u901a\u8fc7SqlSession.getMapper(XXXMapper.class)\u65b9\u6cd5\uff0cMyBatis \u4f1a\u6839\u636e\u76f8\u5e94\u7684\u63a5\u53e3\u58f0\u660e\u7684\u65b9\u6cd5\u4fe1\u606f\uff0c\u901a\u8fc7\u52a8\u6001\u4ee3\u7406\u673a\u5236\u751f\u6210\u4e00\u4e2aMapper \u5b9e\u4f8b<\/strong>\uff0c\u6211\u4eec\u4f7f\u7528Mapper\u63a5\u53e3\u7684\u67d0\u4e00\u4e2a\u65b9\u6cd5\u65f6\uff0cMyBatis\u4f1a\u6839\u636e\u8fd9\u4e2a\u65b9\u6cd5\u7684\u65b9\u6cd5\u540d\u548c\u53c2\u6570\u7c7b\u578b\uff0c\u786e\u5b9aStatement Id\uff0c\u5e95\u5c42\u8fd8\u662f\u901a\u8fc7SqlSession.select(&#8220;statementId&#8221;,parameterObject);\u6216\u8005SqlSession.update(&#8220;statementId&#8221;,parameterObject); \u7b49\u7b49\u6765\u5b9e\u73b0\u5bf9\u6570\u636e\u5e93\u7684\u64cd\u4f5c\uff0c<strong>MyBatis\u5f15\u7528Mapper \u63a5\u53e3\u8fd9\u79cd\u8c03\u7528\u65b9\u5f0f\uff0c\u7eaf\u7cb9\u662f\u4e3a\u4e86\u6ee1\u8db3\u9762\u5411\u63a5\u53e3\u7f16\u7a0b\u7684\u9700\u8981<\/strong>\u3002\uff08\u5176\u5b9e\u8fd8\u6709\u4e00\u4e2a\u539f\u56e0\u662f\u5728\u4e8e\uff0c\u9762\u5411\u63a5\u53e3\u7684\u7f16\u7a0b\uff0c\u4f7f\u5f97\u7528\u6237\u5728\u63a5\u53e3\u4e0a\u53ef\u4ee5\u4f7f\u7528\u6ce8\u89e3\u6765\u914d\u7f6eSQL\u8bed\u53e5\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u8131\u79bbXML\u914d\u7f6e\u6587\u4ef6\uff0c\u5b9e\u73b0\u201c0\u914d\u7f6e\u201d\uff09\u3002<\/p><\/blockquote>\n<h3>5.2 \u6570\u636e\u5904\u7406\u5c42<\/h3>\n<p><strong>\u6570\u636e\u5904\u7406\u5c42\u53ef\u4ee5\u8bf4\u662fMyBatis\u7684\u6838\u5fc3<\/strong>\uff0c\u4ece\u5927\u7684\u65b9\u9762\u4e0a\u8bb2\uff0c\u5b83\u8981\u5b8c\u6210\u4e24\u4e2a\u529f\u80fd\uff1a<\/p>\n<blockquote>\n<ol>\n<li>\u901a\u8fc7\u4f20\u5165\u53c2\u6570\u6784\u5efa\u52a8\u6001SQL\u8bed\u53e5\uff1b<\/li>\n<li>SQL\u8bed\u53e5\u7684\u6267\u884c\u4ee5\u53ca\u5c01\u88c5\u67e5\u8be2\u7ed3\u679c\u96c6\u6210List&lt;E&gt;\uff1b<\/li>\n<\/ol>\n<\/blockquote>\n<h4>5.2.1 \u53c2\u6570\u6620\u5c04\u548c\u52a8\u6001SQL\u8bed\u53e5\u751f\u6210<\/h4>\n<p>\u52a8\u6001\u8bed\u53e5\u751f\u6210\u53ef\u4ee5\u8bf4\u662fMyBatis\u6846\u67b6\u975e\u5e38\u4f18\u96c5\u7684\u4e00\u4e2a\u8bbe\u8ba1\uff0c<strong>MyBatis \u901a\u8fc7\u4f20\u5165\u7684\u53c2\u6570\u503c\uff0c\u4f7f\u7528 Ognl \u6765\u52a8\u6001\u5730\u6784\u9020SQL\u8bed\u53e5<\/strong>\uff0c\u4f7f\u5f97MyBatis \u6709\u5f88\u5f3a\u7684\u7075\u6d3b\u6027\u548c\u6269\u5c55\u6027\u3002<\/p>\n<p><strong>\u53c2\u6570\u6620\u5c04\u6307\u7684\u662f\u5bf9\u4e8ejava \u6570\u636e\u7c7b\u578b\u548cjdbc\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\uff1a<\/strong>\u8fd9\u91cc\u6709\u5305\u62ec\u4e24\u4e2a\u8fc7\u7a0b\uff1a<strong>\u67e5\u8be2\u9636\u6bb5<\/strong>\uff0c\u6211\u4eec\u8981\u5c06java\u7c7b\u578b\u7684\u6570\u636e\uff0c\u8f6c\u6362\u6210jdbc\u7c7b\u578b\u7684\u6570\u636e\uff0c\u901a\u8fc7 preparedStatement.setXXX() \u6765\u8bbe\u503c\uff1b\u53e6\u4e00\u4e2a\u5c31\u662f<strong>\u5bf9resultset\u67e5\u8be2\u7ed3\u679c\u96c6\u7684jdbcType \u6570\u636e\u8f6c\u6362\u6210java \u6570\u636e\u7c7b\u578b<\/strong>\u3002<\/p>\n<h4>5.2.2 SQL\u8bed\u53e5\u7684\u6267\u884c\u4ee5\u53ca\u5c01\u88c5\u67e5\u8be2\u7ed3\u679c\u96c6\u6210List&lt;E&gt;<\/h4>\n<p>\u52a8\u6001SQL\u8bed\u53e5\u751f\u6210\u4e4b\u540e\uff0cMyBatis \u5c06\u6267\u884cSQL\u8bed\u53e5\uff0c\u5e76\u5c06\u53ef\u80fd\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u8f6c\u6362\u6210List&lt;E&gt; \u5217\u8868\u3002<strong>MyBatis \u5728\u5bf9\u7ed3\u679c\u96c6\u7684\u5904\u7406\u4e2d\uff0c\u652f\u6301\u7ed3\u679c\u96c6\u5173\u7cfb\u4e00\u5bf9\u591a\u548c\u591a\u5bf9\u4e00\u7684\u8f6c\u6362<\/strong>\uff0c\u5e76\u4e14\u6709\u4e24\u79cd\u652f\u6301\u65b9\u5f0f\uff0c<strong>\u4e00\u79cd\u4e3a\u5d4c\u5957\u67e5\u8be2\u8bed\u53e5\u7684\u67e5\u8be2\uff0c\u8fd8\u6709\u4e00\u79cd\u662f\u5d4c\u5957\u7ed3\u679c\u96c6\u7684\u67e5\u8be2<\/strong>\u3002<\/p>\n<h3>5.3 \u6846\u67b6\u652f\u6491\u5c42<\/h3>\n<p>1\u3001\u4e8b\u52a1\u7ba1\u7406\u673a\u5236<\/p>\n<p><strong>\u4e8b\u52a1\u7ba1\u7406\u673a\u5236\u5bf9\u4e8eORM\u6846\u67b6\u800c\u8a00\u662f\u4e0d\u53ef\u7f3a\u5c11\u7684\u4e00\u90e8\u5206<\/strong>\uff0c\u4e8b\u52a1\u7ba1\u7406\u673a\u5236\u7684\u8d28\u91cf\u4e5f\u662f\u8003\u91cf\u4e00\u4e2aORM\u6846\u67b6\u662f\u5426\u4f18\u79c0\u7684\u4e00\u4e2a\u6807\u51c6\u3002<\/p>\n<p>2\u3001\u8fde\u63a5\u6c60\u7ba1\u7406\u673a\u5236<\/p>\n<p>\u7531\u4e8e\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\u6240\u5360\u7528\u7684\u8d44\u6e90\u6bd4\u8f83\u5927\uff0c<strong>\u5bf9\u4e8e\u6570\u636e\u541e\u5410\u91cf\u5927\u548c\u8bbf\u95ee\u91cf\u975e\u5e38\u5927\u7684\u5e94\u7528\u800c\u8a00\uff0c\u8fde\u63a5\u6c60\u7684\u8bbe\u8ba1\u5c31\u663e\u5f97\u975e\u5e38\u91cd\u8981<\/strong>\u3002<\/p>\n<p>3\u3001\u7f13\u5b58\u673a\u5236<\/p>\n<p>\u4e3a\u4e86\u63d0\u9ad8\u6570\u636e\u5229\u7528\u7387\u548c\u51cf\u5c0f\u670d\u52a1\u5668\u548c\u6570\u636e\u5e93\u7684\u538b\u529b\uff0c<strong>MyBatis \u4f1a\u5bf9\u4e8e\u4e00\u4e9b\u67e5\u8be2\u63d0\u4f9b\u4f1a\u8bdd\u7ea7\u522b\u7684\u6570\u636e\u7f13\u5b58<\/strong>\uff0c\u4f1a\u5c06\u5bf9\u67d0\u4e00\u6b21\u67e5\u8be2\uff0c\u653e\u7f6e\u5230SqlSession \u4e2d\uff0c\u5728\u5141\u8bb8\u7684\u65f6\u95f4\u95f4\u9694\u5185\uff0c\u5bf9\u4e8e\u5b8c\u5168\u76f8\u540c\u7684\u67e5\u8be2\uff0cMyBatis\u4f1a\u76f4\u63a5\u5c06\u7f13\u5b58\u7ed3\u679c\u8fd4\u56de\u7ed9\u7528\u6237\uff0c\u800c\u4e0d\u7528\u518d\u5230\u6570\u636e\u5e93\u4e2d\u67e5\u627e\u3002<\/p>\n<p>4\u3001SQL\u8bed\u53e5\u7684\u914d\u7f6e\u65b9\u5f0f<\/p>\n<p>\u4f20\u7edf\u7684MyBatis \u914d\u7f6eSQL\u8bed\u53e5\u65b9\u5f0f\u5c31\u662f\u4f7f\u7528XML\u6587\u4ef6\u8fdb\u884c\u914d\u7f6e\u7684\uff0c\u4f46\u662f\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u80fd\u5f88\u597d\u5730\u652f\u6301\u9762\u5411\u63a5\u53e3\u7f16\u7a0b\u7684\u7406\u5ff5\uff0c<strong>\u4e3a\u4e86\u652f\u6301\u9762\u5411\u63a5\u53e3\u7684\u7f16\u7a0b\uff0cMyBatis \u5f15\u5165\u4e86Mapper\u63a5\u53e3\u7684\u6982\u5ff5\uff0c\u9762\u5411\u63a5\u53e3\u7684\u5f15\u5165\uff0c\u5bf9\u4f7f\u7528\u6ce8\u89e3\u6765\u914d\u7f6eSQL\u8bed\u53e5\u6210\u4e3a\u53ef\u80fd\uff0c\u7528\u6237\u53ea\u9700\u8981\u5728\u63a5\u53e3\u4e0a\u6dfb\u52a0\u5fc5\u8981\u7684\u6ce8\u89e3\u5373\u53ef\uff0c\u4e0d\u7528\u518d\u53bb\u914d\u7f6eXML\u6587\u4ef6\u4e86<\/strong>\uff0c\u4f46\u662f\uff0c\u76ee\u524d\u7684MyBatis \u53ea\u662f\u5bf9\u6ce8\u89e3\u914d\u7f6eSQL\u8bed\u53e5\u63d0\u4f9b\u4e86\u6709\u9650\u7684\u652f\u6301\uff0c\u67d0\u4e9b\u9ad8\u7ea7\u529f\u80fd\u8fd8\u662f\u8981\u4f9d\u8d56XML\u914d\u7f6e\u6587\u4ef6\u914d\u7f6eSQL \u8bed\u53e5\u3002<\/p>\n<h3>5.4 \u5f15\u5bfc\u5c42<\/h3>\n<p><strong>\u5f15\u5bfc\u5c42\u662f\u914d\u7f6e\u548c\u542f\u52a8MyBatis\u914d\u7f6e\u4fe1\u606f\u7684\u65b9\u5f0f<\/strong>\u3002MyBatis \u63d0\u4f9b\u4e24\u79cd\u65b9\u5f0f\u6765\u5f15\u5bfcMyBatis \uff1a<strong>\u57fa\u4e8eXML\u914d\u7f6e\u6587\u4ef6\u7684\u65b9\u5f0f\u548c\u57fa\u4e8eJava API \u7684\u65b9\u5f0f<\/strong>\u3002<\/p>\n<h3>5.5 \u4e3b\u8981\u6784\u4ef6\u53ca\u5176\u76f8\u4e92\u5173\u7cfb<\/h3>\n<p>\u4eceMyBatis\u4ee3\u7801\u5b9e\u73b0\u7684\u89d2\u5ea6\u6765\u770b\uff0cMyBatis\u7684\u4e3b\u8981\u7684\u6838\u5fc3\u90e8\u4ef6\u6709\u4ee5\u4e0b\u51e0\u4e2a\uff1a<\/p>\n<blockquote>\n<p><strong>SqlSession\uff1a<\/strong>\u4f5c\u4e3aMyBatis\u5de5\u4f5c\u7684\u4e3b\u8981\u9876\u5c42API\uff0c\u8868\u793a\u548c\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u4f1a\u8bdd\uff0c\u5b8c\u6210\u5fc5\u8981\u6570\u636e\u5e93\u589e\u5220\u6539\u67e5\u529f\u80fd\uff1b<\/p>\n<p><strong>Executor\uff1a<\/strong>MyBatis\u6267\u884c\u5668\uff0c\u662fMyBatis \u8c03\u5ea6\u7684\u6838\u5fc3\uff0c\u8d1f\u8d23SQL\u8bed\u53e5\u7684\u751f\u6210\u548c\u67e5\u8be2\u7f13\u5b58\u7684\u7ef4\u62a4\uff1b<\/p>\n<p><strong>StatementHandler\uff1a<\/strong>\u5c01\u88c5\u4e86JDBC Statement\u64cd\u4f5c\uff0c\u8d1f\u8d23\u5bf9JDBC statement \u7684\u64cd\u4f5c\uff0c\u5982\u8bbe\u7f6e\u53c2\u6570\u3001\u5c06Statement\u7ed3\u679c\u96c6\u8f6c\u6362\u6210List\u96c6\u5408\u3002<\/p>\n<p><strong>ParameterHandler\uff1a<\/strong>\u8d1f\u8d23\u5bf9\u7528\u6237\u4f20\u9012\u7684\u53c2\u6570\u8f6c\u6362\u6210JDBC Statement \u6240\u9700\u8981\u7684\u53c2\u6570\uff1b<\/p>\n<p><strong>ResultSetHandler\uff1a<\/strong>\u8d1f\u8d23\u5c06JDBC\u8fd4\u56de\u7684ResultSet\u7ed3\u679c\u96c6\u5bf9\u8c61\u8f6c\u6362\u6210List\u7c7b\u578b\u7684\u96c6\u5408\uff1b<\/p>\n<p><strong>TypeHandler\uff1a<\/strong>\u8d1f\u8d23java\u6570\u636e\u7c7b\u578b\u548cjdbc\u6570\u636e\u7c7b\u578b\u4e4b\u95f4\u7684\u6620\u5c04\u548c\u8f6c\u6362\uff1b<\/p>\n<p><strong>MappedStatement\uff1a<\/strong>MappedStatement\u7ef4\u62a4\u4e86\u4e00\u6761&lt;select|update|delete|insert&gt;\u8282\u70b9\u7684\u5c01\u88c5\uff1b<\/p>\n<p><strong>SqlSource\uff1a<\/strong>\u8d1f\u8d23\u6839\u636e\u7528\u6237\u4f20\u9012\u7684parameterObject\uff0c\u52a8\u6001\u5730\u751f\u6210SQL\u8bed\u53e5\uff0c\u5c06\u4fe1\u606f\u5c01\u88c5\u5230BoundSql\u5bf9\u8c61\u4e2d\uff0c\u5e76\u8fd4\u56de\uff1b<\/p>\n<p><strong>BoundSql\uff1a<\/strong>\u8868\u793a\u52a8\u6001\u751f\u6210\u7684SQL\u8bed\u53e5\u4ee5\u53ca\u76f8\u5e94\u7684\u53c2\u6570\u4fe1\u606f\uff1b<\/p>\n<p><strong>Configuration\uff1a<\/strong>MyBatis\u6240\u6709\u7684\u914d\u7f6e\u4fe1\u606f\u90fd\u7ef4\u6301\u5728Configuration\u5bf9\u8c61\u4e4b\u4e2d\uff1b<\/p>\n<\/blockquote>\n<p><strong>\u5b83\u4eec\u7684\u5173\u7cfb\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"943\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9Avz\" alt=\"MyBatis\u4e3b\u8981\u6784\u4ef6\u5173\u7cfb\u5982\u56fe\" title=\"MyBatis\u4e3b\u8981\u6784\u4ef6\u5173\u7cfb\u5982\u56fe\" loading=\"lazy\"><\/p>\n<h2>6 SqlSession\u5de5\u4f5c\u8fc7\u7a0b\u5206\u6790<\/h2>\n<ol>\n<li><strong>\u5f00\u542f\u4e00\u4e2a\u6570\u636e\u5e93\u8bbf\u95ee\u4f1a\u8bdd&#8212;\u521b\u5efaSqlSession\u5bf9\u8c61<\/strong><\/li>\n<\/ol>\n<pre><code class=\"dart\">SqlSession sqlSession = factory.openSession(); <\/code><\/pre>\n<p><img decoding=\"async\" width=\"618\" height=\"336\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9AvA\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>MyBatis\u5c01\u88c5\u4e86\u5bf9\u6570\u636e\u5e93\u7684\u8bbf\u95ee\uff0c\u628a\u5bf9\u6570\u636e\u5e93\u7684\u4f1a\u8bdd\u548c\u4e8b\u52a1\u63a7\u5236\u653e\u5230\u4e86SqlSession\u5bf9\u8c61\u4e2d<\/p>\n<ol>\n<li><strong>\u4e3aSqlSession\u4f20\u9012\u4e00\u4e2a\u914d\u7f6e\u7684Sql\u8bed\u53e5\u7684Statement Id\u548c\u53c2\u6570\uff0c\u7136\u540e\u8fd4\u56de\u7ed3\u679c\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"csharp\">List&lt;Employee&gt; result = sqlSession.selectList(&quot;com.louis.mybatis.dao.EmployeesMapper.selectByMinSalary&quot;,params);<\/code><\/pre>\n<blockquote><p>\u4e0a\u8ff0\u7684&#8221;com.louis.mybatis.dao.EmployeesMapper.selectByMinSalary&#8221;\uff0c\u662f\u914d\u7f6e\u5728EmployeesMapper.xml \u7684Statement ID\uff0cparams\u662f\u4f20\u9012\u7684\u67e5\u8be2\u53c2\u6570\u3002<\/p><\/blockquote>\n<p>\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e0bsqlSession.selectList()\u65b9\u6cd5\u7684\u5b9a\u4e49\uff1a<\/p>\n<pre><code class=\"dart\">public &lt;E&gt; List&lt;E&gt; selectList(String statement, Object parameter) {  \n    return this.selectList(statement, parameter, RowBounds.DEFAULT);  \n}  \n \npublic &lt;E&gt; List&lt;E&gt; selectList(String statement, Object parameter, RowBounds rowBounds) {  \n    try {  \n        \/\/1.\u6839\u636eStatement Id\uff0c\u5728mybatis \u914d\u7f6e\u5bf9\u8c61Configuration\u4e2d\u67e5\u627e\u548c\u914d\u7f6e\u6587\u4ef6\u76f8\u5bf9\u5e94\u7684MappedStatement      \n        MappedStatement ms = configuration.getMappedStatement(statement);  \n        \/\/2. \u5c06\u67e5\u8be2\u4efb\u52a1\u59d4\u6258\u7ed9MyBatis \u7684\u6267\u884c\u5668 Executor  \n        List&lt;E&gt; result = executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);  \n        return result;  \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<blockquote><p>MyBatis\u5728\u521d\u59cb\u5316\u7684\u65f6\u5019\uff0c\u4f1a\u5c06MyBatis\u7684\u914d\u7f6e\u4fe1\u606f\u5168\u90e8\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff0c<strong>\u4f7f\u7528org.apache.ibatis.session.Configuration\u5b9e\u4f8b\u6765\u7ef4\u62a4<\/strong>\u3002\u4f7f\u7528\u8005\u53ef\u4ee5\u4f7f\u7528sqlSession.getConfiguration()\u65b9\u6cd5\u6765\u83b7\u53d6\u3002<strong>MyBatis\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u4fe1\u606f\u7684\u7ec4\u7ec7\u683c\u5f0f\u548c\u5185\u5b58\u4e2d\u5bf9\u8c61\u7684\u7ec4\u7ec7\u683c\u5f0f\u51e0\u4e4e\u5b8c\u5168\u5bf9\u5e94\u7684<\/strong>\u3002<\/p><\/blockquote>\n<p>\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u7684\uff1a<\/p>\n<pre><code class=\"csharp\">&lt;select id=&quot;selectByMinSalary&quot; resultMap=&quot;BaseResultMap&quot; parameterType=&quot;java.util.Map&quot; &gt;  \n   select   \n       EMPLOYEE_ID, FIRST_NAME, LAST_NAME, EMAIL, SALARY  \n   from LOUIS.EMPLOYEES  \n   &lt;if test=&quot;min_salary != null&quot;&gt;  \n       where SALARY &lt; #{min_salary,jdbcType=DECIMAL}  \n   &lt;\/if&gt;  \n&lt;\/select&gt;<\/code><\/pre>\n<blockquote><p><strong>\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u4f1a\u751f\u6210\u4e00\u4e2a\u5bf9\u5e94\u7684MappedStatement\u5bf9\u8c61\uff0c\u7136\u540e\u4f1a\u4ee5key=&#8221;com.louis.mybatis.dao.EmployeesMapper.selectByMinSalary&#8221; \uff0cvalue\u4e3aMappedStatement\u5bf9\u8c61\u7684\u5f62\u5f0f\u7ef4\u62a4\u5230Configuration\u7684\u4e00\u4e2aMap\u4e2d<\/strong>\u3002\u5f53\u4ee5\u540e\u9700\u8981\u4f7f\u7528\u7684\u65f6\u5019\uff0c\u53ea\u9700\u8981\u901a\u8fc7Id\u503c\u6765\u83b7\u53d6\u5c31\u53ef\u4ee5\u4e86\u3002<\/p><\/blockquote>\n<p>\u4ece\u4e0a\u8ff0\u7684\u4ee3\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230SqlSession\u7684\u804c\u80fd\u662f\uff1a<strong>SqlSession\u6839\u636eStatement ID, \u5728mybatis\u914d\u7f6e\u5bf9\u8c61Configuration\u4e2d\u83b7\u53d6\u5230\u5bf9\u5e94\u7684MappedStatement\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528mybatis\u6267\u884c\u5668\u6765\u6267\u884c\u5177\u4f53\u7684\u64cd\u4f5c<\/strong>\u3002<\/p>\n<ol>\n<li><strong>MyBatis\u6267\u884c\u5668Executor\u6839\u636eSqlSession\u4f20\u9012\u7684\u53c2\u6570\u6267\u884cquery()\u65b9\u6cd5\uff08\u7531\u4e8e\u4ee3\u7801\u8fc7\u957f\uff0c\u8bfb\u8005\u53ea\u9700\u9605\u8bfb\u6211\u6ce8\u91ca\u7684\u5730\u65b9\u5373\u53ef\uff09\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"php\">\/** \n   * BaseExecutor \u7c7b\u90e8\u5206\u4ee3\u7801 \n   * \n   *\/  \npublic &lt;E&gt; List&lt;E&gt; query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {\n      \/\/ 1. \u6839\u636e\u5177\u4f53\u4f20\u5165\u7684\u53c2\u6570\uff0c\u52a8\u6001\u5730\u751f\u6210\u9700\u8981\u6267\u884c\u7684SQL\u8bed\u53e5\uff0c\u7528BoundSql\u5bf9\u8c61\u8868\u793a    \n      BoundSql boundSql = ms.getBoundSql(parameter);  \n      \/\/ 2. \u4e3a\u5f53\u524d\u7684\u67e5\u8be2\u521b\u5efa\u4e00\u4e2a\u7f13\u5b58Key  \n      CacheKey key = createCacheKey(ms, parameter, rowBounds, boundSql);  \n      return query(ms, parameter, rowBounds, resultHandler, key, boundSql);  \n}  \n \n@SuppressWarnings(&quot;unchecked&quot;)  \npublic &lt;E&gt; List&lt;E&gt; query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {  \n       ErrorContext.instance().resource(ms.getResource()).activity(&quot;executing a query&quot;).object(ms.getId());  \n       if (closed) throw new ExecutorException(&quot;Executor was closed.&quot;);  \n       if (queryStack == 0 &amp;&amp; ms.isFlushCacheRequired()) {  \n           clearLocalCache();  \n       }  \n       List&lt;E&gt; list;  \n       try {  \n           queryStack++;  \n           list = resultHandler == null ? (List&lt;E&gt;) localCache.getObject(key) : null;  \n           if (list != null) {  \n               handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);  \n           } else {  \n               \/\/ 3.\u7f13\u5b58\u4e2d\u6ca1\u6709\u503c\uff0c\u76f4\u63a5\u4ece\u6570\u636e\u5e93\u4e2d\u8bfb\u53d6\u6570\u636e    \n               list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);  \n           }  \n       } finally {  \n           queryStack--;  \n       }  \n       if (queryStack == 0) {  \n           for (DeferredLoad deferredLoad : deferredLoads) {  \n               deferredLoad.load();  \n           }  \n           deferredLoads.clear(); \/\/ issue #601  \n           if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {  \n               clearLocalCache(); \/\/ issue #482  \n           }  \n       }  \n       return list;  \n}\n\nprivate &lt;E&gt; List&lt;E&gt; queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {  \n      List&lt;E&gt; list;  \n      localCache.putObject(key, EXECUTION_PLACEHOLDER);  \n      try {  \n         \n          \/\/4. \u6267\u884c\u67e5\u8be2\uff0c\u8fd4\u56deList \u7ed3\u679c\uff0c\u7136\u540e    \u5c06\u67e5\u8be2\u7684\u7ed3\u679c\u653e\u5165\u7f13\u5b58\u4e4b\u4e2d  \n          list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);  \n      } finally {  \n          localCache.removeObject(key);  \n      }  \n      localCache.putObject(key, list);  \n      if (ms.getStatementType() == StatementType.CALLABLE) {  \n          localOutputParameterCache.putObject(key, parameter);  \n      }  \n      return list;  \n} <\/code><\/pre>\n<pre><code class=\"dart\">\/** \n   * \n   * SimpleExecutor\u7c7b\u7684doQuery()\u65b9\u6cd5\u5b9e\u73b0 \n   * \n   *\/  \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          \/\/5. \u6839\u636e\u65e2\u6709\u7684\u53c2\u6570\uff0c\u521b\u5efaStatementHandler\u5bf9\u8c61\u6765\u6267\u884c\u67e5\u8be2\u64cd\u4f5c  \n          StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);  \n          \/\/6. \u521b\u5efajava.Sql.Statement\u5bf9\u8c61\uff0c\u4f20\u9012\u7ed9StatementHandler\u5bf9\u8c61  \n          stmt = prepareStatement(handler, ms.getStatementLog());  \n          \/\/7. \u8c03\u7528StatementHandler.query()\u65b9\u6cd5\uff0c\u8fd4\u56deList\u7ed3\u679c\u96c6  \n          return handler.&lt;E&gt;query(stmt, resultHandler);  \n       } finally {  \n           closeStatement(stmt);  \n       }  \n}<\/code><\/pre>\n<p>\u4e0a\u8ff0\u7684Executor.query()\u65b9\u6cd5\u51e0\u7ecf\u8f6c\u6298\uff0c<strong>\u6700\u540e\u4f1a\u521b\u5efa\u4e00\u4e2aStatementHandler\u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u5fc5\u8981\u7684\u53c2\u6570\u4f20\u9012\u7ed9StatementHandler<\/strong>\uff0c\u4f7f\u7528StatementHandler\u6765\u5b8c\u6210\u5bf9\u6570\u636e\u5e93\u7684\u67e5\u8be2\uff0c\u6700\u7ec8\u8fd4\u56deList\u7ed3\u679c\u96c6\u3002<\/p>\n<p><strong>\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0cExecutor\u7684\u529f\u80fd\u548c\u4f5c\u7528\u662f\uff1a<\/strong><\/p>\n<blockquote>\n<ol>\n<li>\u6839\u636e\u4f20\u9012\u7684\u53c2\u6570\uff0c\u5b8c\u6210SQL\u8bed\u53e5\u7684\u52a8\u6001\u89e3\u6790\uff0c\u751f\u6210BoundSql\u5bf9\u8c61\uff0c\u4f9bStatementHandler\u4f7f\u7528\uff1b<\/li>\n<li>\u4e3a\u67e5\u8be2\u521b\u5efa\u7f13\u5b58\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\uff1b<\/li>\n<li>\u521b\u5efaJDBC\u7684Statement\u8fde\u63a5\u5bf9\u8c61\uff0c\u4f20\u9012\u7ed9StatementHandler\u5bf9\u8c61\uff0c\u8fd4\u56deList\u67e5\u8be2\u7ed3\u679c\uff1b<\/li>\n<\/ol>\n<\/blockquote>\n<ol>\n<li><strong>StatementHandler\u5bf9\u8c61\u8d1f\u8d23\u8bbe\u7f6eStatement\u5bf9\u8c61\u4e2d\u7684\u67e5\u8be2\u53c2\u6570\u3001\u5904\u7406JDBC\u8fd4\u56de\u7684resultSet\uff0c\u5c06resultSet\u52a0\u5de5\u4e3aList \u96c6\u5408\u8fd4\u56de\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u63a5\u7740\u4e0a\u9762\u7684Executor\u7b2c\u516d\u6b65\uff0c\u770b\u4e00\u4e0b\uff1aprepareStatement() \u65b9\u6cd5\u7684\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"java\">\/** \n   * \n   * SimpleExecutor\u7c7b\u7684doQuery()\u65b9\u6cd5\u5b9e\u73b0 \n   * \n   *\/  \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          StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql); \n          \/\/ 1.\u51c6\u5907Statement\u5bf9\u8c61\uff0c\u5e76\u8bbe\u7f6eStatement\u5bf9\u8c61\u7684\u53c2\u6570 \n          stmt = prepareStatement(handler, ms.getStatementLog()); \n          \/\/ 2. StatementHandler\u6267\u884cquery()\u65b9\u6cd5\uff0c\u8fd4\u56deList\u7ed3\u679c \n          return handler.&lt;E&gt;query(stmt, resultHandler); \n      } finally {\n          closeStatement(stmt); \n      } \n}  \n \nprivate Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {\n      Statement stmt;  \n      Connection connection = getConnection(statementLog);  \n      stmt = handler.prepare(connection);  \n      \/\/\u5bf9\u521b\u5efa\u7684Statement\u5bf9\u8c61\u8bbe\u7f6e\u53c2\u6570\uff0c\u5373\u8bbe\u7f6eSQL \u8bed\u53e5\u4e2d ? \u8bbe\u7f6e\u4e3a\u6307\u5b9a\u7684\u53c2\u6570  \n      handler.parameterize(stmt);  \n      return stmt;  \n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3StatementHandler\u5bf9\u8c61\u4e3b\u8981\u5b8c\u6210\u4e24\u4e2a\u5de5\u4f5c\uff1a<\/p>\n<blockquote>\n<ol>\n<li>\u5bf9\u4e8eJDBC\u7684PreparedStatement\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u521b\u5efa\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u7684\u662fSQL\u8bed\u53e5\u5b57\u7b26\u4e32\u4f1a\u5305\u542b \u82e5\u5e72\u4e2a? \u5360\u4f4d\u7b26\uff0c\u6211\u4eec\u5176\u540e\u518d\u5bf9\u5360\u4f4d\u7b26\u8fdb\u884c\u8bbe\u503c\u3002<br \/><strong>StatementHandler\u901a\u8fc7parameterize(statement)\u65b9\u6cd5\u5bf9Statement\u8fdb\u884c\u8bbe\u503c\uff1b<\/strong><\/li>\n<li>StatementHandler\u901a\u8fc7List&lt;E&gt; query(Statement statement, ResultHandler resultHandler)\u65b9\u6cd5\u6765\u5b8c\u6210\u6267\u884cStatement\uff0c\u548c\u5c06Statement\u5bf9\u8c61\u8fd4\u56de\u7684resultSet\u5c01\u88c5\u6210List\uff1b<\/li>\n<\/ol>\n<\/blockquote>\n<ol>\n<li><strong>StatementHandler \u7684parameterize(statement) \u65b9\u6cd5\u7684\u5b9e\u73b0\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"cpp\">\/** \n   * StatementHandler \u7c7b\u7684parameterize(statement) \u65b9\u6cd5\u5b9e\u73b0  \n   *\/  \npublic void parameterize(Statement statement) throws SQLException {  \n      \/\/\u4f7f\u7528ParameterHandler\u5bf9\u8c61\u6765\u5b8c\u6210\u5bf9Statement\u7684\u8bbe\u503c    \n      parameterHandler.setParameters((PreparedStatement) statement);  \n}  <\/code><\/pre>\n<pre><code class=\"csharp\">\/** \n   *  \n   * ParameterHandler\u7c7b\u7684setParameters(PreparedStatement ps) \u5b9e\u73b0 \n   * \u5bf9\u67d0\u4e00\u4e2aStatement\u8fdb\u884c\u8bbe\u7f6e\u53c2\u6570 \n   *\/  \npublic void setParameters(PreparedStatement ps) throws SQLException {  \n      ErrorContext.instance().activity(&quot;setting parameters&quot;).object(mappedStatement.getParameterMap().getId());  \n      List&lt;ParameterMapping&gt; 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)) { \/\/ issue #448 ask first for additional params  \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         \n                  \/\/ \u6bcf\u4e00\u4e2aMapping\u90fd\u6709\u4e00\u4e2aTypeHandler\uff0c\u6839\u636eTypeHandler\u6765\u5bf9preparedStatement\u8fdb\u884c\u8bbe\u7f6e\u53c2\u6570  \n                  TypeHandler typeHandler = parameterMapping.getTypeHandler();  \n                  JdbcType jdbcType = parameterMapping.getJdbcType();  \n                  if (value == null &amp;&amp; jdbcType == null) jdbcType = configuration.getJdbcTypeForNull();  \n                  \/\/ \u8bbe\u7f6e\u53c2\u6570  \n                  typeHandler.setParameter(ps, i + 1, value, jdbcType);  \n              }  \n          }  \n      }  \n}<\/code><\/pre>\n<blockquote><p>\u4ece\u4e0a\u8ff0\u7684\u4ee3\u7801\u53ef\u4ee5\u770b\u5230,StatementHandler\u7684parameterize(Statement) \u65b9\u6cd5\u8c03\u7528\u4e86 ParameterHandler\u7684setParameters(statement) \u65b9\u6cd5\uff0c<br \/><strong>ParameterHandler\u7684setParameters(Statement)\u65b9\u6cd5\u8d1f\u8d23 \u6839\u636e\u6211\u4eec\u8f93\u5165\u7684\u53c2\u6570\uff0c\u5bf9statement\u5bf9\u8c61\u7684 ? \u5360\u4f4d\u7b26\u5904\u8fdb\u884c\u8d4b\u503c\u3002<\/strong><\/p><\/blockquote>\n<ol>\n<li><strong>StatementHandler \u7684List&lt;E&gt; query(Statement statement, ResultHandler resultHandler)\u65b9\u6cd5\u7684\u5b9e\u73b0\uff1a<\/strong><\/li>\n<\/ol>\n<pre><code class=\"csharp\"> \/** \n    * PreParedStatement\u7c7b\u7684query\u65b9\u6cd5\u5b9e\u73b0 \n    *\/  \npublic &lt;E&gt; List&lt;E&gt; query(Statement statement, ResultHandler resultHandler) throws SQLException {  \n      \/\/1.\u8c03\u7528preparedStatemnt\u3002execute()\u65b9\u6cd5\uff0c\u7136\u540e\u5c06resultSet\u4ea4\u7ed9ResultSetHandler\u5904\u7406    \n      PreparedStatement ps = (PreparedStatement) statement;  \n      ps.execute();  \n      \/\/2. \u4f7f\u7528ResultHandler\u6765\u5904\u7406ResultSet  \n      return resultSetHandler.&lt;E&gt; handleResultSets(ps);  \n}  <\/code><\/pre>\n<blockquote><p>\u4ece\u4e0a\u8ff0\u4ee3\u7801\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0cStatementHandler \u7684List&lt;E&gt; query(Statement statement, ResultHandler resultHandler)\u65b9\u6cd5\u7684\u5b9e\u73b0\uff0c\u662f\u8c03\u7528\u4e86ResultSetHandler\u7684handleResultSets(Statement) \u65b9\u6cd5\u3002<strong>ResultSetHandler\u7684handleResultSets(Statement) \u65b9\u6cd5\u4f1a\u5c06Statement\u8bed\u53e5\u6267\u884c\u540e\u751f\u6210\u7684resultSet \u7ed3\u679c\u96c6\u8f6c\u6362\u6210List&lt;E&gt; \u7ed3\u679c\u96c6<\/strong>\uff1a<\/p><\/blockquote>\n<pre><code class=\"dart\">\/**   \n   * ResultSetHandler\u7c7b\u7684handleResultSets()\u65b9\u6cd5\u5b9e\u73b0 \n   *  \n   *\/  \npublic List&lt;Object&gt; handleResultSets(Statement stmt) throws SQLException {  \n      final List&lt;Object&gt; multipleResults = new ArrayList&lt;Object&gt;();  \n \n      int resultSetCount = 0;  \n      ResultSetWrapper rsw = getFirstResultSet(stmt);  \n \n      List&lt;ResultMap&gt; resultMaps = mappedStatement.getResultMaps();  \n      int resultMapCount = resultMaps.size();  \n      validateResultMapsCount(rsw, resultMapCount);  \n     \n      while (rsw != null &amp;&amp; resultMapCount &gt; resultSetCount) {  \n          ResultMap resultMap = resultMaps.get(resultSetCount);  \n       \n          \/\/\u5c06resultSet  \n          handleResultSet(rsw, resultMap, multipleResults, null);  \n          rsw = getNextResultSet(stmt);  \n          cleanUpAfterHandlingResultSet();  \n          resultSetCount++;  \n      }\n \n      String[] resultSets = mappedStatement.getResulSets();  \n      if (resultSets != null) {  \n          while (rsw != null &amp;&amp; resultSetCount &lt; resultSets.length) {  \n              ResultMapping parentMapping = nextResultMaps.get(resultSets[resultSetCount]);  \n              if (parentMapping != null) {  \n                  String nestedResultMapId = parentMapping.getNestedResultMapId();  \n                  ResultMap resultMap = configuration.getResultMap(nestedResultMapId);  \n                  handleResultSet(rsw, resultMap, null, parentMapping);  \n              }  \n              rsw = getNextResultSet(stmt);  \n              cleanUpAfterHandlingResultSet();  \n              resultSetCount++;  \n          }  \n      }  \n \n      return collapseSingleResultList(multipleResults);  \n}  <\/code><\/pre>\n<h2>7 MyBatis\u521d\u59cb\u5316\u673a\u5236<\/h2>\n<h3>7.1 MyBatis\u7684\u521d\u59cb\u5316\u505a\u4e86\u4ec0\u4e48<\/h3>\n<p><strong>\u4efb\u4f55\u6846\u67b6\u7684\u521d\u59cb\u5316\uff0c\u65e0\u975e\u662f\u52a0\u8f7d\u81ea\u5df1\u8fd0\u884c\u65f6\u6240\u9700\u8981\u7684\u914d\u7f6e\u4fe1\u606f\u3002<\/strong>MyBatis\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u5927\u6982\u5305\u542b\u4ee5\u4e0b\u4fe1\u606f\uff0c\u5176\u9ad8\u5c42\u7ea7\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" width=\"482\" height=\"616\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9AvB\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>MyBatis\u914d\u7f6e\u4fe1\u606f\u7ed3\u6784\u56fe<\/p>\n<p><strong>MyBatis\u7684\u4e0a\u8ff0\u914d\u7f6e\u4fe1\u606f\u4f1a\u914d\u7f6e\u5728XML\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u90a3\u4e48\uff0c\u8fd9\u4e9b\u4fe1\u606f\u88ab\u52a0\u8f7d\u8fdb\u5165MyBatis\u5185\u90e8\uff0cMyBatis\u662f\u600e\u6837\u7ef4\u62a4\u7684\u5462\uff1f<\/strong><\/p>\n<p>MyBatis\u91c7\u7528\u4e86\u4e00\u4e2a\u975e\u5e38\u76f4\u767d\u548c\u7b80\u5355\u7684\u65b9\u5f0f&#8212;<strong>\u4f7f\u7528 org.apache.ibatis.session.Configuration\u5bf9\u8c61\u4f5c\u4e3a\u4e00\u4e2a\u6240\u6709\u914d\u7f6e\u4fe1\u606f\u7684\u5bb9\u5668\uff0cConfiguration\u5bf9\u8c61\u7684\u7ec4\u7ec7\u7ed3\u6784\u548cXML\u914d\u7f6e\u6587\u4ef6\u7684\u7ec4\u7ec7\u7ed3\u6784\u51e0\u4e4e\u5b8c\u5168\u4e00\u6837<\/strong>\uff08\u5f53\u7136\uff0cConfiguration\u5bf9\u8c61\u7684\u529f\u80fd\u5e76\u4e0d\u9650\u4e8e\u6b64\uff0c\u5b83\u8fd8\u8d1f\u8d23\u521b\u5efa\u4e00\u4e9bMyBatis\u5185\u90e8\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u5982Executor\u7b49\uff0c\u8fd9\u5c06\u5728\u540e\u7eed\u7684\u6587\u7ae0\u4e2d\u8ba8\u8bba\uff09\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"742\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9AvC\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Configuration\u5bf9\u8c61\u7684\u7ec4\u7ec7\u7ed3\u6784\u548cXML\u914d\u7f6e\u6587\u4ef6\u7684\u7ec4\u7ec7\u7ed3\u6784\u51e0\u4e4e\u5b8c\u5168\u4e00\u6837<\/p>\n<p>MyBatis\u6839\u636e\u521d\u59cb\u5316\u597dConfiguration\u4fe1\u606f\uff0c\u8fd9\u65f6\u5019\u7528\u6237\u5c31\u53ef\u4ee5\u4f7f\u7528MyBatis\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u4e86\u3002<strong>\u53ef\u4ee5\u8fd9\u4e48\u8bf4\uff0cMyBatis\u521d\u59cb\u5316\u7684\u8fc7\u7a0b\uff0c\u5c31\u662f\u521b\u5efa Configuration\u5bf9\u8c61\u7684\u8fc7\u7a0b<\/strong>\u3002<\/p>\n<p><strong>MyBatis\u7684\u521d\u59cb\u5316\u53ef\u4ee5\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a<\/strong><\/p>\n<blockquote>\n<p><strong>\u57fa\u4e8eXML\u914d\u7f6e\u6587\u4ef6\uff1a<\/strong>\u57fa\u4e8eXML\u914d\u7f6e\u6587\u4ef6\u7684\u65b9\u5f0f\u662f\u5c06MyBatis\u7684\u6240\u6709\u914d\u7f6e\u4fe1\u606f\u653e\u5728XML\u6587\u4ef6\u4e2d\uff0cMyBatis\u901a\u8fc7\u52a0\u8f7d\u5e76XML\u914d\u7f6e\u6587\u4ef6\uff0c\u5c06\u914d\u7f6e\u6587\u4fe1\u606f\u7ec4\u88c5\u6210\u5185\u90e8\u7684Configuration\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u57fa\u4e8eJava API\uff1a<\/strong>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4f7f\u7528XML\u914d\u7f6e\u6587\u4ef6\uff0c\u9700\u8981MyBatis\u4f7f\u7528\u8005\u5728Java\u4ee3\u7801\u4e2d\uff0c\u624b\u52a8\u521b\u5efaConfiguration\u5bf9\u8c61\uff0c\u7136\u540e\u5c06\u914d\u7f6e\u53c2\u6570set \u8fdb\u5165Configuration\u5bf9\u8c61\u4e2d\u3002<\/p>\n<\/blockquote>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u901a\u8fc7 \u57fa\u4e8eXML\u914d\u7f6e\u6587\u4ef6\u65b9\u5f0f\u7684MyBatis\u521d\u59cb\u5316\uff0c\u6df1\u5165\u63a2\u8ba8MyBatis\u662f\u5982\u4f55\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6784\u5efaConfiguration\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528\u5b83\u3002<\/p>\n<h3>7.2 \u57fa\u4e8eXML\u914d\u7f6e\u6587\u4ef6\u521b\u5efaConfiguration\u5bf9\u8c61<\/h3>\n<p>\u73b0\u5728\u5c31\u4ece\u4f7f\u7528MyBatis\u7684\u7b80\u5355\u4f8b\u5b50\u5165\u624b\uff0c\u6df1\u5165\u5206\u6790\u4e00\u4e0bMyBatis\u662f\u600e\u6837\u5b8c\u6210\u521d\u59cb\u5316\u7684\uff0c\u90fd\u521d\u59cb\u5316\u4e86\u4ec0\u4e48\u3002\u770b\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"dart\">String resource = &quot;mybatis-config.xml&quot;;  \nInputStream inputStream = Resources.getResourceAsStream(resource);  \nSqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);  \nSqlSession sqlSession = sqlSessionFactory.openSession();  \nList list = sqlSession.selectList(&quot;com.foo.bean.BlogMapper.queryAllBlogInfo&quot;);<\/code><\/pre>\n<p>\u6709\u8fc7MyBatis\u4f7f\u7528\u7ecf\u9a8c\u7684\u8bfb\u8005\u4f1a\u77e5\u9053\uff0c\u4e0a\u8ff0\u8bed\u53e5\u7684\u4f5c\u7528\u662f\u6267\u884ccom.foo.bean.BlogMapper.queryAllBlogInfo \u5b9a\u4e49\u7684SQL\u8bed\u53e5\uff0c\u8fd4\u56de\u4e00\u4e2aList\u7ed3\u679c\u96c6\u3002\u603b\u7684\u6765\u8bf4\uff0c\u4e0a\u8ff0\u4ee3\u7801\u7ecf\u5386\u4e86<strong>mybatis\u521d\u59cb\u5316 &#8211;&gt;\u521b\u5efaSqlSession &#8211;&gt;\u6267\u884cSQL\u8bed\u53e5<\/strong>\u8fd4\u56de\u7ed3\u679c\u4e09\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u7684\u529f\u80fd\u662f\u6839\u636e\u914d\u7f6e\u6587\u4ef6mybatis-config.xml  \u914d\u7f6e\u6587\u4ef6\uff0c\u521b\u5efaSqlSessionFactory\u5bf9\u8c61\uff0c\u7136\u540e\u4ea7\u751fSqlSession\uff0c\u6267\u884cSQL\u8bed\u53e5\u3002<strong>\u800cmybatis\u7684\u521d\u59cb\u5316\u5c31\u53d1\u751f\u5728\u7b2c\u4e09\u53e5\uff1aSqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);<\/strong> \u73b0\u5728\u5c31\u8ba9\u6211\u4eec\u770b\u770b\u7b2c\u4e09\u53e5\u5230\u5e95\u53d1\u751f\u4e86\u4ec0\u4e48\u3002<\/p>\n<ol>\n<li><strong>MyBatis\u521d\u59cb\u5316\u57fa\u672c\u8fc7\u7a0b\uff1a<\/strong><\/li>\n<\/ol>\n<p>SqlSessionFactoryBuilder\u6839\u636e\u4f20\u5165\u7684\u6570\u636e\u6d41\u751f\u6210Configuration\u5bf9\u8c61\uff0c\u7136\u540e\u6839\u636eConfiguration\u5bf9\u8c61\u521b\u5efa\u9ed8\u8ba4\u7684SqlSessionFactory\u5b9e\u4f8b\u3002<\/p>\n<p><strong>\u521d\u59cb\u5316\u7684\u57fa\u672c\u8fc7\u7a0b\u5982\u4e0b\u5e8f\u5217\u56fe\u6240\u793a\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"695\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9AvD\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>MyBatis\u521d\u59cb\u5316\u5e8f\u5217\u56fe<\/p>\n<p><strong>\u7531\u4e0a\u56fe\u6240\u793a\uff0cmybatis\u521d\u59cb\u5316\u8981\u7ecf\u8fc7\u7b80\u5355\u7684\u4ee5\u4e0b\u51e0\u6b65\uff1a<\/strong><\/p>\n<blockquote>\n<ol>\n<li>\u8c03\u7528SqlSessionFactoryBuilder\u5bf9\u8c61\u7684build(inputStream)\u65b9\u6cd5\uff1b<\/li>\n<li>SqlSessionFactoryBuilder\u4f1a\u6839\u636e\u8f93\u5165\u6d41inputStream\u7b49\u4fe1\u606f\u521b\u5efaXMLConfigBuilder\u5bf9\u8c61;<\/li>\n<li>SqlSessionFactoryBuilder\u8c03\u7528XMLConfigBuilder\u5bf9\u8c61\u7684parse()\u65b9\u6cd5\uff1b<\/li>\n<li>XMLConfigBuilder\u5bf9\u8c61\u8fd4\u56deConfiguration\u5bf9\u8c61\uff1b<\/li>\n<li>SqlSessionFactoryBuilder\u6839\u636eConfiguration\u5bf9\u8c61\u521b\u5efa\u4e00\u4e2aDefaultSessionFactory\u5bf9\u8c61\uff1b<\/li>\n<li>SqlSessionFactoryBuilder\u8fd4\u56de DefaultSessionFactory\u5bf9\u8c61\u7ed9Client\uff0c\u4f9bClient\u4f7f\u7528\u3002<\/li>\n<\/ol>\n<\/blockquote>\n<p><strong>SqlSessionFactoryBuilder\u76f8\u5173\u7684\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/strong><\/p>\n<pre><code class=\"csharp\">public SqlSessionFactory build(InputStream inputStream)  {  \n      return build(inputStream, null, null);  \n}  \n\npublic SqlSessionFactory build(InputStream inputStream, String environment, Properties properties)  {  \n      try  {  \n          \/\/2. \u521b\u5efaXMLConfigBuilder\u5bf9\u8c61\u7528\u6765\u89e3\u6790XML\u914d\u7f6e\u6587\u4ef6\uff0c\u751f\u6210Configuration\u5bf9\u8c61  \n          XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);  \n          \/\/3. \u5c06XML\u914d\u7f6e\u6587\u4ef6\u5185\u7684\u4fe1\u606f\u89e3\u6790\u6210Java\u5bf9\u8c61Configuration\u5bf9\u8c61  \n          Configuration config = parser.parse();  \n          \/\/4. \u6839\u636eConfiguration\u5bf9\u8c61\u521b\u5efa\u51faSqlSessionFactory\u5bf9\u8c61  \n          return build(config);  \n      } catch (Exception e) {  \n          throw ExceptionFactory.wrapException(&quot;Error building SqlSession.&quot;, 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}\n\n\/\/ \u4ece\u6b64\u5904\u53ef\u4ee5\u770b\u51fa\uff0cMyBatis\u5185\u90e8\u901a\u8fc7Configuration\u5bf9\u8c61\u6765\u521b\u5efaSqlSessionFactory,\u7528\u6237\u4e5f\u53ef\u4ee5\u81ea\u5df1\u901a\u8fc7API\u6784\u9020\u597dConfiguration\u5bf9\u8c61\uff0c\u8c03\u7528\u6b64\u65b9\u6cd5\u521bSqlSessionFactory  \npublic SqlSessionFactory build(Configuration config) {  \n      return new DefaultSqlSessionFactory(config);  \n}  <\/code><\/pre>\n<p>\u4e0a\u8ff0\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\u4e2d\uff0c\u6d89\u53ca\u5230\u4e86\u4ee5\u4e0b\u51e0\u4e2a\u5bf9\u8c61\uff1a<\/p>\n<blockquote>\n<p><strong>SqlSessionFactoryBuilder \uff1a<\/strong>SqlSessionFactory\u7684\u6784\u9020\u5668\uff0c\u7528\u4e8e\u521b\u5efaSqlSessionFactory\uff0c\u91c7\u7528\u4e86Builder\u8bbe\u8ba1\u6a21\u5f0f<\/p>\n<p><strong>Configuration \uff1a<\/strong>\u8be5\u5bf9\u8c61\u662fmybatis-config.xml\u6587\u4ef6\u4e2d\u6240\u6709mybatis\u914d\u7f6e\u4fe1\u606f<\/p>\n<p><strong>SqlSessionFactory\uff1a<\/strong>SqlSession\u5de5\u5382\u7c7b\uff0c\u4ee5\u5de5\u5382\u5f62\u5f0f\u521b\u5efaSqlSession\u5bf9\u8c61\uff0c\u91c7\u7528\u4e86Factory\u5de5\u5382\u8bbe\u8ba1\u6a21\u5f0f<\/p>\n<p><strong>XMLConfigBuilder \uff1a<\/strong>\u8d1f\u8d23\u5c06mybatis-config.xml\u914d\u7f6e\u6587\u4ef6\u89e3\u6790\u6210Configuration\u5bf9\u8c61\uff0c\u5171SqlSessonFactoryBuilder\u4f7f\u7528\uff0c\u521b\u5efaSqlSessionFactory<\/p>\n<\/blockquote>\n<ol>\n<li><strong>\u521b\u5efaConfiguration\u5bf9\u8c61\u7684\u8fc7\u7a0b\uff1a<\/strong><br \/>\u63a5\u7740\u4e0a\u8ff0\u7684 MyBatis\u521d\u59cb\u5316\u57fa\u672c\u8fc7\u7a0b\u8ba8\u8bba\uff0c<strong>\u5f53SqlSessionFactoryBuilder\u6267\u884cbuild()\u65b9\u6cd5\uff0c\u8c03\u7528\u4e86XMLConfigBuilder\u7684parse()\u65b9\u6cd5\uff0c\u7136\u540e\u8fd4\u56de\u4e86Configuration\u5bf9\u8c61<\/strong>\u3002\u90a3\u4e48parse()\u65b9\u6cd5\u662f\u5982\u4f55\u5904\u7406XML\u6587\u4ef6\uff0c\u751f\u6210Configuration\u5bf9\u8c61\u7684\u5462\uff1f<\/li>\n<\/ol>\n<ul>\n<li>\uff081\uff09XMLConfigBuilder\u4f1a<strong>\u5c06XML\u914d\u7f6e\u6587\u4ef6\u7684\u4fe1\u606f\u8f6c\u6362\u4e3aDocument\u5bf9\u8c61<\/strong>\uff0c\u800cXML\u914d\u7f6e\u5b9a\u4e49\u6587\u4ef6<strong>DTD\u8f6c\u6362\u6210XMLMapperEntityResolver\u5bf9\u8c61<\/strong>\uff0c\u7136\u540e<strong>\u5c06\u4e8c\u8005\u5c01\u88c5\u5230XpathParser\u5bf9\u8c61\u4e2d\uff0cXpathParser\u7684\u4f5c\u7528\u662f\u63d0\u4f9b\u6839\u636eXpath\u8868\u8fbe\u5f0f\u83b7\u53d6\u57fa\u672c\u7684DOM\u8282\u70b9Node\u4fe1\u606f\u7684\u64cd\u4f5c<\/strong>\u3002<\/li>\n<\/ul>\n<p>\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"695\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9AvD\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>XpathParser\u7ec4\u6210\u7ed3\u6784\u56fe\u548c\u751f\u6210\u56fe<\/p>\n<ul>\n<li>\uff082\uff09\u4e4b\u540eXMLConfigBuilder\u8c03\u7528parse()\u65b9\u6cd5\uff1a<strong>\u4f1a\u4eceXPathParser\u4e2d\u53d6\u51fa &lt;configuration&gt;\u8282\u70b9\u5bf9\u5e94\u7684Node\u5bf9\u8c61\uff0c\u7136\u540e\u89e3\u6790\u6b64Node\u8282\u70b9\u7684\u5b50Node<\/strong>\uff1aproperties, settings, typeAliases,typeHandlers, objectFactory, objectWrapperFactory, plugins, environments,databaseIdProvider, mappers\uff1a<\/li>\n<\/ul>\n<pre><code class=\"cpp\">public Configuration parse() {  \n     if (parsed) {  \n         throw new BuilderException(&quot;Each XMLConfigBuilder can only be used once.&quot;);  \n     }  \n     parsed = true;  \n     \/\/\u6e90\u7801\u4e2d\u6ca1\u6709\u8fd9\u4e00\u53e5\uff0c\u53ea\u6709parseConfiguration(parser.evalNode(&quot;\/configuration&quot;));  \n     \/\/\u4e3a\u4e86\u8ba9\u8bfb\u8005\u770b\u5f97\u66f4\u660e\u6670\uff0c\u6e90\u7801\u62c6\u5206\u4e3a\u4ee5\u4e0b\u4e24\u53e5  \n     XNode configurationNode = parser.evalNode(&quot;\/configuration&quot;);  \n     parseConfiguration(configurationNode);  \n     return configuration;\n}\n\/** \n  * \u89e3\u6790 &quot;\/configuration&quot;\u8282\u70b9\u4e0b\u7684\u5b50\u8282\u70b9\u4fe1\u606f\uff0c\u7136\u540e\u5c06\u89e3\u6790\u7684\u7ed3\u679c\u8bbe\u7f6e\u5230Configuration\u5bf9\u8c61\u4e2d \n  *\/  \nprivate void parseConfiguration(XNode root) {  \n     try {  \n         \/\/1.\u9996\u5148\u5904\u7406properties \u8282\u70b9     \n         propertiesElement(root.evalNode(&quot;properties&quot;)); \/\/issue #117 read properties first  \n         \/\/2.\u5904\u7406typeAliases  \n         typeAliasesElement(root.evalNode(&quot;typeAliases&quot;));  \n         \/\/3.\u5904\u7406\u63d2\u4ef6  \n         pluginElement(root.evalNode(&quot;plugins&quot;));  \n         \/\/4.\u5904\u7406objectFactory  \n         objectFactoryElement(root.evalNode(&quot;objectFactory&quot;));  \n         \/\/5.objectWrapperFactory  \n         objectWrapperFactoryElement(root.evalNode(&quot;objectWrapperFactory&quot;));  \n         \/\/6.settings  \n         settingsElement(root.evalNode(&quot;settings&quot;));  \n         \/\/7.\u5904\u7406environments  \n         environmentsElement(root.evalNode(&quot;environments&quot;)); \/\/ read it after objectFactory and objectWrapperFactory issue #631  \n         \/\/8.database  \n         databaseIdProviderElement(root.evalNode(&quot;databaseIdProvider&quot;));  \n         \/\/9.typeHandlers  \n         typeHandlerElement(root.evalNode(&quot;typeHandlers&quot;));  \n         \/\/10.mappers  \n         mapperElement(root.evalNode(&quot;mappers&quot;));  \n     } catch (Exception e) {  \n         throw new BuilderException(&quot;Error parsing SQL Mapper Configuration. Cause: &quot; + e, e);  \n     }  \n }  <\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u8fd8\u6709\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u5730\u65b9\uff0c<strong>\u5c31\u662f\u89e3\u6790XML\u914d\u7f6e\u6587\u4ef6\u5b50\u8282\u70b9&lt;mappers&gt;\u7684\u65b9\u6cd5mapperElements(root.evalNode(&#8220;mappers&#8221;)), \u5b83\u5c06\u89e3\u6790\u6211\u4eec\u914d\u7f6e\u7684Mapper.xml\u914d\u7f6e\u6587\u4ef6\uff0cMapper\u914d\u7f6e\u6587\u4ef6\u53ef\u4ee5\u8bf4\u662fMyBatis\u7684\u6838\u5fc3<\/strong>\uff0cMyBatis\u7684\u7279\u6027\u548c\u7406\u5ff5\u90fd\u4f53\u73b0\u5728\u6b64Mapper\u7684\u914d\u7f6e\u548c\u8bbe\u8ba1\u4e0a\u3002<\/p>\n<ul>\n<li>\n<p>\uff083\uff09<strong>\u7136\u540e\u5c06\u8fd9\u4e9b\u503c\u89e3\u6790\u51fa\u6765\u8bbe\u7f6e\u5230Configuration\u5bf9\u8c61\u4e2d\uff1a<\/strong><\/p>\n<p>\u89e3\u6790\u5b50\u8282\u70b9\u7684\u8fc7\u7a0b\u8fd9\u91cc\u5c31\u4e0d\u4e00\u4e00\u4ecb\u7ecd\u4e86\uff0c\u7528\u6237\u53ef\u4ee5\u53c2\u7167MyBatis\u6e90\u7801\u4ed4\u7ec6\u63e3\u6469\uff0c<strong>\u6211\u4eec\u5c31\u770b\u4e0a\u8ff0\u7684environmentsElement(root.evalNode(&#8220;environments&#8221;)); \u65b9\u6cd5\u662f\u5982\u4f55\u5c06environments\u7684\u4fe1\u606f\u89e3\u6790\u51fa\u6765\uff0c\u8bbe\u7f6e\u5230Configuration\u5bf9\u8c61\u4e2d\u7684\uff1a<\/strong><\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"dart\">\/** \n  * \u89e3\u6790environments\u8282\u70b9\uff0c\u5e76\u5c06\u7ed3\u679c\u8bbe\u7f6e\u5230Configuration\u5bf9\u8c61\u4e2d \n  * \u6ce8\u610f\uff1a\u521b\u5efaenvronment\u65f6\uff0c\u5982\u679cSqlSessionFactoryBuilder\u6307\u5b9a\u4e86\u7279\u5b9a\u7684\u73af\u5883\uff08\u5373\u6570\u636e\u6e90\uff09\uff1b \n  *      \u5219\u8fd4\u56de\u6307\u5b9a\u73af\u5883\uff08\u6570\u636e\u6e90\uff09\u7684Environment\u5bf9\u8c61\uff0c\u5426\u5219\u8fd4\u56de\u9ed8\u8ba4\u7684Environment\u5bf9\u8c61\uff1b \n  *      \u8fd9\u79cd\u65b9\u5f0f\u5b9e\u73b0\u4e86MyBatis\u53ef\u4ee5\u8fde\u63a5\u591a\u6570\u636e\u6e90 \n  *\/  \nprivate void environmentsElement(XNode context) throws Exception {  \n    if (context != null)  \n    {  \n         if (environment == null)  \n         {  \n             environment = context.getStringAttribute(&quot;default&quot;);  \n         }  \n         for (XNode child : context.getChildren())  \n         {  \n              String id = child.getStringAttribute(&quot;id&quot;);  \n              if (isSpecifiedEnvironment(id))  \n              {  \n                  \/\/1.\u521b\u5efa\u4e8b\u52a1\u5de5\u5382 TransactionFactory  \n                  TransactionFactory txFactory = transactionManagerElement(child.evalNode(&quot;transactionManager&quot;));  \n                  DataSourceFactory dsFactory = dataSourceElement(child.evalNode(&quot;dataSource&quot;));  \n                  \/\/2.\u521b\u5efa\u6570\u636e\u6e90DataSource  \n                  DataSource dataSource = dsFactory.getDataSource();  \n                  \/\/3. \u6784\u9020Environment\u5bf9\u8c61  \n                  Environment.Builder environmentBuilder = new Environment.Builder(id)  \n             .transactionFactory(txFactory)  \n             .dataSource(dataSource);  \n                  \/\/4. \u5c06\u521b\u5efa\u7684Envronment\u5bf9\u8c61\u8bbe\u7f6e\u5230configuration \u5bf9\u8c61\u4e2d  \n                  configuration.setEnvironment(environmentBuilder.build());  \n             }  \n         }  \n    }  \n}<\/code><\/pre>\n<pre><code class=\"csharp\">private boolean isSpecifiedEnvironment(String id)  \n{  \n      if (environment == null)  \n      {  \n           throw new BuilderException(&quot;No environment specified.&quot;);  \n      }  \n      else if (id == null)  \n      {  \n           throw new BuilderException(&quot;Environment requires an id attribute.&quot;);  \n      }  \n      else if (environment.equals(id))  \n      {  \n          return true;  \n      }  \n      return false;  \n }  <\/code><\/pre>\n<ul>\n<li>\n<p>\uff084\uff09<strong>\u8fd4\u56deConfiguration\u5bf9\u8c61\uff1a<\/strong><\/p>\n<p>\u5c06\u4e0a\u8ff0\u7684MyBatis\u521d\u59cb\u5316\u57fa\u672c\u8fc7\u7a0b\u7684\u5e8f\u5217\u56fe\u7ec6\u5316\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"660\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9AvE\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u57fa\u4e8eXML\u914d\u7f6e\u521b\u5efaConfiguration\u5bf9\u8c61\u7684\u8fc7\u7a0b<\/p>\n<\/li>\n<\/ul>\n<h3>7.3 \u57fa\u4e8eJava API\u624b\u52a8\u52a0\u8f7dXML\u914d\u7f6e\u6587\u4ef6\u521b\u5efaConfiguration\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528SqlSessionFactory\u5bf9\u8c61<\/h3>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528XMLConfigBuilder\u624b\u52a8\u89e3\u6790XML\u914d\u7f6e\u6587\u4ef6\u6765\u521b\u5efaConfiguration\u5bf9\u8c61\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"dart\">String resource = &quot;mybatis-config.xml&quot;;  \nInputStream inputStream = Resources.getResourceAsStream(resource);  \n\/\/ \u624b\u52a8\u521b\u5efaXMLConfigBuilder\uff0c\u5e76\u89e3\u6790\u521b\u5efaConfiguration\u5bf9\u8c61  \nXMLConfigBuilder parser = new XMLConfigBuilder(inputStream, null,null);  \nConfiguration configuration=parse();  \n\/\/ \u4f7f\u7528Configuration\u5bf9\u8c61\u521b\u5efaSqlSessionFactory  \nSqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);  \n\/\/ \u4f7f\u7528MyBatis  \nSqlSession sqlSession = sqlSessionFactory.openSession();  \nList list = sqlSession.selectList(&quot;com.foo.bean.BlogMapper.queryAllBlogInfo&quot;);  <\/code><\/pre>\n<h3>7.4 \u6d89\u53ca\u5230\u7684\u8bbe\u8ba1\u6a21\u5f0f<\/h3>\n<p>\u521d\u59cb\u5316\u7684\u8fc7\u7a0b\u6d89\u53ca\u5230\u521b\u5efa\u5404\u79cd\u5bf9\u8c61\uff0c\u6240\u4ee5\u4f1a\u4f7f\u7528\u4e00\u4e9b\u521b\u5efa\u578b\u7684\u8bbe\u8ba1\u6a21\u5f0f\u3002<strong>\u5728\u521d\u59cb\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0cBuilder\u6a21\u5f0f\u8fd0\u7528\u7684\u6bd4\u8f83\u591a<\/strong>\u3002<\/p>\n<h4>7.4.1 Builder\u6a21\u5f0f\u5e94\u75281\uff1a SqlSessionFactory\u7684\u521b\u5efa<\/h4>\n<p>\u5bf9\u4e8e\u521b\u5efaSqlSessionFactory\u65f6\uff0c\u4f1a<strong>\u6839\u636e\u60c5\u51b5\u63d0\u4f9b\u4e0d\u540c\u7684\u53c2\u6570\uff0c\u5176\u53c2\u6570\u7ec4\u5408\u53ef\u4ee5\u6709\u4ee5\u4e0b\u51e0\u79cd<\/strong>\uff1a<\/p>\n<p><img decoding=\"async\" width=\"464\" height=\"494\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9AvF\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6839\u636e\u60c5\u51b5\u63d0\u4f9b\u4e0d\u540c\u7684\u53c2\u6570\uff0c\u521b\u5efaSqlSessionFactory<\/p>\n<p>\u7531\u4e8e\u6784\u9020\u65f6\u53c2\u6570\u4e0d\u5b9a\uff0c\u53ef\u4ee5\u4e3a\u5176\u521b\u5efa\u4e00\u4e2a\u6784\u9020\u5668Builder\uff0c<strong>\u5c06SqlSessionFactory\u7684\u6784\u5efa\u8fc7\u7a0b\u548c\u8868\u793a\u5206\u5f00<\/strong>\uff1a<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"266\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9AvG\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>MyBatis\u5c06SqlSessionFactoryBuilder\u548cSqlSessionFactory\u76f8\u4e92\u72ec\u7acb<\/p>\n<h4>7.4.2 Builder\u6a21\u5f0f\u5e94\u75282\uff1a \u6570\u636e\u5e93\u8fde\u63a5\u73af\u5883Environment\u5bf9\u8c61\u7684\u521b\u5efa<\/h4>\n<p>\u5728\u6784\u5efaConfiguration\u5bf9\u8c61\u7684\u8fc7\u7a0b\u4e2d\uff0cXMLConfigBuilder\u89e3\u6790 mybatis XML\u914d\u7f6e\u6587\u4ef6\u8282\u70b9&lt;environment&gt;\u8282\u70b9\u65f6\uff0c\u4f1a\u6709\u4ee5\u4e0b\u76f8\u5e94\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"java\">private void environmentsElement(XNode context) throws Exception {  \n    if (context != null) {  \n        if (environment == null) {  \n            environment = context.getStringAttribute(&quot;default&quot;);  \n        }  \n        for (XNode child : context.getChildren()) {  \n            String id = child.getStringAttribute(&quot;id&quot;);  \n            \/\/\u662f\u548c\u9ed8\u8ba4\u7684\u73af\u5883\u76f8\u540c\u65f6\uff0c\u89e3\u6790\u4e4b  \n            if (isSpecifiedEnvironment(id)) {  \n                TransactionFactory txFactory = transactionManagerElement(child.evalNode(&quot;transactionManager&quot;));  \n                DataSourceFactory dsFactory = dataSourceElement(child.evalNode(&quot;dataSource&quot;));  \n                DataSource dataSource = dsFactory.getDataSource();  \n  \n                \/\/\u4f7f\u7528\u4e86Environment\u5185\u7f6e\u7684\u6784\u9020\u5668Builder\uff0c\u4f20\u9012id \u4e8b\u52a1\u5de5\u5382\u548c\u6570\u636e\u6e90  \n                Environment.Builder environmentBuilder = new Environment.Builder(id)  \n                .transactionFactory(txFactory)  \n                .dataSource(dataSource);  \n                configuration.setEnvironment(environmentBuilder.build());  \n            }  \n        }  \n    }  \n}  <\/code><\/pre>\n<p><strong>\u5728Environment\u5185\u90e8\uff0c\u5b9a\u4e49\u4e86\u9759\u6001\u5185\u90e8Builder\u7c7b\uff1a<\/strong><\/p>\n<pre><code class=\"kotlin\">public final class Environment {  \n    private final String id;  \n    private final TransactionFactory transactionFactory;  \n    private final DataSource dataSource;  \n  \n    public Environment(String id, TransactionFactory transactionFactory, DataSource dataSource) {  \n        if (id == null) {  \n            throw new IllegalArgumentException(&quot;Parameter 'id' must not be null&quot;);  \n        }  \n        if (transactionFactory == null) {  \n            throw new IllegalArgumentException(&quot;Parameter 'transactionFactory' must not be null&quot;);  \n        }  \n        this.id = id;  \n        if (dataSource == null) {  \n            throw new IllegalArgumentException(&quot;Parameter 'dataSource' must not be null&quot;);  \n        }  \n        this.transactionFactory = transactionFactory;  \n        this.dataSource = dataSource;  \n    }  \n  \n    public static class Builder {  \n        private String id;  \n        private TransactionFactory transactionFactory;  \n        private DataSource dataSource;  \n  \n        public Builder(String id) {  \n            this.id = id;  \n        }  \n  \n        public Builder transactionFactory(TransactionFactory transactionFactory) {  \n            this.transactionFactory = transactionFactory;  \n            return this;  \n        }  \n  \n        public Builder dataSource(DataSource dataSource) {  \n            this.dataSource = dataSource;  \n            return this;  \n        }  \n  \n        public String id() {  \n            return this.id;  \n        }  \n  \n        public Environment build() {  \n            return new Environment(this.id, this.transactionFactory, this.dataSource);  \n        }  \n    }  \n  \n    public String getId() {  \n        return this.id;  \n    }  \n  \n    public TransactionFactory getTransactionFactory() {  \n        return this.transactionFactory;  \n    }  \n  \n    public DataSource getDataSource() {  \n        return this.dataSource;  \n    }\n}<\/code><\/pre>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=E46K8WLH%2BHCBp5VqbUYGMg%3D%3D.NCJBK2IOTIE0FvniaeHcq7%2FOtN0L92ji84RU3wcmudc%3D\" rel=\"noopener noreferrer\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=KOFNDyqReEHRLS8Om8iVsw%3D%3D.HsATJRnYrjyZUgUiPOq9XTlqq05uJNVeclYfmmklekXo%2Biq3bphXU7NWeUBIQPe%2FT7R%2F%2Bvr6tdA2DLIUuEoVcQ%3D%3D\" rel=\"noopener noreferrer\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=GMGZVM6JkcvbbA5Ls74SCw%3D%3D.%2F%2BobwagD%2BcMM%2BoZ1JhPIZD7iM6r9IIar2hd4IwAuOKibbp%2B0MzAB%2FHXS0DY1vs65\" rel=\"noopener noreferrer\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=NF9WTcx60YZUdkRROTpDwQ%3D%3D.IKYRTCyaknycxY2ThZWZmV0yTs%2FKJ1hbG1ReDEyIntI2I8RzhR%2BC%2BIyXm%2BBy3xPOp0b4Rd4GIihFxdYbod8I9w%3D%3D\" rel=\"noopener noreferrer\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=iyQmdGBO8liuPyLH2AX%2BBQ%3D%3D.dpizQy7ejLhOyfniDCahZeW3tulY7wtNrJ%2BTn8CSfzjv%2FS%2FKljuZrgynjZkJYKnRsu5SV27A7S6keHZynMBqSflKG8H90jdW%2FDWUp5eOUQw%3D\" rel=\"noopener noreferrer\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044191959\">https:\/\/segmentfault.com\/a\/1190000044191959<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u4e03\u5bf8\u77e5\u67b6\u6784 \/\u94fe\u63a5\uff1ahttps:\/\/www.jianshu.com\/p\/ec40a82cae28 1 \u5f15 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008243","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008243","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"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008243"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008243\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}