{"id":1451,"date":"2022-03-29T10:45:24","date_gmt":"2022-03-29T02:45:24","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=1451"},"modified":"2022-03-29T17:51:58","modified_gmt":"2022-03-29T09:51:58","slug":"mybatis%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2022\/03\/29\/mybatis%e5%85%a5%e9%97%a8\/","title":{"rendered":"MyBatis\u5165\u95e8"},"content":{"rendered":"<p>MyBatis \u662f\u4e00\u6b3e\u4f18\u79c0\u7684\u6301\u4e45\u5c42\u6846\u67b6\uff0c\u5b83\u652f\u6301\u81ea\u5b9a\u4e49 SQL\u3001\u5b58\u50a8\u8fc7\u7a0b\u4ee5\u53ca\u9ad8\u7ea7\u6620\u5c04\u3002MyBatis \u514d\u9664\u4e86\u51e0\u4e4e\u6240\u6709\u7684 JDBC \u4ee3\u7801\u4ee5\u53ca\u8bbe\u7f6e\u53c2\u6570\u548c\u83b7\u53d6\u7ed3\u679c\u96c6\u7684\u5de5\u4f5c\u3002MyBatis \u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684 XML \u6216\u6ce8\u89e3\u6765\u914d\u7f6e\u548c\u6620\u5c04\u539f\u59cb\u7c7b\u578b\u3001\u63a5\u53e3\u548c Java POJO\uff08Plain Old Java Objects\uff0c\u666e\u901a\u8001\u5f0f Java \u5bf9\u8c61\uff09\u4e3a\u6570\u636e\u5e93\u4e2d\u7684\u8bb0\u5f55\u3002<\/p>\n<p>\u4f7f\u7528MyBatis\u4e4b\u524d\uff0c\u9700\u8981\u5148\u4e0b\u8f7djar\u5305\u5e76\u5bfc\u5165\u5230\u9879\u76ee\u5e93\u4e2d\u3002<\/p>\n<p>mybatis\u6846\u67b6\u4e3b\u8981\u662f\u56f4\u7ed5\u7740<code>SqlSessionFactory<\/code>\u6765\u8fdb\u884c\u7684\uff0cSqlSessionFactory \u7684\u5b9e\u4f8b\u53ef\u4ee5\u901a\u8fc7 SqlSessionFactoryBuilder \u83b7\u5f97\u3002\u800c SqlSessionFactoryBuilder \u5219\u53ef\u4ee5\u4ece XML \u914d\u7f6e\u6587\u4ef6\u6216\u4e00\u4e2a\u9884\u5148\u914d\u7f6e\u7684 Configuration \u5b9e\u4f8b\u6765\u6784\u5efa\u51fa SqlSessionFactory \u5b9e\u4f8b\u3002<code>SqlSessionFactory<\/code>\u7684\u521b\u5efa\u8fc7\u7a0b\u5927\u81f4\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5b9a\u4e49\u4e00\u4e2aConfiguration\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u6570\u636e\u6e90\u3001\u4e8b\u52a1\u3001mapper\u6587\u4ef6\u8d44\u6e90\u4ee5\u53ca\u5f71\u54cd\u6570\u636e\u5e93\u884c\u4e3a\u5c5e\u6027\u8bbe\u7f6esettings<\/li>\n<li>\u901a\u8fc7\u914d\u7f6e\u5bf9\u8c61\uff0c\u5219\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aSqlSessionFactoryBuilder\u5bf9\u8c61<\/li>\n<li>\u901a\u8fc7 SqlSessionFactoryBuilder \u83b7\u5f97SqlSessionFactory \u7684\u5b9e\u4f8b\u3002<\/li>\n<li>SqlSessionFactory\u7684\u5b9e\u4f8b\u53ef\u4ee5\u83b7\u5f97\u64cd\u4f5c\u6570\u636e\u7684SqlSession\u5b9e\u4f8b\uff0c\u901a\u8fc7\u8fd9\u4e2a\u5b9e\u4f8b\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u64cd\u4f5c<\/li>\n<\/ol>\n<h5>MyBatis\u914d\u7f6e<\/h5>\n<p>MyBatis\u4e0e\u6570\u636e\u5e93\u7684\u8fde\u63a5\u901a\u8fc7.xml\u914d\u7f6e\u6587\u4ef6\u6765\u5b8c\u6210\uff0c\u6211\u4eec\u53ea\u9700\u8981\u586b\u5199\u914d\u7f6e\u6587\u4ef6\u7684\u4e00\u4e9b\u5c5e\u6027\u548c\u4fe1\u606f\u5c31\u53ef\u4ee5\uff0c\u975e\u5e38\u65b9\u4fbf\u3002<br \/>\nXML \u914d\u7f6e\u6587\u4ef6\u4e2d\u5305\u542b\u4e86\u5bf9 MyBatis \u7cfb\u7edf\u7684\u6838\u5fc3\u8bbe\u7f6e\uff0c\u5305\u62ec\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u5b9e\u4f8b\u7684\u6570\u636e\u6e90\uff08DataSource\uff09\u4ee5\u53ca\u51b3\u5b9a\u4e8b\u52a1\u4f5c\u7528\u57df\u548c\u63a7\u5236\u65b9\u5f0f\u7684\u4e8b\u52a1\u7ba1\u7406\u5668\uff08TransactionManager\uff09\u3002<br \/>\n``<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;\n&lt;!DOCTYPE configuration\n        PUBLIC &quot;-\/\/mybatis.org\/\/DTD Config 3.0\/\/EN&quot;\n        &quot;http:\/\/mybatis.org\/dtd\/mybatis-3-config.dtd&quot;&gt;\n&lt;configuration&gt;\n    &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;\n    &lt;mappers&gt;\n        &lt;mapper url=&quot;file:mappers\/TestMapper.xml&quot;\/&gt;\n        &lt;!-- \u8fd9\u5c31\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6620\u5c04\u5668 --&gt;\n    &lt;\/mappers&gt;\n&lt;\/configuration&gt;<\/code><\/pre>\n<p><code>DOCTYPE configuration<\/code>\u8bf4\u660e\u8fd9\u662f\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\u3002<code>http:\/\/mybatis.org\/dtd\/mybatis-3-config.dtd<\/code>\u662f\u4e00\u4e2a\u6587\u6863\u5b9a\u4e49\u7c7b\u578b\u6587\u4ef6(.dtd)\uff0c\u5b83\u91cc\u9762\u89c4\u5b9a\u4e86\u6211\u4eec\u5982\u4f55\u4f7f\u7528\u6807\u7b7e\u6765\u6b63\u786e\u8fdb\u884c\u914d\u7f6e\u3002\u5728\u4e0a\u9762\u7684xml\u6587\u4ef6\u4e2d\u6709\u56db\u4e2a\u53f6\u8282\u70b9<code>property<\/code>\uff0c\u5206\u522b\u6307\u5b9a\u8fde\u63a5\u6570\u636e\u5e93\u7684\u9a71\u52a8\uff0cURL\uff0c\u767b\u5f55\u7528\u6237\u540d\uff0c\u5bc6\u7801\u3002<\/p>\n<h5>\u521b\u5efasession<\/h5>\n<p>\u914d\u7f6e\u597d\u4e4b\u540e\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528MyBatis\u3002SqlSessionFactoryBuilder\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6765\u521b\u5efa\u4e00\u4e2aSqlSessionFactory\u5b9e\u4f8b\uff0c\u6bcf\u4e2a\u57fa\u4e8e MyBatis \u7684\u5e94\u7528\u90fd\u662f\u4ee5\u4e00\u4e2a SqlSessionFactory \u7684\u5b9e\u4f8b\u4e3a\u6838\u5fc3\u7684\uff0c\u5b83\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002SqlSessionFactory\u53c8\u53ef\u4ee5\u901a\u8fc7openSession\u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2asession\u5bf9\u8c61\uff0csession\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\uff0c\u4f9b\u4e0d\u540c\u7ebf\u7a0b\u4f7f\u7528\uff0c\u5b83\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\u3002<\/p>\n<p>SqlSession\u662fMyBatis\u7684\u5173\u952e\u5bf9\u8c61\uff0c\u662f\u6267\u884c\u6301\u4e45\u5316\u64cd\u4f5c\u7684\u72ec\u4eab\uff0c\u7c7b\u4f3c\u4e8eJDBC\u4e2d\u7684Connection\u3002\u5b83\u662f\u5e94\u7528\u7a0b\u5e8f\u4e0e\u6301\u4e45\u5c42\u4e4b\u95f4\u6267\u884c\u4ea4\u4e92\u64cd\u4f5c\u7684\u4e00\u4e2a\u5355\u7ebf\u7a0b\u5bf9\u8c61\uff0c\u4e5f\u662fMyBatis\u6267\u884c\u6301\u4e45\u5316\u64cd\u4f5c\u7684\u5173\u952e\u5bf9\u8c61\u3002SqlSession\u5bf9\u8c61\u5b8c\u5168\u5305\u542b\u4ee5\u6570\u636e\u5e93\u4e3a\u80cc\u666f\u7684\u6240\u6709\u6267\u884cSQL\u64cd\u4f5c\u7684\u65b9\u6cd5\uff0c\u5b83\u7684\u5e95\u5c42\u5c01\u88c5\u4e86JDBC\u8fde\u63a5\uff0c\u53ef\u4ee5\u7528SqlSession\u5b9e\u4f8b\u6765\u76f4\u63a5\u6267\u884c\u88ab\u6620\u5c04\u7684SQL\u8bed\u53e5\u3002\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u5e94\u8be5\u6709\u5b83\u81ea\u5df1\u7684SqlSession\u5b9e\u4f8b\u3002SqlSession\u7684\u5b9e\u4f8b\u4e0d\u80fd\u88ab\u5171\u4eab\uff0c\u540c\u65f6SqlSession\u4e5f\u662f\u7ebf\u7a0b\u4e0d\u5b89\u5168\u7684\uff0c\u7edd\u5bf9\u4e0d\u80fd\u5c06SqlSession\u5b9e\u4f8b\u7684\u5f15\u7528\u653e\u5728\u4e00\u4e2a\u7c7b\u7684\u9759\u6001\u5b57\u6bb5\u751a\u81f3\u662f\u5b9e\u4f8b\u5b57\u6bb5\u4e2d\u3002\u4e5f\u7edd\u4e0d\u80fd\u5c06SqlSession\u5b9e\u4f8b\u7684\u5f15\u7528\u653e\u5728\u4efb\u4f55\u7c7b\u578b\u7684\u7ba1\u7406\u8303\u56f4\u4e2d\uff0c\u6bd4\u5982Servlet\u5f53\u4e2d\u7684HttpSession\u5bf9\u8c61\u4e2d\u3002\u4f7f\u7528\u5b8cSqlSsesion\u4e4b\u540e\u5173\u95edSession\u5f88\u91cd\u8981\uff0c\u5e94\u8be5\u786e\u4fdd\u4f7f\u7528finally\u5757\u6765\u5173\u95ed\u5b83\u3002<\/p>\n<pre><code class=\"language-java\">public static void main(String[] args) throws FileNotFoundException {\n    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(new FileInputStream(&quot;mybatis-config.xml&quot;));\n    try (SqlSession sqlSession = sqlSessionFactory.openSession(true)){     \/\/ \u8fd9\u91cc\u7684true\u8868\u793a\u5f00\u542f\u81ea\u52a8\u63d0\u4ea4\uff0c\u82e5false\u5219\u8fdb\u5165\u4e8b\u52a1\u6a21\u5f0f\uff0c\u9700\u624b\u52a8commit\n            \/\/\u6682\u65f6\u8fd8\u6ca1\u6709\u4e1a\u52a1\n    }\n}<\/code><\/pre>\n<p>openSession(boolean b)\u65b9\u6cd5\u91cc\u7684\u4e00\u4e2a\u53c2\u6570\u8868\u793a\u662f\u5426\u5f00\u542f\u81ea\u52a8\u63d0\u4ea4autoCommit\u3002<br \/>\n<img src=\"http:\/\/www.zyhcoding.club:80\/wp-content\/uploads\/2022\/03\/SqlSessionFactoryBuilder.png\" alt=\"\" \/><\/p>\n<p>SqlSession\u51e0\u4e4e\u53ef\u4ee5\u5b8c\u6210\u6240\u6709\u7684\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u63a5\u53e3\u4e2d\u5b9a\u4e49\u4e86\u5927\u91cf\u7684\u65b9\u6cd5\u3002<\/p>\n<h5>\u6620\u5c04\u5668mapper<\/h5>\n<p>\u73b0\u5728\u6211\u4eec\u60f3\u8bfb\u53d6\u4e00\u5f20\u8868\u5e76\u5c06\u5176\u5185\u5bb9\u6620\u5c04\u4e3a\u4e00\u4e2a\u5b9e\u4f53\u7c7b\uff0c\u9996\u5148\u7f16\u5199\u5b9e\u4f53\u7c7b\u3002<\/p>\n<pre><code class=\"language-java\">import lombok.Data;\n\n@Data\npublic class Student {\n    int sid;\n    String name;\n    String sex;\n}<\/code><\/pre>\n<p>\u7136\u540e\u9700\u8981\u5728\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2amapper\u6587\u4ef6\u5939\uff0c\u521b\u5efa\u65b0\u7684xml\u6587\u4ef6\u6765\u4f5c\u4e3a\u6620\u5c04\u5668\u3002<br \/>\nTestMapper.xml\u6587\u4ef6<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;\n&lt;!DOCTYPE mapper\n        PUBLIC &quot;-\/\/mybatis.org\/\/DTD Mapper 3.0\/\/EN&quot;\n        &quot;http:\/\/mybatis.org\/dtd\/mybatis-3-mapper.dtd&quot;&gt;\n&lt;mapper namespace=&quot;TestMapper&quot;&gt;\n    &lt;select id=&quot;selectStudent&quot; resultType=&quot;com.test.entity.Student&quot;&gt;\n        select * from student\n    &lt;\/select&gt;\n&lt;\/mapper&gt;<\/code><\/pre>\n<p>\u5176\u4e2dnamespace\u5c31\u662f\u547d\u540d\u7a7a\u95f4\uff0c\u6bcf\u4e2aMapper\u90fd\u662f\u552f\u4e00\u7684\uff0c\u56e0\u6b64\u9700\u8981\u7528\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u6765\u533a\u5206\uff0c\u5b83\u8fd8\u53ef\u4ee5\u7528\u6765\u7ed1\u5b9a\u4e00\u4e2a\u63a5\u53e3\u3002\u6211\u4eec\u5728\u91cc\u9762\u5199\u5165\u4e86\u4e00\u4e2aselect\u6807\u7b7e\uff0c\u8868\u793a\u6dfb\u52a0\u4e00\u4e2aselect\u64cd\u4f5c\uff0c\u540c\u65f6id\u4f5c\u4e3a\u64cd\u4f5c\u7684\u540d\u79f0\uff0cresultType\u6307\u5b9a\u4e3a\u6211\u4eec\u521a\u521a\u5b9a\u4e49\u7684\u5b9e\u4f53\u7c7b\uff0c\u8868\u793a\u5c06\u6570\u636e\u5e93\u7ed3\u679c\u6620\u5c04\u4e3a<code>Student<\/code>\u7c7b\uff0c\u7136\u540e\u5c31\u5728\u6807\u7b7e\u4e2d\u5199\u5165\u6211\u4eec\u7684\u67e5\u8be2\u8bed\u53e5\u5373\u53ef\u3002<\/p>\n<p>\u8fd9\u6837\u5c31\u5199\u597d\u4e86\u4e00\u4e2a\u6620\u5c04\u5668\uff0c\u7136\u540e\u518d\u5c06\u6b64\u6620\u5c04\u5668\u6dfb\u52a0\u5230MyBatis\u914d\u7f6e\u6587\u4ef6\u4e2d\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;!-- \u5728mappers\u6807\u7b7e\u5185\u90e8\u6dfb\u52a0\u591a\u4e2a\u6620\u5c04\u5668 --&gt;\n&lt;mappers&gt;\n    &lt;mapper url=&quot;file:mappers\/TestMapper.xml&quot;\/&gt;\n    &lt;!-- \u8fd9\u5c31\u6dfb\u52a0\u4e86\u4e00\u4e2a\u6620\u5c04\u5668\uff0c\u9664\u4e86url\u8fd8\u53ef\u4ee5\u7528resource --&gt;\n&lt;\/mappers&gt;<\/code><\/pre>\n<p>\u4f7f\u7528selectList\u65b9\u6cd5\u6765\u83b7\u53d6\u591a\u884c\u6570\u636e\u3002<\/p>\n<pre><code class=\"language-java\">public class Main {\n    public static void main(String[] args) throws FileNotFoundException {\n        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(new FileInputStream(&quot;mybatis-config.xml&quot;));\n        try (SqlSession sqlSession = sqlSessionFactory.openSession(true)){\n\n            List&lt;Student&gt; list=  sqlSession.selectList(&quot;selectStudent&quot;);\n            list.stream()\n                    .forEach(System.out::println);\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>MyBatis \u662f\u4e00\u6b3e\u4f18\u79c0\u7684\u6301\u4e45\u5c42\u6846\u67b6\uff0c\u5b83\u652f\u6301\u81ea\u5b9a\u4e49 SQL\u3001\u5b58\u50a8\u8fc7\u7a0b\u4ee5\u53ca\u9ad8\u7ea7\u6620\u5c04\u3002MyBatis  [&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\/1451"}],"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=1451"}],"version-history":[{"count":11,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1451\/revisions"}],"predecessor-version":[{"id":1457,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/1451\/revisions\/1457"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=1451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=1451"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=1451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}