
{"id":2608,"date":"2020-12-07T09:55:31","date_gmt":"2020-12-07T01:55:31","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2608"},"modified":"2020-12-07T12:41:54","modified_gmt":"2020-12-07T04:41:54","slug":"2020java%e6%a1%86%e6%9e%b6%e6%95%99%e7%a8%8b%ef%bc%9ajdbctemplate%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2608","title":{"rendered":"2020java\u6846\u67b6\u6559\u7a0b\uff1ajdbcTemplate\u7684\u4f7f\u7528"},"content":{"rendered":"<h3>2020java\u6846\u67b6\u6559\u7a0b\uff1ajdbcTemplate\u7684\u4f7f\u7528<\/h3>\n<p>1.\u521b\u5efa\u9879\u76ee<\/p>\n<p>2.\u5bfc\u5305<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-2254\" src=\"\/wp-content\/uploads\/2020\/12\/06\/5.png\" alt=\"\" width=\"680\" \/><\/p>\n<p>3.\u65e5\u5fd7\u6587\u4ef6<\/p>\n<p>4.\u521b\u5efa\u6570\u636e\u5e93\u8868<\/p>\n<p>5.\u521b\u5efa\u5de5\u5177\u7c7b\u5e76\u8fde\u63a5\u6570\u636e\u5e93<\/p>\n<p>6.\u521b\u5efa\u5b9e\u4f53\u7c7b\u3001dao\u3001service\u4ee5\u53ca\u5b9e\u73b0\u7c7b<\/p>\n<p>\u4f7f\u7528JdbcTemplate<\/p>\n<p><span style=\"color: #ff0000;\">\u67e5\u8be2\u6240\u6709\uff1a<\/span><\/p>\n<pre>public List&lt;Role&gt; getAllList() {\r\n    try{\r\n        return jt.query(\"select*from role\", new RowMapper&lt;Role&gt;() {\r\n            @Override\r\n            public Role mapRow(ResultSet rs, int i) throws SQLException {\r\n                return roleMapper(rs);\r\n            }\r\n        });\r\n    }catch (Exception e){}\r\n    return null;\r\n}<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>\u67e5\u8be2\u5355\u4e2a<\/strong><\/span><\/p>\n<pre>public Role getID(int id) {\r\n    try{\r\n        return jt.queryForObject(\"select*from role where id=?\", new RowMapper&lt;Role&gt;() {\r\n            @Override\r\n            public Role mapRow(ResultSet rs, int i) throws SQLException {\r\n                return roleMapper(rs);\r\n            }\r\n        },id);\r\n    }catch (Exception e){}\r\n    return null;\r\n}<\/pre>\n<p><strong>\u5c01\u88c5Mapper\u65b9\u6cd5<\/strong><\/p>\n<pre>public Role roleMapper(ResultSet rs){\r\n    Role r=new Role();\r\n    try {\r\n        r.setId(rs.getInt(\"id\"));\r\n        r.setNames(rs.getString(\"names\"));\r\n        r.setNames2(rs.getString(\"names2\"));\r\n        return r;\r\n    } catch (Exception e) {}\r\n    return null;\r\n}<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>\u4f7f\u7528spring\u7ba1\u7406\u5bf9\u8c61\u548c\u5bf9\u8c61\u95f4\u7684\u4f9d\u8d56<\/strong><\/span><\/p>\n<pre>&lt;context:property-placeholder location=\"classpath:database.properties\"\/&gt;\r\n&lt;!-- \u8fde\u63a5\u6c60 --&gt;\r\n&lt;bean name=\"dataSource\" class=\"com.mchange.v2.c3p0.ComboPooledDataSource\"&gt;\r\n    &lt;property name=\"driverClass\" value=\"${driver}\"\/&gt;\r\n    &lt;property name=\"jdbcUrl\" value=\"${url}\"\/&gt;\r\n    &lt;property name=\"user\" value=\"${username}\"\/&gt;\r\n    &lt;property name=\"password\" value=\"${password}\"\/&gt;\r\n&lt;\/bean&gt;\r\n&lt;!-- JDBCTemplate  --&gt;\r\n&lt;bean name=\"jdbcTemplate\" class=\"org.springframework.jdbc.core.JdbcTemplate\"&gt;\r\n    &lt;property name=\"dataSource\" ref=\"dataSource\"\/&gt;\r\n&lt;\/bean&gt;\r\n&lt;!-- roleDao --&gt;\r\n&lt;bean name=\"roleDao\" class=\"dao.impl.RoleDaoImpl\"&gt;\r\n    &lt;property name=\"jt\" ref=\"jdbcTemplate\"\/&gt;\r\n&lt;\/bean&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>2020java\u6846\u67b6\u6559\u7a0b\uff1ajdbcTemplate\u7684\u4f7f\u7528 1.\u521b\u5efa\u9879\u76ee 2.\u5bfc\u5305 3.\u65e5\u5fd7\u6587\u4ef6 4.\u521b\u5efa\u6570\u636e\u5e93 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2608","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2608"}],"version-history":[{"count":4,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2608\/revisions"}],"predecessor-version":[{"id":2613,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2608\/revisions\/2613"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}