{"id":1466,"date":"2022-03-30T20:50:39","date_gmt":"2022-03-30T12:50:39","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=1466"},"modified":"2022-03-30T21:13:48","modified_gmt":"2022-03-30T13:13:48","slug":"mybatis%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2022\/03\/30\/mybatis%e9%85%8d%e7%bd%ae\/","title":{"rendered":"MyBatis\u914d\u7f6e"},"content":{"rendered":"<h5>\u8bbe\u7f6e<\/h5>\n<p>settings\u662fMyBatis\u4e2d\u6781\u4e3a\u91cd\u8981\u7684\u8c03\u6574\u8bbe\u7f6e\uff0c\u5b83\u4eec\u4f1a\u6539\u53d8 MyBatis \u7684\u8fd0\u884c\u65f6\u884c\u4e3a\u3002<\/p>\n<ul>\n<li>Enabled - \u5168\u5c40\u6027\u5730\u5f00\u542f\u6216\u5173\u95ed\u6240\u6709\u6620\u5c04\u5668\u914d\u7f6e\u6587\u4ef6\u4e2d\u5df2\u914d\u7f6e\u7684\u4efb\u4f55\u7f13\u5b58\u3002\u9ed8\u8ba4\u4e3atrue<\/li>\n<li>defaultStatementTimeout - \u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4\uff0c\u5b83\u51b3\u5b9a\u6570\u636e\u5e93\u9a71\u52a8\u7b49\u5f85\u6570\u636e\u5e93\u54cd\u5e94\u7684\u79d2\u6570\u3002<\/li>\n<li>\n<\/li>\n<\/ul>\n<p>settings\u914d\u7f6e<\/p>\n<pre><code class=\"language-xml\">&lt;settings&gt;\n  &lt;setting name=&quot;cacheEnabled&quot; value=&quot;true&quot;\/&gt;\n  &lt;setting name=&quot;lazyLoadingEnabled&quot; value=&quot;true&quot;\/&gt;\n  &lt;setting name=&quot;multipleResultSetsEnabled&quot; value=&quot;true&quot;\/&gt;\n  &lt;setting name=&quot;useColumnLabel&quot; value=&quot;true&quot;\/&gt;\n  &lt;setting name=&quot;useGeneratedKeys&quot; value=&quot;false&quot;\/&gt;\n  &lt;setting name=&quot;autoMappingBehavior&quot; value=&quot;PARTIAL&quot;\/&gt;\n  &lt;setting name=&quot;autoMappingUnknownColumnBehavior&quot; value=&quot;WARNING&quot;\/&gt;\n  &lt;setting name=&quot;defaultExecutorType&quot; value=&quot;SIMPLE&quot;\/&gt;\n  &lt;setting name=&quot;defaultStatementTimeout&quot; value=&quot;25&quot;\/&gt;\n  &lt;setting name=&quot;defaultFetchSize&quot; value=&quot;100&quot;\/&gt;\n  &lt;setting name=&quot;safeRowBoundsEnabled&quot; value=&quot;false&quot;\/&gt;\n  &lt;setting name=&quot;mapUnderscoreToCamelCase&quot; value=&quot;false&quot;\/&gt;\n  &lt;setting name=&quot;localCacheScope&quot; value=&quot;SESSION&quot;\/&gt;\n  &lt;setting name=&quot;jdbcTypeForNull&quot; value=&quot;OTHER&quot;\/&gt;\n  &lt;setting name=&quot;lazyLoadTriggerMethods&quot; value=&quot;equals,clone,hashCode,toString&quot;\/&gt;\n&lt;\/settings&gt;<\/code><\/pre>\n<h5>\u7c7b\u578b\u522b\u540d\uff08typeAlises\uff09<\/h5>\n<p>\u7c7b\u578b\u522b\u540d\u53ef\u4e3a Java \u7c7b\u578b\u8bbe\u7f6e\u4e00\u4e2a\u7f29\u5199\u540d\u5b57\u3002 \u5b83\u4ec5\u7528\u4e8e XML \u914d\u7f6e\uff0c\u610f\u5728\u964d\u4f4e\u5197\u4f59\u7684\u5168\u9650\u5b9a\u7c7b\u540d\u4e66\u5199\u3002<code>type<\/code>\u5b57\u6bb5\u8868\u793a\u6240\u9009\u7684\u7c7b\u540d\uff0c<code>alias<\/code>\u8868\u793a\u5bf9\u8be5\u7c7b\u8d77\u7684\u522b\u540d\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;typeAlises&gt;\n  &lt;typeAlias type=&quot;domain.blog.Author&quot; alias=&quot;Author&quot;\/&gt;\n  &lt;typeAlias type=&quot;domain.blog.Blog&quot; alias=&quot;Blog&quot;\/&gt;\n  &lt;typeAlias type=&quot;domain.blog.Comment&quot; alias=&quot;Comment&quot;\/&gt;\n  &lt;typeAlias type=&quot;domain.blog.Post&quot; alias=&quot;Post&quot;\/&gt;\n  &lt;typeAlias type=&quot;domain.blog.Section&quot;alias=&quot;Section&quot;\/&gt;\n  &lt;typeAlias alias=&quot;Tag&quot; type=&quot;domain.blog.Tag&quot; alias=&quot;Tag&quot;\/&gt;\n&lt;\/typeAlises&gt;<\/code><\/pre>\n<p>\u4e5f\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u4e00\u4e2a\u5305\u540d\uff0c\u8fd9\u6837\u8fd9\u4e2a\u5305\u5185\u7684\u6240\u6709\u7c7b\u90fd\u4f1a\u6709\u4e00\u4e2a\u522b\u540d\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;typeAliases&gt;\n  &lt;package name=&quot;domain.blog&quot;\/&gt;\n&lt;\/typeAliases&gt;<\/code><\/pre>\n<p>\u9ed8\u8ba4\u4f1a\u4f7f\u7528\u9996\u5b57\u6bcd\u5c0f\u5199\u7684\u7c7b\u5168\u9650\u5b9a\u540d\u6765\u4f5c\u4e3a\u7c7b\u7684\u522b\u540d\u3002\u4e5f\u53ef\u4ee5\u4f7f\u7528\u6ce8\u89e3\u6765\u6307\u5b9a\u522b\u540d\u3002<\/p>\n<pre><code class=\"language-java\">@Alias(&quot;abcde&quot;)   \/\/ abcde\u4e3a\u8be5\u7c7b\u7684\u522b\u540d\npublic class Student {\n    private int sid;\n    private String name;\n    private String sex;\n}<\/code><\/pre>\n<h5>\u73af\u5883\u914d\u7f6e\uff08environments\uff09<\/h5>\n<p>MyBatis \u53ef\u4ee5\u914d\u7f6e\u6210\u9002\u5e94\u591a\u79cd\u73af\u5883\uff0c\u8fd9\u79cd\u673a\u5236\u6709\u52a9\u4e8e\u5c06 SQL \u6620\u5c04\u5e94\u7528\u4e8e\u591a\u79cd\u6570\u636e\u5e93\u4e4b\u4e2d\uff0c \u73b0\u5b9e\u60c5\u51b5\u4e0b\u6709\u591a\u79cd\u7406\u7531\u9700\u8981\u8fd9\u4e48\u505a\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;environments default=&quot;development&quot;&gt;\n        &lt;environment id=&quot;development&quot;&gt;\n            &lt;transactionManager type=&quot;JDBC&quot;\/&gt;\n            &lt;dataSource type=&quot;POOLED&quot;&gt;\n                &lt;property name=&quot;driver&quot; value=&quot;com.mysql.cj.jdbc.Driver&quot;\/&gt;\n                &lt;property name=&quot;url&quot; value=&quot;jdbc:mysql:\/\/localhost:3306\/study&quot;\/&gt;\n                &lt;property name=&quot;username&quot; value=&quot;root&quot;\/&gt;\n                &lt;property name=&quot;password&quot; value=&quot;123456&quot;\/&gt;\n            &lt;\/dataSource&gt;\n        &lt;\/environment&gt;\n    &lt;\/environments&gt;<\/code><\/pre>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u53ef\u4ee5\u8bbe\u5b9a\u591a\u4e2a\u73af\u5883\uff0c\u6bcf\u4e2aSqlSessionFactory\u53ea\u80fd\u5bf9\u5e94\u4e00\u4e2a\u73af\u5883\uff0c\u53ef\u4ee5\u5728\u521b\u5efa\u65f6\u6307\u5b9a\u4f7f\u7528\u54ea\u4e2a\u73af\u5883\u3002<\/p>\n<pre><code class=\"language-java\">SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environment);\nSqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader, environment, properties);<\/code><\/pre>\n<p>\u5f53\u6ca1\u6709\u6307\u5b9a\u6240\u4f7f\u7528\u7684\u73af\u5883\u65f6\uff0c\u4f1a\u4f7f\u7528\u9ed8\u8ba4\u73af\u5883\u3002<\/p>\n<p>\u914d\u7f6e\u73af\u5883\u7684\u4e00\u4e9b\u5185\u5bb9\uff1a<\/p>\n<ul>\n<li>id - \u8868\u793a\u8be5\u73af\u5883\u7684\u540d\u79f0\u3002<\/li>\n<li>transactionManager - \u4e8b\u52a1\u7ba1\u7406\u5668\u7684\u914d\u7f6e<\/li>\n<li>dataSource - \u6570\u636e\u6e90\u7684\u914d\u7f6e<\/li>\n<\/ul>\n<p><strong>\u4e8b\u52a1\u7ba1\u7406\u5668<\/strong>\u5728MyBatis\u4e2d\u6709\u4e24\u79cd\u4e8b\u52a1\u7ba1\u7406\u5668\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;transactionManager type=&quot;[JDBC|MANAGED]&quot;&gt;\n      &lt;property name=&quot;...&quot; value=&quot;...&quot;\/&gt;\n    &lt;\/transactionManager&gt;<\/code><\/pre>\n<ul>\n<li>JDBC - \u8fd9\u4e2a\u914d\u7f6e\u76f4\u63a5\u4f7f\u7528\u4e86 JDBC \u7684\u63d0\u4ea4\u548c\u56de\u6eda\u8bbe\u65bd\uff0c\u5b83\u4f9d\u8d56\u4ece\u6570\u636e\u6e90\u83b7\u5f97\u7684\u8fde\u63a5\u6765\u7ba1\u7406\u4e8b\u52a1\u4f5c\u7528\u57df\u3002<\/li>\n<li>MANAGED - \u8fd9\u4e2a\u914d\u7f6e\u51e0\u4e4e\u6ca1\u505a\u4ec0\u4e48\u3002\u5b83\u4ece\u4e0d\u63d0\u4ea4\u6216\u56de\u6eda\u4e00\u4e2a\u8fde\u63a5\uff0c\u800c\u662f\u8ba9\u5bb9\u5668\u6765\u7ba1\u7406\u4e8b\u52a1\u7684\u6574\u4e2a\u751f\u547d\u5468\u671f\uff08\u6bd4\u5982 JEE \u5e94\u7528\u670d\u52a1\u5668\u7684\u4e0a\u4e0b\u6587\uff09\u3002<\/li>\n<\/ul>\n<p><strong>\u6570\u636e\u6e90\uff08dataSource\uff09<\/strong>dataSource \u5143\u7d20\u4f7f\u7528\u6807\u51c6\u7684 JDBC \u6570\u636e\u6e90\u63a5\u53e3\u6765\u914d\u7f6e JDBC \u8fde\u63a5\u5bf9\u8c61\u7684\u8d44\u6e90\u3002\u6709\u4e09\u79cd\u5185\u5efa\u7684\u6570\u636e\u6e90\u7c7b\u578b\uff08\u4e5f\u5c31\u662f type=&quot;[UNPOOLED|POOLED|JNDI]&quot;\uff09<\/p>\n<ul>\n<li>POOLED - \u8fd9\u79cd\u6570\u636e\u6e90\u7684\u5b9e\u73b0\u5229\u7528\u201c\u6c60\u201d\u7684\u6982\u5ff5\u5c06 JDBC \u8fde\u63a5\u5bf9\u8c61\u7ec4\u7ec7\u8d77\u6765\uff0c\u907f\u514d\u4e86\u521b\u5efa\u65b0\u7684\u8fde\u63a5\u5b9e\u4f8b\u65f6\u6240\u5fc5\u9700\u7684\u521d\u59cb\u5316\u548c\u8ba4\u8bc1\u65f6\u95f4\u3002 \u8fd9\u79cd\u5904\u7406\u65b9\u5f0f\u5f88\u6d41\u884c\uff0c\u80fd\u4f7f\u5e76\u53d1 Web \u5e94\u7528\u5feb\u901f\u54cd\u5e94\u8bf7\u6c42\u3002<\/li>\n<li>UNPOOLED \u2013 \u8fd9\u4e2a\u6570\u636e\u6e90\u7684\u5b9e\u73b0\u4f1a\u6bcf\u6b21\u8bf7\u6c42\u65f6\u6253\u5f00\u548c\u5173\u95ed\u8fde\u63a5\u3002\u867d\u7136\u6709\u70b9\u6162\uff0c\u4f46\u5bf9\u90a3\u4e9b\u6570\u636e\u5e93\u8fde\u63a5\u53ef\u7528\u6027\u8981\u6c42\u4e0d\u9ad8\u7684\u7b80\u5355\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\uff0c\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002<\/li>\n<li>JNDI \u2013 \u8fd9\u4e2a\u6570\u636e\u6e90\u5b9e\u73b0\u662f\u4e3a\u4e86\u80fd\u5728\u5982 EJB \u6216\u5e94\u7528\u670d\u52a1\u5668\u8fd9\u7c7b\u5bb9\u5668\u4e2d\u4f7f\u7528\uff0c\u5bb9\u5668\u53ef\u4ee5\u96c6\u4e2d\u6216\u5728\u5916\u90e8\u914d\u7f6e\u6570\u636e\u6e90\uff0c\u7136\u540e\u653e\u7f6e\u4e00\u4e2a JNDI \u4e0a\u4e0b\u6587\u7684\u6570\u636e\u6e90\u5f15\u7528\u3002<\/li>\n<\/ul>\n<p>\u5e38\u7528\u7684\u5c5e\u6027\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<\/p>\n<ul>\n<li>driver \u2013 \u8fd9\u662f JDBC \u9a71\u52a8\u7684 Java \u7c7b\u5168\u9650\u5b9a\u540d\uff08\u5e76\u4e0d\u662f JDBC \u9a71\u52a8\u4e2d\u53ef\u80fd\u5305\u542b\u7684\u6570\u636e\u6e90\u7c7b\uff09\u3002<\/li>\n<li>url \u2013 \u8fd9\u662f\u6570\u636e\u5e93\u7684 JDBC URL \u5730\u5740\u3002<\/li>\n<li>username \u2013 \u767b\u5f55\u6570\u636e\u5e93\u7684\u7528\u6237\u540d\u3002<\/li>\n<li>password \u2013 \u767b\u5f55\u6570\u636e\u5e93\u7684\u5bc6\u7801\u3002<\/li>\n<li>defaultTransactionIsolationLevel \u2013 \u9ed8\u8ba4\u7684\u8fde\u63a5\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u3002<\/li>\n<li>defaultNetworkTimeout \u2013 \u7b49\u5f85\u6570\u636e\u5e93\u64cd\u4f5c\u5b8c\u6210\u7684\u9ed8\u8ba4\u7f51\u7edc\u8d85\u65f6\u65f6\u95f4\uff08\u5355\u4f4d\uff1a\u6beb\u79d2\uff09\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u8bbe\u7f6e settings\u662fMyBatis\u4e2d\u6781\u4e3a\u91cd\u8981\u7684\u8c03\u6574\u8bbe\u7f6e\uff0c\u5b83\u4eec\u4f1a\u6539\u53d8 MyBatis \u7684\u8fd0\u884c\u65f6\u884c\u4e3a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[79],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1466"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=1466"}],"version-history":[{"count":3,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1466\/revisions"}],"predecessor-version":[{"id":1469,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1466\/revisions\/1469"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=1466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=1466"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=1466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}