﻿{"id":118,"date":"2016-03-16T23:11:34","date_gmt":"2016-03-16T15:11:34","guid":{"rendered":"http:\/\/www.chenweikang.top\/?p=118"},"modified":"2016-03-16T23:51:25","modified_gmt":"2016-03-16T15:51:25","slug":"mybatis%e5%ad%a6%e4%b9%a0%e4%b9%8b%e8%b7%af-helloworld","status":"publish","type":"post","link":"https:\/\/www.chenweikang.top\/?p=118","title":{"rendered":"Mybatis\u5b66\u4e60\u4e4b\u8def---HelloWorld"},"content":{"rendered":"<blockquote>\n<div class=\"para\"><\/div>\n<div class=\"para\">MyBatis \u672c\u662f<a href=\"http:\/\/baike.baidu.com\/view\/28283.htm\" target=\"_blank\">apache<\/a>\u7684\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee<a href=\"http:\/\/baike.baidu.com\/view\/628102.htm\" target=\"_blank\">iBatis<\/a>, 2010\u5e74\u8fd9\u4e2a\u9879\u76ee\u7531apache software foundation \u8fc1\u79fb\u5230\u4e86google code\uff0c\u5e76\u4e14\u6539\u540d\u4e3aMyBatis \u30022013\u5e7411\u6708\u8fc1\u79fb\u5230Github\u3002<\/div>\n<div class=\"para\">iBATIS\u4e00\u8bcd\u6765\u6e90\u4e8e\u201cinternet\u201d\u548c\u201cabatis\u201d\u7684\u7ec4\u5408\uff0c\u662f\u4e00\u4e2a\u57fa\u4e8eJava\u7684<a href=\"http:\/\/baike.baidu.com\/view\/198047.htm\" target=\"_blank\">\u6301\u4e45\u5c42<\/a>\u6846\u67b6\u3002iBATIS\u63d0\u4f9b\u7684\u6301\u4e45\u5c42\u6846\u67b6\u5305\u62ecSQL Maps\u548cData Access Objects\uff08DAO\uff09<\/div>\n<div class=\"para\"><\/div>\n<div class=\"para\">OK \u5f00\u542fMybatis\u5b66\u4e60\u4e4b\u8def<\/div>\n<\/blockquote>\n<div class=\"para\">\u00a0<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-119\" src=\"http:\/\/www.chenweikang.top\/wp-content\/uploads\/2016\/03\/1-1.png\" alt=\"1\" width=\"652\" height=\"320\" \/><\/div>\n<h2 class=\"para\">\u4e00\u3001\u521b\u5efa\u9879\u76ee \u914d\u7f6emybatis<\/h2>\n<h3>1. \u5bfc\u5165mybatis jar\u5305<\/h3>\n<blockquote><p>com.mysql.jdbc.Driver.jar ---\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p>log4j-1.2.17.jar ---\u65e5\u5fd7\u8bb0\u5f55<\/p>\n<p>mybatis-3.3.1.jar ---mybatis\u6838\u5fc3<\/p><\/blockquote>\n<h3>2. \u6dfb\u52a0\u914d\u7f6e\u6587\u4ef6<\/h3>\n<blockquote>\n<h4>A . db.properties \u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f<\/h4>\n<p>&nbsp;<\/p>\n<p>driver=com.mysql.jdbc.Driver<\/p>\n<p>url=jdbc:mysql:\/\/127.0.0.1:3306\/mybatis_db<\/p>\n<p>username=root<\/p>\n<p>password=199432<\/p>\n<h4>B. mybatis-config.xml mybatis \u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>&nbsp;<\/p>\n<p>&lt;!-- \u5f15\u5165\u6570\u636e\u5e93\u8fde\u63a5\u914d\u7f6e --&gt;<\/p>\n<p>&lt;properties resource=\"db.properties\" \/&gt;<\/p>\n<p>&lt;!-- \u6307\u5b9a\u522b\u540d\u6240\u5728\u7684\u5305 --&gt;<\/p>\n<p>&lt;typeAliases&gt;<\/p>\n<p>&lt;package name=\"com.cwk.model\" \/&gt;<\/p>\n<p>&lt;\/typeAliases&gt;<\/p>\n<p>&lt;environments default=\"development\"&gt;<\/p>\n<p>&lt;!-- \u53ef\u4ee5\u914d\u7f6e\u591a\u4e2a\u73af\u5883 \u4f7f\u7528default\u6307\u5b9a\u4f7f\u7528\u54ea\u4e2a\u73af\u5883 --&gt;<\/p>\n<p>&lt;environment id=\"development\"&gt;<\/p>\n<p>&lt;transactionManager type=\"JDBC\"\/&gt;<\/p>\n<p>&lt;dataSource type=\"POOLED\"&gt;<\/p>\n<p>&lt;property name=\"driver\" value=\"${driver}\"\/&gt;<\/p>\n<p>&lt;property name=\"url\" value=\"${url}\"\/&gt;<\/p>\n<p>&lt;property name=\"username\" value=\"${username}\"\/&gt;<\/p>\n<p>&lt;property name=\"password\" value=\"${password}\"\/&gt;<\/p>\n<p>&lt;\/dataSource&gt;<\/p>\n<p>&lt;\/environment&gt;<\/p>\n<p>&lt;\/environments&gt;<\/p>\n<p>&lt;!-- mapper\u6620\u5c04\u6587\u4ef6\u6240\u5728\u7684\u5305 --&gt;<\/p>\n<p>&lt;mappers&gt;<\/p>\n<p>&lt;package name=\"com.cwk.dao\"\/&gt;<\/p>\n<p>&lt;\/mappers&gt;<\/p>\n<p>&nbsp;<\/p><\/blockquote>\n<h2 class=\"para\">\u4e8c\u3001\u521b\u5efa\u5b9e\u4f53\u7c7b Mapper\u63a5\u53e3\u548c<\/h2>\n<h3>3. \u521b\u5efa\u5b9e\u4f53\u7c7b \uff0c\u521b\u5efaEmployeeMapper \u63a5\u53e3 \u548c \u540c\u540d\u7684\u6620\u5c04\u6587\u4ef6<\/h3>\n<blockquote><p>&nbsp;<\/p>\n<p>&lt;mapper namespace=\"com.cwk.dao.EmployeeMapper\" &gt;<br \/>\n&lt;!--\u5b9e\u73b0\u6dfb\u52a0\u6570\u636e--&gt;<br \/>\n&lt;insert id=\"add\" parameterType=\"Employee\"&gt;<br \/>\nINSERT INTO employee<br \/>\n(username,salary,deptID)<br \/>\nVALUES<br \/>\n(#{username},#{salary},#{deptID})<br \/>\n&lt;\/insert&gt;<br \/>\n&lt;\/mapper&gt;<\/p><\/blockquote>\n<h3>4. \u521b\u5efa\u5de5\u5177\u7c7b \u83b7\u53d6SqlSession<\/h3>\n<blockquote><p>&nbsp;<\/p>\n<p>public class SqlSessionFactoryUtil {<\/p>\n<p>private static SqlSessionFactory sqlSessionFactory = null;<\/p>\n<p>private static SqlSessionFactory getSessionFactory(){<\/p>\n<p>\/\/\u521d\u59cb\u5316 sqlSessionFactory<\/p>\n<p>if(sqlSessionFactory==null){<\/p>\n<p>Reader reader;<\/p>\n<p>try {<\/p>\n<p>reader = Resources.getResourceAsReader(\"mybatis-config.xml\"); \/\/\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/p>\n<p>sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);<\/p>\n<p>} catch (IOException e) {<\/p>\n<p>e.printStackTrace();<\/p>\n<p>}<br \/>\n}<\/p>\n<p>return sqlSessionFactory;<br \/>\n}<\/p>\n<p>public static SqlSession openSession(){<\/p>\n<p>return getSessionFactory().openSession(); \u00a0\/\/\u8fd4\u56deiSqlSession<br \/>\n}<\/p><\/blockquote>\n<h3>5. \u521b\u5efa\u6d4b\u8bd5\u7c7b(\u4f7f\u7528junit\u6d4b\u8bd5)<\/h3>\n<blockquote><p>&nbsp;<\/p>\n<p>private SqlSession sqlSession = null;<\/p>\n<p>private Logger logger = null;<\/p>\n<p>@Before<br \/>\npublic void setUp() throws Exception {<\/p>\n<p>\/\/\u521d\u59cb\u6570\u636e\u5e93\u8fde\u63a5<\/p>\n<p>sqlSession = SqlSessionFactoryUtil.openSession();<\/p>\n<p>\/\/\u6570\u636e\u65e5\u5fd7<\/p>\n<p>logger = Logger.getLogger(TestCRUD.class);<br \/>\n}<\/p>\n<p>@After<\/p>\n<p>public void tearDown() throws Exception {<\/p>\n<p>sqlSession.close();<br \/>\n}<\/p>\n<p>@Test<br \/>\npublic void testAdd() {<\/p>\n<p>logger.info(\"\u6dfb\u52a0\u5458\u5de5!\");<\/p>\n<p>Employee emp = new Employee(3,\"\u738b\u4e94\",3500f);<\/p>\n<p>EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class);<\/p>\n<p>int res = empMapper.add(emp);<\/p>\n<p>System.out.println(emp);<\/p>\n<p>sqlSession.commit();<br \/>\nif(res&gt;0){<\/p>\n<p>logger.info(\"\u6dfb\u52a0\u6210\u529f!\");<br \/>\n}else{<\/p>\n<p>logger.info(\"\u6dfb\u52a0\u5931\u8d25!\");<br \/>\n}}<\/p><\/blockquote>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.chenweikang.top\">\u5de6\u624b\u4ee3\u7801\u53f3\u624b\u8bd7<\/a> &raquo; <a href=\"https:\/\/www.chenweikang.top\/?p=118\">Mybatis\u5b66\u4e60\u4e4b\u8def---HelloWorld<\/a><\/p><div class=\"__youshang\">\r\n            <div id=\"__youshang_popup\" class=\"wechat popup\" style=\"display: none;\">\r\n                <div class=\"head\">~\u8c22\u8c22\u6253\u8d4f~<\/div>\r\n                <div class=\"qrcode\"><div class=\"qrcode-li wechat\" ><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/wexin.png\" \/><\/div><div class=\"qrcode-li alipay\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-pay.png\" \/><\/div><div class=\"qrcode-li hongbao\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-hongbao.png\" \/><\/div><\/div>\r\n                <ul class=\"platform\"><li class=\"icon-wechat active\" data-bg-color=\"#05af4e\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-alipay \" data-bg-color=\"#00a2ea\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-hongbao \" data-bg-color=\"#dd5746\" data-thanks=\"<p>\u626b\u7801\u9886\u7ea2\u5305<\/p><p style='margin-top: 24px;'>\uff08\u4f59\u989d\u5b9d\u652f\u4ed8\u65f6\u53ef\u62b5\u73b0\uff09<\/p>\"><\/li><\/ul>\r\n            <\/div>\r\n            <a href=\"javascript:void(0);\" id=\"__youshang_btn\">\u8d4f<\/a>\r\n        <\/div>","protected":false},"excerpt":{"rendered":"<p>MyBatis \u672c\u662fapache\u7684\u4e00\u4e2a\u5f00\u6e90\u9879\u76eeiBatis, 2010\u5e74\u8fd9\u4e2a\u9879\u76ee\u7531apache softwar [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,9,3],"tags":[32],"class_list":["post-118","post","type-post","status-publish","format-standard","hentry","category-java","category-houduan","category-note","tag-mybatis"],"_links":{"self":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=118"}],"version-history":[{"count":0,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/118\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}