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 15 and version 14:

At line 323 added 76 lines.
;:''运行dao测试的ant任务叫做__test-dao__,如果你传递testcase参数(用__-Dtestcase=name__),它会查看{{**/*${testcase}*}}允许我们传递Person、PersonDao、或者PersonDaoTest以及所有会执行PersonDaoTest的类。''
让我们创建一个实现PersonDao的类PersonDaoHibernate并使用Hibernate来get/save/delete这个Person对象,为此,我们在{{src/dao/**/dao/hibernate}}创建一个新类{{PersonDAOHibernate.java}},它应该扩展[BaseDaoHibernate|http://raibledesigns.com/downloads/appfuse/api/org/appfuse/dao/BaseDAOHibernate.java.html],并且实现PersonDAO。为了简洁,省略Javadocs。''
[{Java2HtmlPlugin
package org.appfuse.dao.hibernate;
import org.appfuse.model.Person;
import org.appfuse.dao.PersonDao;
import org.springframework.orm.ObjectRetrievalFailureException;
public class PersonDaoHibernate extends BaseDaoHibernate implements PersonDao {
public Person getPerson(Long id) {
Person person = (Person) getHibernateTemplate().get(Person.class, id);
if (person == null) {
throw new ObjectRetrievalFailureException(Person.class, id);
}
return person;
}
public void savePerson(Person person) {
getHibernateTemplate().saveOrUpdate(person);
}
public void removePerson(Long id) {
// object must be loaded before it can be deleted
getHibernateTemplate().delete(getPerson(id));
}
}
}]
现在,如果你运行__ant test-dao -Dtestcase=PersonDao__,你会得到同样的错误,我们必须配置Spring来让它知道PersonDaoHibernate是PersonDAO的实现,同样的,我们也要告诉它还有个Person对象。
!!配置Spring中的Person和PersonDao [#5]
首先我们要告诉Spring所有Hibernate文件的位置,为此,打开{{src/dao/**/dao/hibernate/applicationContext-hibernate.xml}},在以下代码块添加{{"Person.hbm.xml"}}。
[{Java2HtmlPlugin
<property name="mappingResources">
<list>
<value>org/appfuse/model/Person.hbm.xml</value>
<value>org/appfuse/model/Role.hbm.xml</value>
<value>org/appfuse/model/User.hbm.xml</value>
<value>org/appfuse/model/UserCookie.hbm.xml</value>
</list>
</property>
}]
&#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;
[{Java2HtmlPlugin
<!-- PersonDao: Hibernate implementation -->
<bean id="personDao" class="org.appfuse.dao.hibernate.PersonDaoHibernate">
<property name="sessionFactory"><ref local="sessionFactory"/></property>
</bean>
}]
;:''&#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;''%%
!!&#36816;&#34892;DaoTest[#6]
&#20445;&#23384;&#25152;&#26377;&#20462;&#25913;&#30340;&#25991;&#20214;&#65292;&#36816;&#34892;__ant test-dao -Dtestcase=PersonDao__&#12290;
__Yeah Baby, Yeah:__
%%(color:green)BUILD SUCCESSFUL\\
Total time: 9 seconds%%
----
''&#19979;&#19968;&#37096;&#20998;:'' __Part II:__ [&#21019;&#24314;&#31649;&#29702;&#22120;Manager|CreateManager] - &#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.