{"id":5168,"date":"2018-04-08T13:17:57","date_gmt":"2018-04-08T04:17:57","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=5168"},"modified":"2020-05-28T13:57:27","modified_gmt":"2020-05-28T04:57:27","slug":"mybatis-%eb%9d%bc%ec%9d%b4%eb%b8%8c%eb%9f%ac%eb%a6%ac-%ec%82%ac%ec%9a%a9%ed%95%98%ea%b8%b0","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=5168","title":{"rendered":"mybatis \ub77c\uc774\ube0c\ub7ec\ub9ac \uc0ac\uc6a9\ud558\uae30"},"content":{"rendered":"<p>mybatis API\ub97c \uc0ac\uc6a9\ud574 DB\ub97c \uc5f0\uacb0\ud558\uace0 SQL\ubb38\uc744 \ub358\uc838 \uadf8 \uacb0\uacfc\ub97c \uc5bb\uc5b4\uc624\ub294 \uc608\uc81c \ucf54\ub4dc\ub97c \uc815\ub9ac\ud569\ub2c8\ub2e4. \ud5a5\ud6c4 \ud55c\ub2e8\uacc4 \uc218\uc900 \ub192\uc740 \uc751\uc6a9\uc744 \uc704\ud574 \ucd5c\uc18c\ud55c\uc758 \ucf54\ub4dc\ub9cc\uc73c\ub85c mybatis\ub97c \ud65c\uc6a9\ud558\ub294 \ud504\ub85c\uc81d\ud2b8\uac00 \uac16\ucdb0\uc57c\ud560 \ucd5c\uc18c\ud55c\uc758 \uc124\uc815 \ubc0f \ucf54\ub4dc\ub9cc\uc744 \uc5b8\uae09\ud569\ub2c8\ub2e4.<\/p>\n<p>\uc774\ud074\ub9bd\uc2a4\ub97c \ud1b5\ud574 \uc2e0\uaddc \ud504\ub85c\uc81d\ud2b8\ub97c \uc0dd\uc131\ud558\uace0, \uc774\ub54c \uc0ac\uc6a9\ud55c \ud504\ub85c\uc81d\ud2b8 \uba85\uc740 tstMyBatis\ub85c \uc9c0\uc815\ud558\uc600\uc2b5\ub2c8\ub2e4. main \ud568\uc218\ub294 EntryMain \ud074\ub798\uc2a4\uc5d0 \ub2f4\uc558\uc2b5\ub2c8\ub2e4. EntryMain.java\uc5d0 \ub300\ud55c \ub0b4\uc6a9\uc740 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstMyBatis;\r\n\r\nimport org.apache.ibatis.session.SqlSession;\r\n\r\npublic class EntryMain {\r\n    public static void main(String[] args) {\r\n        SqlSession session = SqlMapClient.getSqlSession();\r\n        String player = session.selectOne(\"test.getFirstPlayer\");\r\n        System.out.println(\"Player Name: \" + player);\r\n        session.close();\r\n    }\r\n}\r\n<\/pre>\n<p>mybatis\ub294 Java\uc758 \uae30\ubcf8 \ub77c\uc774\ube0c\ub7ec\ub9ac\uac00 \uc544\ub2c8\ubbc0\ub85c https:\/\/github.com\/mybatis\/mybatis-3\/releases \uc5d0\uc11c \ub2e4\uc6b4\ub85c\ub4dc \ubc1b\uc2b5\ub2c8\ub2e4. \ubc84\uc804\uc5d0 \ub530\ub77c \ud30c\uc77c\uba85\uc740 \ub2ec\ub77c\uc9c0\uaca0\uc9c0\ub9cc, \ud30c\uc77c \uc911 mybatis-3.4.6.jar \ud30c\uc77c\uc744 \ud504\ub85c\uc81d\ud2b8\uc5d0 libs \ub514\ub809\ud1a0\ub9ac\ub97c \ub9cc\ub4e4\uc5b4 \ub123\uace0 \ucc38\uc870\ud569\ub2c8\ub2e4. \uc544\uc6b8\ub7ec \uc5f0\uacb0\ud558\uace0\uc790 \ud558\ub294 DBMS\ub97c \uc704\ud55c jdbc \ub77c\uc774\ube0c\ub7ec\ub9ac\ub3c4 libs \ub514\ub809\ud1a0\ub9ac\uc5d0 \uc800\uc7a5\ud574 \ub450\uace0 \ucc38\uc870\ud569\ub2c8\ub2e4. \uc800 \uac19\uc740 \uacbd\uc6b0 PostgreSQL\ub97c \uc0ac\uc6a9\ud558\ubbc0\ub85c postgresql-42.2.2.jar\ub97c \uc0ac\uc6a9\ud558\uc600\uc2b5\ub2c8\ub2e4.<\/p>\n<p>\uc704\uc758 \ucf54\ub4dc\ub97c \ubcf4\uba74 SqlMapClient\ub77c\ub294 \ud074\ub798\uc2a4\uac00 \ubcf4\uc785\ub2c8\ub2e4. \uc774 \ud074\ub798\uc2a4\uc5d0 \ub300\ud55c \uc18c\uc2a4\ub294 SqlMapClient.java\uc774\uba70 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\npackage tstMyBatis;\r\n\r\nimport java.io.IOException;\r\nimport java.io.Reader;\r\n\r\nimport org.apache.ibatis.io.Resources;\r\nimport org.apache.ibatis.session.SqlSession;\r\nimport org.apache.ibatis.session.SqlSessionFactory;\r\nimport org.apache.ibatis.session.SqlSessionFactoryBuilder;\r\n\r\npublic class SqlMapClient {\r\n    private static SqlSession _session = null;\r\n\t\r\n   static {\r\n        try {\r\n            String resource = \"config\/myBatisConfig.xml\";\r\n            Reader reader = Resources.getResourceAsReader(resource);\r\n            SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);\r\n\t\t\t\r\n            _session = sqlMapper.openSession();\r\n         } catch(IOException e) {\r\n             e.printStackTrace();\r\n        }\r\n    }\r\n\t\r\n    public static SqlSession getSqlSession() {\r\n        return _session;\r\n    }\r\n}\r\n<\/pre>\n<p>\uc704\uc758 \ucf54\ub4dc\ub97c \ubcf4\uba74 mybatis\uc758 \uc124\uc815\ud30c\uc77c\ub85c myBatisConfig.xml\uc744 \uc0ac\uc6a9\ud558\uace0 \uc788\ub294\ub370\uc694. \uc774 \ud30c\uc77c\uc758 \ub0b4\uc6a9\uc740 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<!DOCTYPE configuration\r\n  PUBLIC \"-\/\/mybatis.org\/\/DTD Config 3.0\/\/EN\"\r\n  \"http:\/\/mybatis.org\/dtd\/mybatis-3-config.dtd\">\r\n\r\n<configuration>\r\n    <properties resource=\"config\/db.properties\" \/>\r\n    \r\n    <environments default=\"development\">\r\n        <environment id=\"development\">\r\n            <transactionManager type=\"JDBC\" \/>\r\n            <dataSource type=\"POOLED\">\r\n            \t<property name=\"driver\" value=\"${driver}\" \/>\r\n                <property name=\"url\" value=\"${url}\" \/>\r\n                <property name=\"username\" value=\"${username}\" \/>\r\n                <property name=\"password\" value=\"${password}\" \/>\r\n            <\/dataSource>\r\n        <\/environment>\r\n    <\/environments>\r\n\r\n    <mappers>\r\n        <mapper resource=\"config\/sql.xml\" \/>\r\n    <\/mappers>\r\n<\/configuration>\r\n<\/pre>\n<p>\uc77c\ub2e8 \uc774\uc815\ub3c4\ucbe4\uc5d0\uc11c \ud504\ub85c\uc81d\ud2b8\uc758 \uad6c\uc131\uc744 \uc0b4\ud3b4\ubcf4\uba74 \uc544\ub798\uc640 \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2018\/04\/java_mybatis.png\" alt=\"\" width=\"271\" class=\"aligncenter size-full wp-image-5169\" \/><\/p>\n<p>\ub2e4\uc2dc myBatisConfig.xml\uc758 \ub0b4\uc6a9\uc744 \ubcf4\uba74 \uc18d\uc131\uc815\ubcf4\ub85c\uc368 db.properties \ud30c\uc77c\uacfc \ub9f5\ud37c\uc815\ubcf4\ub85c\uc368 sql.xml \ud30c\uc77c\uc5d0 \ub300\ud55c \ucc38\uc870\uac00 \ubcf4\uc785\ub2c8\ub2e4. db.properties \ud30c\uc77c\uc740 DB\uc5d0 \ub300\ud55c \uc5f0\uacb0\uc5d0 \ud544\uc694\ud55c \uc815\ubcf4\uac00 \uc800\uc7a5\ub418\uc5b4 \uc788\uc73c\uba70 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<\/p>\n<p><code>driver=org.postgresql.Driver<br \/>\nurl=jdbc:postgresql:\/\/localhost:5432\/postgres<br \/>\nusername=postgres<br \/>\npassword=#HappyDay#<\/code><\/p>\n<p>\uc55e\uc11c \uc5b8\uae09\ud588\ub358 \uac83\ucc98\ub7fc PostgreSQL\uc744 \uc0ac\uc6a9\ud55c\ub2e4\uace0 \ud588\uc73c\ubbc0\ub85c \uc774\uc5d0 \ub300\ud574 \ud544\uc694\ud55c \uac12\ub4e4\uc774 \ub2f4\uaca8 \uc788\ub294 \uac83\uc744 \ubcfc \uc218 \uc788\uc2b5\ub2c8\ub2e4. \ub2e4\uc74c\uc740 SQL\uc5d0 \ub300\ud55c \ub9f5\ud551 \uc815\ubcf4\uac00 \ub2f4\uaca8 \uc788\ub294 sql.xml \ud30c\uc77c\uc5d0 \ub300\ud55c \ub0b4\uc6a9\uc785\ub2c8\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n\r\n&lt;!DOCTYPE mapper PUBLIC \"-\/\/mybatis.org\/\/DTD Mapper 3.0\/\/EN\" \r\n  \"http:\/\/mybatis.org\/dtd\/mybatis-3-mapper.dtd\">\r\n\r\n<mapper namespace=\"test\">\r\n\t<select id=\"getFirstPlayer\" resultType=\"String\">\r\n\t\tSELECT playername FROM players LIMIT 1\r\n\t<\/select>\r\n<\/mapper>\r\n<\/pre>\n<p>EntryMain \ud074\ub798\uc2a4\uc758 8\ubc88 \ucf54\ub4dc\uc5d0\uc11c \uc2e4\ud589\ud560 SQL\uc744 test.getFirstPlayer\ub85c \ucc38\uc870\ud558\uace0 \uc788\ub294\ub370\uc694. \ubc14\ub85c test\uac00 mapper\uc758 \ub124\uc784\uc2a4\ud398\uc774\uc2a4\uc774\uace0 getFirstPlayer\uac00 \uc2e4\ud589\ud560 SQL\uc5d0 \ub300\ud55c ID\uc785\ub2c8\ub2e4. \uc774\uc81c \uc2e4\ud589\ud558\uba74 \ud574\ub2f9 SQL\ubb38\uc774 \uc2e4\ud589\ub418\uc5b4 \ud654\uba74\uc5d0 \uadf8 \uacb0\uacfc\uac00 \ud45c\uc2dc\ub429\ub2c8\ub2e4.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mybatis API\ub97c \uc0ac\uc6a9\ud574 DB\ub97c \uc5f0\uacb0\ud558\uace0 SQL\ubb38\uc744 \ub358\uc838 \uadf8 \uacb0\uacfc\ub97c \uc5bb\uc5b4\uc624\ub294 \uc608\uc81c \ucf54\ub4dc\ub97c \uc815\ub9ac\ud569\ub2c8\ub2e4. \ud5a5\ud6c4 \ud55c\ub2e8\uacc4 \uc218\uc900 \ub192\uc740 \uc751\uc6a9\uc744 \uc704\ud574 \ucd5c\uc18c\ud55c\uc758 \ucf54\ub4dc\ub9cc\uc73c\ub85c mybatis\ub97c \ud65c\uc6a9\ud558\ub294 \ud504\ub85c\uc81d\ud2b8\uac00 \uac16\ucdb0\uc57c\ud560 \ucd5c\uc18c\ud55c\uc758 \uc124\uc815 \ubc0f \ucf54\ub4dc\ub9cc\uc744 \uc5b8\uae09\ud569\ub2c8\ub2e4. \uc774\ud074\ub9bd\uc2a4\ub97c \ud1b5\ud574 \uc2e0\uaddc \ud504\ub85c\uc81d\ud2b8\ub97c \uc0dd\uc131\ud558\uace0, \uc774\ub54c \uc0ac\uc6a9\ud55c \ud504\ub85c\uc81d\ud2b8 \uba85\uc740 tstMyBatis\ub85c \uc9c0\uc815\ud558\uc600\uc2b5\ub2c8\ub2e4. main \ud568\uc218\ub294 EntryMain \ud074\ub798\uc2a4\uc5d0 \ub2f4\uc558\uc2b5\ub2c8\ub2e4. EntryMain.java\uc5d0 \ub300\ud55c \ub0b4\uc6a9\uc740 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4. package tstMyBatis; import org.apache.ibatis.session.SqlSession; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=5168\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;mybatis \ub77c\uc774\ube0c\ub7ec\ub9ac \uc0ac\uc6a9\ud558\uae30&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[],"class_list":["post-5168","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/5168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5168"}],"version-history":[{"count":6,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/5168\/revisions"}],"predecessor-version":[{"id":9613,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/5168\/revisions\/9613"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5168"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}