Raible's Wiki

Raible Designs
Wiki Home
News
Recent Changes

AppFuse

Homepage
  - Korean
  - Chinese
  - Italian
  - Japanese

QuickStart Guide
  - Chinese
  - French
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish
  - Japanese

User Guide
  - Korean
  - Chinese

Tutorials
  - Chinese
  - German
  - Italian
  - Korean
  - Portuguese
  - Spanish

FAQ
  - Korean

Latest Downloads

Other Applications

Struts Resume
Security Example
Struts Menu

Set your name in
UserPreferences


Referenced by
Articles
Articles_cn
Articles_pt
Articles_zh
CreateManager_zh
LeftMenu
SandBox




JSPWiki v2.2.33

[RSS]


Hide Menu

CreateDAO_zh


Difference between version 23 and version 1:

At line 1 changed 1 line.
__Part I:__ [在AppFuse建立DAO和对象|CreateDAO_zh] - 一个建立对象(代表数据库的表)和把这些对象存储到数据库的Java类的教程。
__Part I:__ [在AppFuse建立DAO和POJO |CreateDAO_zh] - 一个建立对象(代表数据库的表)和把这些对象存储到数据库的Java类的教程。
At line 3 changed 2 lines.
!!About this tutorial
This tutorial will show you how to create a new table in the database, and how the create Java code to access this table.
!!关于本教程
本教程将向你展示如何在一个数据库里创建表,以及如何完成访问这些表的Java代码。
At line 6 changed 1 line.
We will create an object and then some more classes to persist (save/retrieve/delete) that object from the database. In Java speak, we call the object a Plain Old Java Object (a.k.a. a [POJO|http://forum.java.sun.com/thread.jsp?forum=92&thread=425300&tstart=0&trange=15]). This object basically represents a database table. The ''other classes'' will be:
我们将建立一个对象以及处理(保存/检索/删除)这些类到数据库的一些代码。用Java术语,我们叫它Plain Old Java Object(a.k.a. a [POJO|http://forum.java.sun.com/thread.jsp?forum=92&thread=425300&tstart=0&trange=15])。这个对象通常代表了数据库中的一个表,''其他的类''包括:
At line 8 changed 2 lines.
* A Data Access Object (a.k.a. a [DAO|http://java.sun.com/blueprints/patterns/DAO.html]), an [Interface|http://java.sun.com/docs/books/tutorial/java/concepts/interface.html] and a Hibernate Implementation
* A [JUnit|http://www.junit.org] class to test our DAO is working
* 一个数据访问对象Data Access Object (a.k.a. a [DAO|http://java.sun.com/blueprints/patterns/DAO.html]), 一个 [Interface|http://java.sun.com/docs/books/tutorial/java/concepts/interface.html]和一个Hibernate实现
* 一个 [JUnit|http://www.junit.org] 类来测试我们的DAO对象
At line 11 changed 1 line.
%%note __NOTE:__ If you're using MySQL and you want to use transactions (which you probably will) you have to use InnoDB tables. To do this, add the following to your mysql configuration file ({{/etc/my.cnf}} or {{c:\Windows\my.ini}}). The 2nd setting (for a UTF-8 character set) is needed for 4.1.7+.
%%note __NOTE:__ 如果你使用MySQL并且希望使用事务 (很有可能是这个情况),你需要使用InnoDB tables,为了做到这一点, 添加以下两句话到 ({{/etc/my.cnf}} 或者 {{c:\Windows\my.ini}})。 其中第二个设置 (设置使用UTF-8字符)是4.1.7+所必需的。
At line 17 changed 1 line.
If you're using PostgreSQL and you get confusing errors about batch processing, try turning it off by adding {{<prop key="hibernate.jdbc.batch_size">0</prop>}} to your {{src/dao/**/hibernate/applicationContext-hibernate.xml}} file.
如果你使用PostgreSQL并且在成批处理时得到许多迷惑的错误,试着把关闭它,方法是增加 {{<prop key="hibernate.jdbc.batch_size">0</prop>}} 到你的 {{src/dao/**/hibernate/applicationContext-hibernate.xml}}文件。
At line 20 changed 1 line.
AppFuse uses [Hibernate|http://www.hibernate.org] for its default persistence layer. Hibernate is an Object/Relational (O/R) Framework that allows you to relate your Java Objects to database tables. It allows you to very easily perform CRUD (Create, Retrieve, Update, Delete) on your objects.
AppFuse使用[Hibernate|http://www.hibernate.org] 作为持久化层, Hibernate是一套对象/关系Object/Relational (O/R)框架,他允许你把Java对象和数据库之间联系起来,它可以很方便的对你的对象执行CRUD (Create, Retrieve, Update, Delete)操作。
At line 22 changed 1 line.
;:''You can also use [iBATIS|http://ibatis.com] as a persistence framework option. To install iBATIS in AppFuse, view the README.txt in {{extras/ibatis}}. If you're using iBATIS over Hibernate, I expect you have your reasons and are familiar with the framework. I also expect that you can figure out how to adapt this tutorial to work with iBATIS. ;-)''
;:''你也可以选择使用[iBATIS|http://ibatis.com]作为持久化层,如果要在AppFuse里安装iBATIS, 请查看{{extras/ibatis}}中的README.txt。如果你选择iBATIS而不是Hibernate, 希望你有自己的原因并且熟悉这个框架,我也希望你能够领会到如何将教程应用到iBATIS ;-)''
At line 24 changed 4 lines.
!Font Conventions (work in progress)
;:Literal strings intended to be executed at the command prompt look like this: __ant test-all__.
;:References to files, directories and packages which exist in your source tree: <font size="2">{{build.xml}}</font>.
;:%%(color: blue)''And suggestions for how to do stuff in the "Real World" are in blue italics.''%%
!&#23383;&#20307;&#24815;&#20363; (&#36827;&#34892;&#20013;)
;:&#35201;&#22312;&#21629;&#20196;&#34892;&#19979;&#25191;&#34892;&#30340;&#21629;&#20196;&#26159;&#36825;&#20010;&#26679;&#23376;: __ant test-all__.
;:&#23545;&#30446;&#24405;&#25110;&#32773;&#21253;&#20013;&#30340;&#25991;&#20214;&#30340;&#24341;&#29992;&#26159;&#36825;&#20010;&#26679;&#23376;: <font size="2">{{build.xml}}</font>.
;:%%(color: blue)''&#25105;&#22312;“&#30495;&#23454;&#19990;&#30028;”&#20013;&#23454;&#38469;&#25805;&#20316;&#30340;&#26041;&#24335;&#29992;&#34013;&#33394;&#26012;&#20307;&#34920;&#31034;&#12290;''%%
At line 29 changed 1 line.
Let's get started on creating a new Object, DAO and Test in AppFuse's project structure.
&#35753;&#25105;&#20204;&#32487;&#32493;&#22312;AppFuse&#39033;&#30446;&#30340;&#32467;&#26500;&#19979;&#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#23545;&#35937;&#12289;DAO&#21644;&#27979;&#35797;&#12290;
At line 31 changed 7 lines.
!Table of Contents
* [1] Create a new Object and add [XDoclet|http://xdoclet.sf.net] tags
* [2] Create a new database table from the object using Ant
* [3] Create a new DaoTest to run JUnit tests on the DAO
* [4] Create a new DAO to perform CRUD on the object
* [5] Configure Spring for the Person object and PersonDao
* [6] Run the DaoTest
!&#30446;&#24405;
* [1] &#24314;&#31435;&#19968;&#20010;&#23545;&#35937;&#65292;&#24182;&#19988;&#20316;[XDoclet|http://xdoclet.sf.net]&#26631;&#35760;
* [2] &#20351;&#29992;Ant&#26681;&#25454;&#23545;&#35937;&#24314;&#31435;&#25968;&#25454;&#24211;&#20013;&#30340;&#34920;
* [3] &#21019;&#24314;&#19968;&#20010;DaoTest&#26469;&#36816;&#34892;DAO&#23545;&#35937;&#30340;JUnit&#27979;&#35797;
* [4] &#21019;&#24314;&#19968;&#20010;&#26032;&#30340;DAO&#26469;&#25191;&#34892;&#20851;&#20110;&#36825;&#20010;&#23545;&#35937;&#30340;CRUD&#25805;&#20316;
* [5] &#22312;spring&#37324;&#37197;&#32622;Person&#21644;PersonDao
* [6] &#36816;&#34892;DaoTest
At line 39 changed 1 line.
!!Create a new Object and add XDoclet tags [#1]
!!&#24314;&#31435;&#19968;&#20010;&#23545;&#35937;&#65292;&#24182;&#19988;&#20316;XDoclet&#26631;&#35760; [#1]
At line 41 changed 1 line.
The first thing we need to do is create an object to persist. Let's create a simple "Person" object (in the {{src/dao/**/model}} directory) that has an id, a firstName and a lastName (as properties).
&#25105;&#20204;&#35201;&#20570;&#30340;&#31532;&#19968;&#20214;&#20107;&#24773;&#23601;&#26159;&#24314;&#31435;&#19968;&#20010;&#38656;&#35201;&#25345;&#20037;&#21270;&#30340;&#23545;&#35937;&#65292;&#25105;&#20204;&#35201;&#22312;{{src/dao/**/model}}&#30446;&#24405;&#19979;&#24314;&#31435;&#19968;&#20010;&#31616;&#21333;&#30340;Person&#23545;&#35937;&#65292;&#36825;&#20010;&#23545;&#35937;&#21253;&#25324;id&#12289;firstName&#21644;lastName&#23646;&#24615;&#12290;
At line 43 changed 1 line.
%%note __NOTE:__ Copying the Java code in these tutorials [doesn't work in Firefox|http://raibledesigns.com/page/rd?anchor=java2html_plugin_for_jspwiki_causes]. A workaround is to CTRL+Click (Command+Click on OS X) the code block and then copy it.%%
%%note __&#27880;&#24847;&#65306;__ [不能在FireFox中|http://raibledesigns.com/page/rd?anchor=java2html_plugin_for_jspwiki_causes]&#30452;&#25509;&#25335;&#36125;&#26412;&#25945;&#31243;&#30340;&#20195;&#30721; &#65292;&#20294;&#25105;&#20204;&#21487;&#20197;&#36890;&#36807;CTRL+Click&#36873;&#23450;&#19968;&#20010;&#20195;&#30721;&#25152;&#22312;&#30340;&#24037;&#20316;&#21306;(OS X&#19979;&#26159;Command+Click)&#65292;&#28982;&#21518;&#20877;&#25335;&#36125;&#12290;%%
At line 62 changed 1 line.
This class should extend [BaseObject|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/model/BaseObject.java.html], which has 3 abstract methods: (equals(), hashCode() and toString()) that you will need to implement in the Person class. The first two are required by Hibernate. The easiest way to do this is using [Commonclipse|http://commonclipse.sf.net]. More information on using this tool can be found on [Lee Grey's site|http://www.leegrey.com/hmm/2004/09/29/1096491256000.html]. Another Eclipse Plugin you can use is [Commons4E|http://commons4e.berlios.de/]. I haven't used it, so I can't comment on its functionality.
&#36825;&#20010;&#31867;&#24517;&#39035;&#25193;&#23637;[BaseObject|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/model/BaseObject.java.html]&#65292;&#32780;&#36825;&#20010;BaseObject&#26377;&#19977;&#20010;&#25277;&#35937;&#26041;&#27861;(equals(), hashCode()&#21644;toString())&#38656;&#35201;&#20320;&#22312;Person&#31867;&#37324;&#23454;&#29616;&#65292;&#21069;&#20004;&#20010;&#26159;Hibernate&#30340;&#38656;&#35201;&#12290;&#20026;&#20102;&#23436;&#25104;&#36825;&#37096;&#20998;&#24037;&#20316;&#26368;&#31616;&#21333;&#30340;&#26041;&#24335;&#26159;&#20351;&#29992;[Commonclipse|http://commonclipse.sf.net]&#65292;&#20851;&#20110;&#36825;&#20010;&#24037;&#20855;&#26356;&#22810;&#30340;&#20449;&#24687;&#21487;&#20197;&#22312;[Lee Grey的站点|http://www.leegrey.com/hmm/2004/09/29/1096491256000.html]&#37324;&#30475;&#21040;&#65292;&#21478;&#22806;&#19968;&#20010;&#20320;&#21487;&#20197;&#20351;&#29992;&#30340;Eclipse&#30340;&#25554;&#20214;&#26159;[Commons4E|http://commons4e.berlios.de/]&#65292;&#25105;&#36824;&#27809;&#26377;&#20351;&#29992;&#36807;&#65292;&#36825;&#37324;&#19981;&#20415;&#23545;&#20854;&#21151;&#33021;&#20316;&#20986;&#35780;&#35770;&#12290;
At line 64 changed 1 line.
;:''If you're using [IntelliJ IDEA|http://www.jetbrains.com/idea], you can generate equals() and hashCode(), but not toString(). There is a [ToStringPlugin|http://www.intellij.org/twiki/bin/view/Main/ToStringPlugin] that works reasonably well.''
;:''&#22914;&#26524;&#20320;&#20351;&#29992;[IntelliJ IDEA|http://www.jetbrains.com/idea]&#65292;&#20320;&#21487;&#20197;&#33258;&#21160;&#20135;&#29983;equals()&#21644;hashCode()&#65292;&#20294;&#27809;&#26377;toString()&#65292;&#26377;&#19968;&#20010; [ToStringPlugin|http://www.intellij.org/twiki/bin/view/Main/ToStringPlugin]&#25554;&#20214;&#20570;&#24471;&#38750;&#24120;&#19981;&#38169;''
At line 66 changed 1 line.
Now that we have this POJO created, we need to add XDoclet tags to generate the Hibernate mapping file. This mapping file is used by Hibernate to map objects &rarr; tables and properties (variables) &rarr; columns.
&#29616;&#22312;&#25105;&#20204;&#24050;&#32463;&#21019;&#24314;&#20102;&#36825;&#20010;POJO&#23545;&#35937;&#65292;&#25105;&#20204;&#38656;&#35201;&#22686;&#21152;XDoclet&#26631;&#35760;&#26469;&#20135;&#29983;Hibernate&#30340;&#26144;&#23556;&#25991;&#20214;&#65292;&#36825;&#20123;&#25991;&#20214;&#29992;&#26469;&#26144;&#23556;&#23545;&#35937;&rarr; &#34920;&#21644;&#23646;&#24615;(&#21464;&#37327;) &rarr; &#23383;&#27573;&#12290;
At line 68 changed 1 line.
First of all, we add a [@hibernate.class|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.class%20(0..1)] tag that tells Hibernate what table this object relates to:
&#39318;&#20808;&#65292;&#25105;&#20204;&#22686;&#21152;[@hibernate.class|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.class%20(0..1)] &#26469;&#21578;&#35785;Hibernate&#25105;&#20204;&#23558;&#35201;&#21644;&#37027;&#20010;&#34920;&#20316;&#20851;&#32852;&#65306;
At line 78 changed 1 line.
We also have to add a primary key mapping or XDoclet will puke when generating the mapping file. Note that all @hibernate.* tags should be placed in the __getters'__ Javadocs of your POJOs.
&#25105;&#20204;&#20063;&#35201;&#22686;&#21152;&#20027;&#38190;&#30340;&#26144;&#23556;,&#21542;&#21017;XDoclet&#20250;&#22312;&#20135;&#29983;&#26144;&#23556;&#25991;&#20214;&#26102;&#20986;&#38169;,&#27880;&#24847;&#25152;&#26377;&#30340;@hibernate.*&#26631;&#31614;&#24517;&#39035;&#22312;__getters'__&#30340;Javadocs&#37324;&#38754;&#12290;
At line 93 changed 1 line.
;:%%(color: blue)''I'm using {{generator-class="increment"}} instead of {{generate-class="native"}} because I [found some issues|AppFuseOnDB2] when using "native" on other databases. If you only plan on using MySQL, I __recommend you use the "native" value__. This tutorial uses increment.''%%
;:%%(color: blue)''&#25105;&#20351;&#29992;{{generator-class="increment"}}&#32780;&#19981;&#20351;&#29992;{{generate-class="native"}} &#26159;&#22240;&#20026;&#25105;&#23545;&#25968;&#25454;&#24211;&#20351;&#29992;"native"&#26102;[发现一些问题|AppFuseOnDB2]&#65292;&#22914;&#26524;&#20320;&#21482;&#26159;&#24076;&#26395;&#20351;&#29992;MySQL&#65292;__&#25512;&#33616;&#20351;&#29992;"native"&#20540;__&#65292;&#26412;&#25945;&#31243;&#20351;&#29992;increment&#12290;''%%
At line 95 changed 2 lines.
!!Create a new database table from the object using Ant [#2]
At this point, you can create the person table by running __ant setup-db__. This task creates the {{Person.hbm.xml}} file and creates a database table called "person". From the ant console, you can see the table schema the Hibernate creates for you:
!!&#20351;&#29992;Ant&#26681;&#25454;&#23545;&#35937;&#20135;&#29983;&#25968;&#25454;&#24211;&#34920;[#2]
&#22312;&#36825;&#31181;&#24773;&#20917;&#19979;,&#20320;&#21487;&#20197;&#36890;&#36807;&#36816;&#34892;__ant setup-db__&#26469;&#24314;&#31435;person&#34920;&#65292;&#36825;&#20010;&#20219;&#21153;&#20250;&#20135;&#29983;&#25991;&#20214;{{Person.hbm.xml}}&#24182;&#19988;&#20250;&#24314;&#31435;&#21483;&#20570;"person"&#30340;&#34920;&#65292;&#20174;Ant&#30340;&#25511;&#21046;&#21488;&#31383;&#21475;&#65292;&#20320;&#21487;&#20197;&#30475;&#21040;Hibernate&#20026;&#20320;&#24314;&#31435;&#30340;&#34920;&#32467;&#26500;&#30340;&#20869;&#23481;&#12290;
At line 104 changed 1 line.
If you want to look at the {{Person.hbm.xml}} file that Hibernate generates for you, look in the {{build/dao/gen/**/model}} directory. Here's the contents of {{Person.hbm.xml}} (so far):
&#22914;&#26524;&#20320;&#26597;&#30475;Hibernate&#29983;&#25104;&#30340;&#25991;&#20214;{{Person.hbm.xml}}&#65292;&#21487;&#20197;&#21040;{{build/dao/gen/**/model}}&#30446;&#24405;&#65292;&#36825;&#37324;&#26159;{{Person.hbm.xml}}&#30340;&#20869;&#23481;(&#30446;&#21069;&#30340;&#20869;&#23481;):
At line 143 changed 1 line.
Now we'll add additional [@hibernate.property|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.property%20(0..1)] tags for our other columns (first_name, last_name):
&#29616;&#22312;&#25105;&#20204;&#35201;&#20026;&#20854;&#23427;&#30340;&#23383;&#27573;(first_name, last_name)&#28155;&#21152;&#39069;&#22806;&#30340;[@hibernate.property|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.property%20(0..1)]&#26631;&#31614;&#65306;
At line 146 added 1 line.
At line 162 changed 1 line.
In this example, the only reason for adding the ''column'' attribute is because the column name is different from our property name. If they're the same, you don't need to specify the ''column'' attribute. See the [@hibernate.property|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.property%20(0..1)] reference for other attributes you can specify for this tag.
&#22312;&#36825;&#20010;&#20363;&#23376;&#37324;&#65292;&#28155;&#21152;''column''&#23646;&#24615;&#30340;&#21807;&#19968;&#21407;&#22240;&#26159;&#22240;&#20026;&#36825;&#20010;&#23383;&#27573;&#21517;&#19982;&#23427;&#30340;&#23646;&#24615;&#21517;&#19981;&#30456;&#21516;&#65292;&#22914;&#26524;&#20182;&#20204;&#30456;&#21516;&#65292;&#20320;&#27809;&#26377;&#24517;&#35201;&#26469;&#25351;&#23450;''column''&#23646;&#24615;&#65292;&#20851;&#20110;&#20854;&#23427;&#21487;&#20197;&#20351;&#29992;&#30340;&#26631;&#31614;&#35831;&#30475;[@hibernate.property|http://xdoclet.sourceforge.net/tags/hibernate-tags.html#@hibernate.property%20(0..1)]&#12290;
At line 164 changed 1 line.
Run __ant setup-db__ again to get the additional columns added to your table.
&#20877;&#27425;&#36816;&#34892;__ant setup-db__&#25226;&#26032;&#21152;&#30340;&#23646;&#24615;&#21152;&#21040;&#25968;&#25454;&#24211;&#34920;&#37324;&#12290;
At line 173 changed 1 line.
If you want to change the size of your columns, modify the ''length'' attribute in your @hibernate.property tag. If you want to make it a required field (NOT NULL), add not-null="true".
&#22914;&#26524;&#26399;&#26395;&#20462;&#25913;&#23383;&#27573;&#30340;&#38271;&#24230;&#65292;&#20462;&#25913;@hibernate.property&#26631;&#31614;&#30340;''length''&#23646;&#24615;&#65292;&#22914;&#26524;&#24076;&#26395;&#25226;&#23383;&#27573;&#25913;&#20026;&#24517;&#28155;&#23383;&#27573;(NOT NULL)&#65292;&#21487;&#20197;&#22686;&#21152;&#23646;&#24615;not-null="true"&#12290;
At line 175 changed 1 line.
!!Create a new DaoTest to run JUnit tests on your DAO [#3]
!!&#24314;&#31435;&#26032;&#30340;DaoTest&#26469;&#23545;&#20320;&#30340;DAO&#36816;&#34892;JUnit&#27979;&#35797;[#3]
At line 177 changed 1 line.
%%note <a name="appgen"></a>__NOTE:__ AppFuse versions 1.6.1+ contain include an [AppGen] tool that can be used to generate all the classes for the rest of these tutorials. However, it's best that you go through these tutorials before using this tool - then you'll know what code it's generating.%%
%%note <a name="appgen"></a>__&#27880;&#24847;:__&#20174;Appfuse&#29256;&#26412;1.6.1+&#24320;&#22987;&#21253;&#25324;&#20102;&#19968;&#20010;[AppGen]&#24037;&#20855;&#65292;&#21487;&#20197;&#29992;&#26469;&#29983;&#25104;&#26412;&#25945;&#31243;&#20313;&#19979;&#30340;&#25152;&#26377;&#30340;&#31867;&#30340;&#20195;&#30721;&#65292;&#19981;&#36807;&#65292;&#25105;&#20204;&#26368;&#22909;&#36824;&#26159;&#20808;&#36807;&#19968;&#36941;&#25945;&#31243;&#20877;&#20351;&#29992;&#36825;&#20010;&#24037;&#20855;&#20135;&#29983;&#20195;&#30721;&#12290;%%
At line 179 changed 1 line.
Now we'll create a DaoTest to test that our DAO works. "Wait a minute," you say, "we haven't created a DAO!" You are correct. However, I've found that [Test-Driven Development|http://www.artima.com/intv/testdriven.html] breeds higher quality software. For years, I thought __write your test before your class__ was hogwash. It just seemed stupid. Then I tried it and I found that it works great. The only reason I do all this test-driven stuff now is because I've found it rapidly speeds up the process of software development.
&#29616;&#22312;&#65292;&#25105;&#20204;&#35201;&#21019;&#24314;&#19968;&#20010;DaoTest&#26469;&#27979;&#35797;&#25105;&#20204;&#30340;DAO&#30340;&#24037;&#20316;&#65292;“&#31561;&#20250;&#20799;”&#65292;&#20320;&#35828;&#65292;“&#25105;&#20204;&#36824;&#19981;&#26366;&#21019;&#24314;DAO&#21602;&#65281;”&#65292;&#20320;&#35828;&#24471;&#23545;&#12290;&#26080;&#35770;&#22914;&#20309;&#65292;&#25105;&#21457;&#29616;[测试驱动开发|http://www.artima.com/intv/testdriven.html]&#22823;&#22823;&#30340;&#20419;&#36827;&#20102;&#36719;&#20214;&#36136;&#37327;&#65292;&#22312;&#35768;&#22810;&#24180;&#37324;&#25105;&#19968;&#30452;&#35748;&#20026;__&#22312;&#20889;&#20195;&#30721;&#20043;&#21069;&#20889;&#27979;&#35797;__&#26159;&#32993;&#35828;&#20843;&#36947;&#65292;&#36825;&#30475;&#36215;&#26469;&#24456;&#24858;&#34850;&#65292;&#20294;&#24403;&#25105;&#23581;&#35797;&#20043;&#21518;&#25105;&#35748;&#20026;&#36825;&#26679;&#38750;&#24120;&#22909;&#65292;&#29616;&#22312;&#25105;&#25353;&#29031;&#27979;&#35797;&#39537;&#21160;&#30340;&#26041;&#24335;&#24037;&#20316;&#23436;&#20840;&#22240;&#20026;&#25105;&#21457;&#29616;&#36825;&#26679;&#21487;&#20197;&#22823;&#22823;&#25552;&#39640;&#25105;&#36719;&#20214;&#24320;&#21457;&#30340;&#25928;&#29575;&#12290;
At line 181 changed 1 line.
To start, create a {{PersonDaoTest.java}} class in the {{test/dao/**/dao}} directory. This class should extend [BaseDaoTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDaoTestCase.java.html], a subclass of JUnit's [TestCase|http://junit.sourceforge.net/javadoc/junit/framework/TestCase.html] which already exists in this package. This parent class is used to load [Spring's|http://www.springframework.org] ApplicationContext (since Spring binds the layers together), and for (optionally) loading a .properties file (ResourceBundle) that has the same name as your {{*Test.class}}. In this example, if you put a {{PersonDaoTest.properties}} file in the same directory as {{PersonDaoTest.java}}, this file's properties will be available via an "rb" variable.
&#24320;&#22987;&#65292;&#25105;&#20204;&#22312;{{test/dao/**/dao}}&#30446;&#24405;&#19979;&#24314;&#31435;&#31867;{{PersonDaoTest.java}}&#65292;&#36825;&#20010;&#31867;&#24517;&#39035;&#25193;&#23637;[BaseDaoTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDaoTestCase.java.html]&#65292;&#32780;BaseDAOTestCase&#36825;&#20010;&#31867;&#26159;JUnit&#31867;[TestCase|http://junit.sourceforge.net/javadoc/junit/framework/TestCase.html]&#30340;&#23376;&#31867;&#65292;&#36825;&#20010;&#31867;&#29992;&#26469;&#21152;&#36733;[Spring|http://www.springframework.org]&#30340;ApplicationContext&#65288;&#22240;&#20026;Spring&#25226;&#21508;&#20010;&#23618;&#32465;&#23450;&#65289;&#21644;&#21333;&#20803;&#27979;&#35797;&#31867;&#21516;&#19968;&#30446;&#24405;&#19979;&#21516;&#20320;&#30340;&#27979;&#35797;&#31867;&#25991;&#20214;&#21516;&#21517;&#30340;.properties&#25991;&#20214;&#65288;ResourceBundle&#65289;&#65292;&#36825;&#20010;&#23646;&#24615;&#25991;&#20214;&#30340;&#23646;&#24615;&#21487;&#20197;&#36890;&#36807;“rb”&#23646;&#24615;&#26469;&#35775;&#38382;&#12290;
At line 183 changed 1 line.
;:%%(color: blue)''I usually copy (open &rarr; save as) an existing test (i.e. UserDaoTest.java) and find/replace [[Uu]ser with [[Pp]erson, or whatever the name of my object is.''%%
;:%%(color: blue)''&#25105;&#32463;&#24120;&#25335;&#36125;(&#25171;&#24320;&rarr;&#21478;&#23384;&#20026;)&#19968;&#20010;&#24050;&#23384;&#22312;&#30340;&#27979;&#35797;(&#22914;UserDaoTest.java)&#65292;&#28982;&#21518;&#26597;&#25214;/&#26367;&#25442; [[Uu]ser&#20026;[[Pp]erson&#65292;&#25110;&#32773;&#20219;&#20309;&#20854;&#23427;&#38656;&#35201;&#26367;&#25442;&#30340;&#20869;&#23481;&#12290;''%%
At line 197 changed 3 lines.
protected void setUp() throws Exception {
super.setUp();
dao = (PersonDao) ctx.getBean("personDao");
public void setPersonDao(PersonDao dao) {
this.dao = dao;
At line 201 removed 5 lines.
protected void tearDown() throws Exception {
super.tearDown();
dao = null;
}
At line 209 changed 1 line.
The code you see above is what we need for a basic JUnit test that initializes and destroys our PersonDao. The "ctx" object is a reference to Spring's ApplicationContext, which is initialized in a static block of the [BaseDaoTestCase's|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDaoTestCase.java.html] class.
&#20197;&#19978;&#26159;&#25105;&#20204;&#20351;&#29992;JUnit&#27979;&#35797;&#32780;&#21021;&#22987;&#21270;&#21644;&#38144;&#27585;PersonDao&#30340;&#22522;&#26412;&#20195;&#30721;&#65292;&#23545;&#35937;“ctx”&#24341;&#29992;&#20102;Spring&#30340;ApplicationContext&#65292;&#23427;&#22312;[BaseDaoTestCase|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDaoTestCase.java.html]&#31867;&#30340;&#38745;&#24577;&#20195;&#30721;&#21306;&#37324;&#34987;&#21021;&#22987;&#21270;&#12290;
At line 211 changed 1 line.
Now we need to actually test that the CRUD (create, retrieve, update, delete) methods work in our DAO. To do this we created methods that begin with "test" (all lower case). As long as these methods are public, have a void return type and take no arguments, they will be called by our &lt;junit&gt; task in our Ant build.xml file. Here's some simple tests for testing CRUD. An important thing to remember is that each method (also known as a test), should be autonomous. Add the following methods to your {{PersonDaoTest.java}} file:
&#29616;&#22312;&#25105;&#20204;&#38656;&#35201;&#23454;&#38469;&#27979;&#35797;DAO&#20013;&#30340;CRUD(create, retrieve, update, delete)&#26041;&#27861;&#65292;&#20026;&#27492;&#25105;&#20204;&#38656;&#35201;&#20026;&#27599;&#20010;&#26041;&#27861;&#24314;&#31435;&#20197;test&#65288;&#20840;&#37096;&#23567;&#20889;&#65289;&#24320;&#22836;&#30340;&#27979;&#35797;&#26041;&#27861;&#65292;&#21482;&#35201;&#36825;&#20010;&#26041;&#27861;&#26159;&#20844;&#20849;&#30340;&#65292;&#36820;&#22238;&#31867;&#22411;&#26159;void,&#23427;&#20204;&#23601;&#20250;&#34987;&#25105;&#20204;build.xml&#20013;&#30340;Ant&#30340;&lt;junit&gt;&#20219;&#21153;&#35843;&#29992;&#65292;&#22914;&#19979;&#26159;&#19968;&#20123;&#31616;&#21333;&#30340;CRUD&#27979;&#35797;&#65292;&#38656;&#35201;&#27880;&#24847;&#30340;&#19968;&#28857;&#26159;&#25152;&#26377;&#30340;&#26041;&#27861;&#65288;&#25110;&#32773;&#21483;&#20570;&#27979;&#35797;&#65289;&#24517;&#39035;&#26159;&#33258;&#27835;&#30340;&#65292;&#28155;&#21152;&#22914;&#19979;&#20195;&#30721;&#21040;&#25991;&#20214;{{PersonDaoTest.java}}&#65306;
At line 268 removed 1 line.
;:%%(color: blue)''In the testGetPerson method, we're creating a person and then calling a get. I usually enter a record in the database that I can always rely on. Since [DBUnit|http://www.dbunit.org] is used to populate the database with test data before our tests are run, you can simply add the new table/record to the metadata/sql/sample-data.xml file:''%%
At line 265 added 2 lines.
;:%%(color: blue)''&#22312;testGetPerson&#26041;&#27861;&#65292;&#25105;&#20204;&#21019;&#24314;&#20102;&#19968;&#20010;person&#24182;&#19988;&#35843;&#29992;get&#26041;&#27861;&#65292;&#25105;&#36890;&#24120;&#20250;&#22686;&#21152;&#19968;&#26465;&#25105;&#25152;&#38656;&#35201;&#30340;&#35760;&#24405;&#21040;&#25968;&#25454;&#24211;&#65292;&#22240;&#20026;&#22312;&#27979;&#35797;&#36816;&#34892;&#20043;&#21069;[DBUnit|http://www.dbunit.org]&#20250;&#20026;&#25968;&#25454;&#24211;&#20934;&#22791;&#27979;&#35797;&#25968;&#25454;&#65292;&#25105;&#20204;&#21487;&#20197;&#31616;&#21333;&#30340;&#22312;metadata/sql/sample-data.xml&#37324;&#28155;&#21152;&#27979;&#35797;&#25152;&#24517;&#39035;&#30340;&#35760;&#24405;''%%
At line 285 changed 1 line.
;:%%(color: blue)''This way, you can eliminate the "create new" functionality in the testGetPerson method. If you'd rather add this record directly into the database (via SQL or a GUI), you can rebuild your {{sample-data.xml}} file using __ant db-export__ and then __cp {{db-export.xml metadata/sql/sample-data.xml}}__.''%%
;:%%(color: blue)''&#36890;&#36807;&#36825;&#31181;&#26041;&#24335;&#20320;&#21487;&#20197;&#22312;testGetPerson&#26041;&#27861;&#37324;&#28040;&#38500;&#21019;&#24314;&#26032;&#32426;&#24405;&#30340;&#21160;&#20316;&#65292;&#22914;&#26524;&#20320;&#24895;&#24847;&#30452;&#25509;&#25554;&#20837;&#35760;&#24405;&#21040;&#25968;&#25454;&#24211;&#65288;&#20351;&#29992;SQL&#25110;&#32773;GUI&#65289;&#65292;&#20320;&#21487;&#20197;&#29992;__ant db-export__&#21644;__cp {{db-export.xml metadata/sql/sample-data.xml}}__&#37325;&#26032;&#26500;&#24314;&#20320;&#30340;{{sample-data.xml}}&#25991;&#20214;&#12290;''%%
At line 287 changed 2 lines.
In the above example, you can see that we're calling person.set*(value) to populate our object before saving it. This is easy in this example, but it could get quite cumbersome if we're persisting an object with 10 required fields (not-null="true"). This is why I created the ResourceBundle in the BaseDaoTestCase. Simply create a {{PersonDaoTest.properties}} file in the same directory as {{PersonDaoTest.java}} and define your property values inside it:
;:%%(color: blue)''I tend to just hard-code test values into Java code - but the .properties file is an option that works great for large objects.''%%
&#22312;&#19978;&#38754;&#30340;&#20363;&#23376;&#37324;&#65292;&#20320;&#21487;&#20197;&#30475;&#21040;&#25105;&#20204;&#35843;&#29992;person.set*(value)&#26469;&#20934;&#22791;&#25105;&#20204;&#38656;&#35201;&#20445;&#23384;&#30340;&#23545;&#35937;&#65292;&#22312;&#36825;&#20010;&#20363;&#23376;&#37324;&#24456;&#31616;&#21333;&#65292;&#20294;&#26159;&#24403;&#20320;&#35201;&#25554;&#20837;10&#26465;&#24517;&#28155;&#23383;&#27573;(not-null="true")&#26102;&#23601;&#27604;&#36739;&#40635;&#28902;&#20102;&#65292;&#36825;&#23601;&#26159;&#25105;&#20026;&#20160;&#20040;&#35201;&#22312;BaseDaoTestCase&#20351;&#29992;ResourceBundle&#25991;&#20214;&#65292;&#21482;&#35201;&#22312;{{PersonDaoTest.java}}&#21516;&#19968;&#20010;&#30446;&#24405;&#21019;&#24314;&#19968;&#20010;{{PersonDaoTest.properties}}&#24182;&#19988;&#22312;&#37324;&#38754;&#23450;&#20041;&#20320;&#30340;&#23646;&#24615;&#20540;&#65306;
;:%%(color: blue)''&#25105;&#36890;&#24120;&#21482;&#26159;&#22312;Java&#37324;&#30828;&#32534;&#30721;&#65292;&#20294;&#26159;&#36825;&#20010;.properties&#23545;&#20110;&#22823;&#23545;&#35937;&#24456;&#26377;&#29992;&#12290;''%%
At line 293 changed 1 line.
Then, rather than calling person.set* to populate your objects, you can use the BaseDaoTestCase.populate(java.lang.Object) method:
&#27492;&#26102;&#65292;&#20320;&#35201;&#36890;&#36807;&#35843;&#29992;BaseDaoTestCase.populate(java.lang.Object)&#26041;&#27861;&#26469;&#20934;&#22791;&#23545;&#35937;&#65292;&#32780;&#19981;&#26159;&#20351;&#29992;person.set*&#12290;
At line 301 changed 1 line.
At this point, the PersonDaoTest class won't compile yet because there is no PersonDao.class in our classpath, we need to create it. PersonDAO.java is an interface, and PersonDAOHibernate.java is the Hibernate implementation of that interface. Let's go ahead and create those.
&#22312;&#30446;&#21069;&#24773;&#20917;&#19979;&#65292;&#36824;&#19981;&#21487;&#20197;&#32534;&#35793;PersonDaoTest&#65292;&#22240;&#20026;&#22312;&#31867;&#36335;&#24452;&#37324;&#36824;&#27809;&#26377;PersonDao.class&#65292;&#25105;&#20204;&#38656;&#35201;&#21019;&#24314;&#23427;&#12290;PersonDao.java&#26159;&#19968;&#20010;&#25509;&#21475;&#65292;PersonDaoHibernate.java&#26159;&#23427;&#30340;Hibernate&#23454;&#29616;&#65292;&#35753;&#25105;&#20204;&#32487;&#32493;&#65292;&#24320;&#22987;&#21019;&#24314;&#12290;
At line 303 changed 2 lines.
!!Create a new DAO to perform CRUD on the object [#4]
First off, create a PersonDao.java interface in the {{src/dao/**/dao}} directory and specify the basic CRUD methods for any implementation classes. ''I've eliminated the JavaDocs in the class below for display purposes.''
!!&#21019;&#24314;&#19968;&#20010;&#23545;&#23545;&#35937;&#25191;&#34892;CRUD&#25805;&#20316;&#30340;&#26032;DAO[#4]
&#39532;&#19978;&#65292;&#22312;{{src/dao/**/dao}}&#30446;&#24405;&#37324;&#24314;&#31435;PersonDao.java&#25509;&#21475;&#65292;&#24182;&#19988;&#25351;&#23450;&#25152;&#26377;&#23454;&#29616;&#31867;&#35201;&#23454;&#29616;&#30340;&#22522;&#26412;CRUD&#25805;&#20316;&#65292;&#20026;&#20102;&#26174;&#31034;&#26041;&#20415;&#65292;&#25105;&#24050;&#32463;&#21435;&#25481;&#20102;&#25152;&#26377;JavaDocs&#12290;
At line 319 changed 1 line.
Notice in the class above there are no exceptions on the method signatures. This is due to the power of [Spring|http://www.springframework.org] and how it wraps Exceptions with RuntimeExceptions. At this point, you should be able to compile all the source in {{src/dao}} and {{test/dao}} using __ant compile-dao__. However, if you try to run __ant test-dao -Dtestcase=PersonDao__, you will get an error: <span style="color: red">No bean named 'personDao' is defined</span>. This is an error message from Spring - indicating that we need to specify a bean named ''personDAO'' in {{applicationContext-hibernate.xml}}. Before we do that, we need to create the PersonDao implementation class.
&#27880;&#24847;&#65292;&#22312;&#20197;&#19978;&#30340;&#26041;&#27861;&#22768;&#26126;&#19978;&#24182;&#27809;&#26377;exceptions&#35828;&#26126;&#65292;&#36825;&#26159;&#22240;&#20026;[Spring|http://www.springframework.org]&#20351;&#29992;RuntimeExceptions&#26469;&#21253;&#35065;Exceptions&#30340;&#26041;&#24335;&#65292;&#27492;&#26102;&#65292;&#20320;&#24050;&#32463;&#21487;&#20197;&#20351;&#29992;__ant compile-dao__&#26469;&#32534;&#35793;{{src/dao}}&#21644;{{test/dao}}&#19979;&#30340;&#25152;&#26377;&#28304;&#25991;&#20214;&#65292;&#28982;&#32780;&#24403;&#20320;&#36816;&#34892;__ant test-dao -Dtestcase=PersonDao__&#36827;&#34892;&#27979;&#35797;&#26102;&#65292;&#20320;&#20250;&#24471;&#21040;&#19968;&#20010;&#38169;&#35823;&#65306;<span style="color: red">No bean named 'personDao' is defined</span>&#65292;&#36825;&#26159;&#19968;&#20010;Spring&#30340;&#38169;&#35823;&#65292;&#35828;&#26126;&#20320;&#24517;&#39035;&#22312;{{applicationContext-hibernate.xml}}&#25351;&#23450;&#19968;&#20010;&#21517;&#23383;&#20026;''personDAO''&#30340;bean&#65292;&#22312;&#27492;&#20043;&#21069;&#25105;&#20204;&#38656;&#35201;&#21019;&#24314;PersonDao&#30340;&#23454;&#29616;&#31867;&#12290;
At line 321 changed 1 line.
;:''The ant task for running dao tests is called __test-dao__. If you pass in a testcase parameter (using __-Dtestcase=name__), it will look for {{**/*${testcase}*}} - allowing us to pass in Person, PersonDao, or PersonDaoTest - all of which will execute the PersonDaoTest class.''
;:''&#36816;&#34892;dao&#27979;&#35797;&#30340;ant&#20219;&#21153;&#21483;&#20570;__test-dao__&#65292;&#22914;&#26524;&#20320;&#20256;&#36882;testcase&#21442;&#25968;(&#29992;__-Dtestcase=name__)&#65292;&#23427;&#20250;&#26597;&#30475;{{**/*${testcase}*}}&#20801;&#35768;&#25105;&#20204;&#20256;&#36882;Person&#12289;PersonDao&#12289;&#25110;&#32773;PersonDaoTest&#20197;&#21450;&#25152;&#26377;&#20250;&#25191;&#34892;PersonDaoTest&#30340;&#31867;&#12290;''
At line 323 changed 1 line.
Let's start by creating a PersonDaoHibernate class that implements the methods in PersonDao and uses Hibernate to get/save/delete the Person object. To do this, create a new class in {{src/dao/**/dao/hibernate}} and name it {{PersonDAOHibernate.java}}. It should extend [BaseDaoHibernate|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDAOHibernate.java.html] and implement PersonDAO. ''Javadocs eliminated for brevity.''
&#35753;&#25105;&#20204;&#21019;&#24314;&#19968;&#20010;&#23454;&#29616;PersonDao&#30340;&#31867;PersonDaoHibernate&#24182;&#20351;&#29992;Hibernate&#26469;get/save/delete&#36825;&#20010;Person&#23545;&#35937;&#65292;&#20026;&#27492;&#65292;&#25105;&#20204;&#22312;{{src/dao/**/dao/hibernate}}&#21019;&#24314;&#19968;&#20010;&#26032;&#31867;{{PersonDaoHibernate.java}}&#65292;&#23427;&#24212;&#35813;&#25193;&#23637;[BaseDaoHibernate|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDAOHibernate.java.html]&#65292;&#24182;&#19988;&#23454;&#29616;PersonDao&#12290;''&#20026;&#20102;&#31616;&#27905;&#65292;&#30465;&#30053;Javadocs&#12290;''
At line 356 changed 1 line.
Now, if you try to run __ant test-dao -Dtestcase=PersonDao__, you will get the same error. We need to configure Spring so it knows that PersonDaoHibernate is the implementation of PersonDAO, and we also need to tell it about the Person object.
&#29616;&#22312;&#65292;&#22914;&#26524;&#20320;&#36816;&#34892;__ant test-dao -Dtestcase=PersonDao__&#65292;&#20320;&#20250;&#24471;&#21040;&#21516;&#26679;&#30340;&#38169;&#35823;&#65292;&#25105;&#20204;&#24517;&#39035;&#37197;&#32622;Spring&#26469;&#35753;&#23427;&#30693;&#36947;PersonDaoHibernate&#26159;PersonDao&#30340;&#23454;&#29616;&#65292;&#21516;&#26679;&#30340;&#65292;&#25105;&#20204;&#20063;&#35201;&#21578;&#35785;&#23427;&#36824;&#26377;&#20010;Person&#23545;&#35937;&#12290;
At line 358 changed 1 line.
!!Configure Spring for the Person object and PersonDao [#5]
!!&#37197;&#32622;Spring&#20013;&#30340;Person&#21644;PersonDao [#5]
At line 360 changed 1 line.
First, we need to tell Spring where the Hibernate mapping file is located. To do this, open {{src/dao/**/dao/hibernate/applicationContext-hibernate.xml}} and add {{"Person.hbm.xml"}} to the following code block.
&#39318;&#20808;&#25105;&#20204;&#35201;&#21578;&#35785;Spring&#25152;&#26377;Hibernate&#25991;&#20214;&#30340;&#20301;&#32622;&#65292;&#20026;&#27492;&#65292;&#25171;&#24320;{{src/dao/**/dao/hibernate/applicationContext-hibernate.xml}}&#65292;&#22312;&#20197;&#19979;&#20195;&#30721;&#22359;&#28155;&#21152;{{"Person.hbm.xml"}}&#12290;
At line 368 changed 2 lines.
<value>org/appfuse/model/User.hbm.xml</value>
<value>org/appfuse/model/UserCookie.hbm.xml</value>
<value>org/appfuse/model/User.hbm.xml</value>
At line 374 changed 1 line.
Now we need to add some XML to this file to bind PersonDaoHibernate to PersonDao. To do this, add the following at the bottom of the file:
&#29616;&#22312;&#25105;&#20204;&#38656;&#35201;&#28155;&#21152;&#19968;&#20123;XML&#25968;&#25454;&#26469;&#32465;&#23450;PersonDaoHibernate&#21040;PersonDao&#65292;&#20026;&#27492;&#65292;&#28155;&#21152;&#22914;&#19979;&#20195;&#30721;&#21040;&#25991;&#20214;&#24213;&#37096;&#65306;
At line 384 changed 1 line.
;:''You could also use __autowire="byName"__ to the &lt;bean&gt; and get rid of the "sessionFactory" property''. %%(color: blue)''Personally, I like having the dependencies of my objects documented (in XML).''%%
;:''&#20320;&#20063;&#21487;&#20197;&#20026;&lt;bean&gt;&#20351;&#29992;__autowire="byName"__&#23646;&#24615;&#26469;&#28040;&#38500;"sessionFactory"&#23646;&#24615;''&#12290;%%(color: blue)''&#20174;&#20010;&#20154;&#26469;&#35762;&#65292;&#25105;&#21916;&#27426;&#22312;XML&#25991;&#20214;&#37324;&#20445;&#30041;&#23545;&#35937;&#30340;&#20381;&#36182;&#12290;''%%
At line 386 changed 2 lines.
!!Run the DaoTest [#6]
Save all your edited files and try running __ant test-dao -Dtestcase=PersonDao__ one more time.
!!&#36816;&#34892;DaoTest[#6]
&#20445;&#23384;&#25152;&#26377;&#20462;&#25913;&#30340;&#25991;&#20214;&#65292;&#36816;&#34892;__ant test-dao -Dtestcase=PersonDao__&#12290;
At line 395 changed 1 line.
''Next Up:'' __Part II:__ [Creating new Managers|CreateManager] - A HowTo for creating Business Facades, which are similar to [Session Facades|http://java.sun.com/blueprints/patterns/SessionFacade.html], but don't use EJBs. These facades are used to provide communication from the front-end to the DAO layer.
''&#19979;&#19968;&#37096;&#20998;:'' __Part II:__ [创建新的Manager|CreateManager_zh] - &#26159;&#19968;&#20010;&#24314;&#31435;&#31867;&#20284;&#20110;[Session Facades|http://java.sun.com/blueprints/patterns/SessionFacade.html]&#30340;&#65292;&#20294;&#19981;&#20351;&#29992;EJBs&#30340;&#19994;&#21153;Facade&#35828;&#26126;&#65292;&#36825;&#20010;facades&#29992;&#26469;&#24314;&#31435;&#20174;&#21069;&#31471;&#21040;DAO&#23618;&#30340;&#32852;&#31995;&#12290;

Back to CreateDAO_zh, or to the Page History.