{"id":9,"date":"2013-03-27T15:08:02","date_gmt":"2013-03-27T15:08:02","guid":{"rendered":"http:\/\/www.yeetrack.com\/post\/2013-03-27\/spring%20DAO%E6%A8%A1%E5%9D%97%E6%94%AF%E6%8C%81jdbc"},"modified":"2013-09-27T22:36:45","modified_gmt":"2013-09-27T14:36:45","slug":"springdao%e6%a8%a1%e5%9d%97%e6%94%af%e6%8c%81jdbc","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=9","title":{"rendered":"spring DAO\u6a21\u5757\u652f\u6301jdbc"},"content":{"rendered":"<p>jdbc\u7f16\u7a0b\u4e2d\uff0c\u9700\u8981\u624b\u52a8\u83b7\u53d6Connection\u3001Statement\u3001PreparedStatement\u3001ResultSet\u3001SQLException\uff0c\u4f7f\u7528\u5b8c\u6bd5\u540e\u8fd8\u9700\u8981\u624b\u52a8\u91ca\u653e\u8fde\u63a5\u3002<br \/> spring\u6846\u67b6\u5bf9jdbc\u8fdb\u884c\u4e86\u5c01\u88c5\uff0c\u4f7f\u7528\u81ea\u5df1\u7684\u5c01\u88c5\u7684JdbcTemplate\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u4e0b\u9762\u662f\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50<\/p>\n<p><!--more--><\/p>\n<p>\u3002<br \/> <strong>Person.java<\/strong>\u5b9a\u4e49Person\u7c7b\uff0c\u4ee3\u7801\uff1a<\/p>\n<pre config=\"brush:java;toolbar:false;\">\npackage com.yeetrack.springDAO;\n\nimport java.util.Date;\n\npublic class Person\n{\n\n    private int id;\n    private String name;\n    private String sex;\n    private int age;\n    private Date birthday;\n    public int getId()\n    {\n        return id;\n    }\n    public void setId(int id)\n    {\n        this.id = id;\n    }\n    public String getName()\n    {\n        return name;\n    }\n    public void setName(String name)\n    {\n        this.name = name;\n    }\n    public String getSex()\n    {\n        return sex;\n    }\n    public void setSex(String sex)\n    {\n        this.sex = sex;\n    }\n    public int getAge()\n    {\n        return age;\n    }\n    public void setAge(int age)\n    {\n        this.age = age;\n    }\n    public Date getBirthday()\n    {\n        return birthday;\n    }\n    public void setBirthday(Date birthday)\n    {\n        this.birthday = birthday;\n    }\n\n}\n<\/pre>\n<p><strong>IPersonDAO.java<\/strong> DAO\u5c42\u63a5\u53e3\uff0c\u5b9a\u4e49Person\u4e2d\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre config=\"brush:java;toolbar:false;\">\npackage com.yeetrack.springDAO;\n\nimport java.util.List;\n\n\/**\n * person\u7c7b\u64cd\u4f5c\u63a5\u53e3\n * @author youthflies\n *\n *\/\npublic interface IPersonDAO\n{\n\n    public String getPersonName(int id);   \/\/\u6839\u636eid\u83b7\u53d6\u59d3\u540d\n    public void addPerson(Person person);  \/\/\u6dfb\u52a0person\n    public int getPersonCount();    \/\/\u83b7\u53d6person\u6570\u76ee\n    public List&lt;Person&gt; listPerson();  \/\/\u8fd4\u56de\u6240\u6709person\n}\n<\/pre>\n<p><strong>PersonDaoImpl.java<\/strong> PersonDaoImpl\u7c7b\u5b9e\u73b0\u4e86IPersonDAO\u63a5\u53e3\uff0c\u800c\u4e14\u7ee7\u627f\u4e86Spring DAO\u6a21\u5757\u4e2d\u7684JdbcDaoSupport\u7c7b\u3002JdbcDaoSupport\u63d0\u4f9bJdbcTemplate\u5bf9\u8c61\uff0c\u5c01\u88c5\u4e86\u5e38\u7528\u7684jdbc\u64cd\u4f5c\uff0c\u6211\u4eec\u4f7f\u7528JdbcTemplate\u5373\u53ef\u3002<\/p>\n<pre config=\"brush:java;toolbar:false;\">\npackage com.yeetrack.springDAO;\n\nimport java.util.ArrayList;\nimport java.util.Date;\nimport java.util.List;\nimport java.util.Map;\n\nimport org.springframework.jdbc.core.support.JdbcDaoSupport;\n\npublic class PersonDaoImpl extends JdbcDaoSupport implements IPersonDAO\n{\n\n    \/**\n     * \u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u521b\u5efa\u6570\u636e\u5e93\u8868\u7ed3\u6784\n     *\/\n    public void initDatabase()\n    {\n        String sql = &quot;create table if not exists tb_person &quot;\n                + &quot;(id int auto_increment, &quot; + &quot;name varchar(255), &quot;\n                + &quot;sex varchar(10), age int , birthday timestamp, primary key(id) )&quot;;\n\n        getJdbcTemplate().execute(sql);\n    }\n\n    \/**\n     * \u6839\u636eid\u83b7\u53d6person\u540d\u5b57\n     *\/\n    public String getPersonName(int id)\n    {\n        \/\/ TODO Auto-generated method stub\n        String sql = &quot;select name from tb_person where id = &quot; + id;\n        return (String) getJdbcTemplate().queryForObject(sql, String.class); \n    }\n\n    \/**\n     * \u5411\u6570\u636e\u5e93\u4e2d\u6dfb\u52a0\u8bb0\u5f55\n     *\/\n    public void addPerson(Person person)\n    {\n        \/\/ TODO Auto-generated method stub\n        String sql = &quot;insert into tb_person (name, sex, age, birthday ) values (?, ?, ?, ?)&quot;;\n\n        getJdbcTemplate().update(sql, new Object[] { person.getName(), person.getSex(), person.getAge(), person.getBirthday()});\n\n    }\n\n    \/**\n     * \u83b7\u53d6\u8bb0\u5f55\u603b\u6761\u6570\n     *\/\n    public int getPersonCount()\n    {\n        \/\/ TODO Auto-generated method stub\n        String sql = &quot;select count(*) from tb_person&quot;;\n        return getJdbcTemplate().queryForInt(sql);\n    }\n\n    \/**\n     * \u8fd4\u56de\u6240\u6709\u7684Person\n     * @return \u8fd4\u56dePerson List\n     *\/\n    public List&lt;Person&gt; listPerson()\n    {\n        \/\/ TODO Auto-generated method stub\n        String sql = &quot;select id, name, sex, age, birthday from tb_person&quot;;\n        @SuppressWarnings(&quot;unchecked&quot;)\n        List&lt;Map&lt;String, Object&gt;&gt; list =getJdbcTemplate().queryForList(sql);\n        List&lt;Person&gt; personList = new ArrayList&lt;Person&gt;();\n\n        for(Map&lt;String, Object&gt; row: list)\n        {\n            Person person = new Person();\n            person.setId((Integer)row.get(&quot;id&quot;));\n            person.setName((String)row.get(&quot;name&quot;));\n            person.setSex((String)row.get(&quot;sex&quot;));\n            person.setAge((Integer)row.get(&quot;age&quot;));\n            person.setBirthday((Date)row.get(&quot;birthday&quot;));\n\n            personList.add(person);\n        }\n\n        return personList;\n    }\n\n}\n<\/pre>\n<p>applicationContext.xml\u914d\u7f6e\uff0c\u6211\u4eec\u9700\u8981\u914d\u7f6e\u4e00\u4e2a\u6570\u636e\u6e90\uff0c\u5e76\u628a\u8be5\u6570\u636e\u6e90\u8bbe\u7f6e\u5230PersonDaoImpl\u4e2d\uff0cPersonDaoImpl\u4e2d\u6709\u4e2a\u65b9\u6cd5initDataBase()\uff0c\u7528\u6765\u751f\u6210\u8868\u7ed3\u6784\uff0c\u9700\u8981\u914d\u7f6e\u5230applicationContext.xml\u4e2d\uff1a<\/p>\n<pre config=\"brush:html;toolbar:false;\">\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n    xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xmlns:p=&quot;http:\/\/www.springframework.org\/schema\/p&quot;\n    xmlns:context=&quot;http:\/\/www.springframework.org\/schema\/context&quot;\n    xmlns:aop=&quot;http:\/\/www.springframework.org\/schema\/aop&quot;\n    xsi:schemaLocation=&quot;     \n          http:\/\/www.springframework.org\/schema\/beans     \n          http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.0.xsd     \n          http:\/\/www.springframework.org\/schema\/context     \n          http:\/\/www.springframework.org\/schema\/context\/spring-context-3.0.xsd \n          http:\/\/www.springframework.org\/schema\/aop     \n          http:\/\/www.springframework.org\/schema\/aop\/spring-aop-3.0.xsd&quot;\n    default-autowire=&quot;byName&quot;&gt;\n\n    &lt;bean id=&quot;dataSource&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot; destroy-method=&quot;close&quot;&gt;\n        &lt;property name=&quot;driverClassName&quot; value=&quot;${jdbc.driverClassName}&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;url&quot; value=&quot;${jdbc.url}&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;username&quot; value=&quot;${jdbc.username}&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;password&quot; value=&quot;${jdbc.password}&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n    &lt;bean id=&quot;propertyConfigurer&quot; class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;\n        &lt;property name=&quot;location&quot; value=&quot;classpath:jdbc.properties&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n\n    &lt;bean id=&quot;personDao&quot; class=&quot;com.yeetrack.springDAO.PersonDaoImpl&quot; depends-on=&quot;dataSource&quot; init-method=&quot;initDatabase&quot;&gt;\n        &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n\n&lt;\/beans&gt;\n<\/pre>\n<p>\u6570\u636e\u5e93\u914d\u7f6e\u6587\u4ef6\uff0cjdbc.properties<\/p>\n<pre config=\"brush:html;toolbar:false;\">\n#\u914d\u7f6e\u6570\u636e\u9a71\u52a8\njdbc.driverClassName=com.mysql.jdbc.Driver\njdbc.url=jdbc:mysql:\/\/127.0.0.1:3306\/test?characterEncoding=UTF-8\njdbc.username=root\njdbc.password=toor\n<\/pre>\n<p>\u4e0b\u9762\u662f\u6d4b\u8bd5\u4ee3\u7801(\u9700\u8981\u6709\u4e2atest\u6570\u636e\u5e93)\uff0cTestDAO.java<\/p>\n<pre config=\"brush:java;toolbar:false;\">\npackage com.yeetrack.springDAO;\n\nimport java.util.Date;\n\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\npublic class TestDao\n{\n\n    \/**\n     * @param args\n     *\/\n    public static void main(String[] args)\n    {\n        \/\/ TODO Auto-generated method stub\n        ApplicationContext context = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);\n        IPersonDAO personDao = (IPersonDAO) context.getBean(&quot;personDao&quot;);\n\n        Person person =new Person();\n        person.setName(&quot;youthflies&quot;);\n        person.setAge(22);\n        person.setSex(&quot;M&quot;);\n        person.setBirthday(new Date());\n        personDao.addPerson(person);\n\n        System.out.println(personDao.getPersonName(1));\n\n        System.out.println(personDao.getPersonCount());\n\n        System.out.println(personDao.listPerson());\n\n    }\n\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>jdbc\u7f16\u7a0b\u4e2d\uff0c\u9700\u8981\u624b\u52a8\u83b7\u53d6Connection\u3001Statement\u3001PreparedStatement\u3001ResultSet\u3001SQLException\uff0c\u4f7f\u7528\u5b8c\u6bd5\u540e\u8fd8\u9700\u8981\u624b\u52a8\u91ca\u653e\u8fde\u63a5\u3002 spring\u6846\u67b6&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[8,7,5],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-coding","tag-java","tag-7","tag-5"],"views":2783,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9"}],"version-history":[{"count":1,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":185,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/9\/revisions\/185"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}