Create DAO segment code in appfuse1.7 Help
Last Update:2017-02-28
Source: Internet
Author: User
Create
/* Created on 2005-4-21 */package Org.appfuse.model;
Import Org.apache.commons.lang.builder.equalsbuilder;import Org.apache.commons.lang.builder.HashCodeBuilder; Import Org.apache.commons.lang.builder.ToStringBuilder;
/** * @hibernate. Class table= ' person ' */public class person extends Baseobject {
Private Long ID;
Private String FirstName;
Private String LastName;
/** * @return Returns the firstName. * @hibernate. Property column= "First_Name" length= "M"/Public String Getfirstname () {return firstName;}
/** * @param firstName * The FirstName to set. */public void Setfirstname (String firstName) {this.firstname = FirstName;}
/** * @return Returns the ID. * @hibernate. ID column= "id" generator-class= "native" unsaved-value= "null" */public Long getId () {return ID;}
/** * @param ID * The ID to set. */public void setId (Long id) {this.id = ID;}
/** * @return Returns the lastName. * @hibernate. Property column= "Last_Name" length= "M"/Public String Getlastname () {return lastName;}
/** * @param lastName * The LastName to set. */public void Setlastname (String lastName) {this.lastname = LastName;}
/** * @see Java.lang.object#equals (object) */public Boolean equals (object) {if (!) ( Object instanceof) {return false; Person RHS = (person) object; Return to New Equalsbuilder (). Append (This.firstname, Rhs.firstname). Append (This.id, rhs.id). Append (This.lastna Me, Rhs.lastname). Isequals (); }/** * Public boolean equals (Object object) {if (!) ( Object instanceof Address)} {return false; }
Address RHS = (address) object;
return new Equalsbuilder (). Append (This.postalcode, Rhs.postalcode) Append (this.country , Rhs.country) Append ( This.address, rhs.address) Append (this.province, rhs.province) . Append (this.city, rhs.city) isequals (); } */ /** * @see java.lang.Object #hashCode () */ public int hashcode () { return new Hashcodebuilder (-1196181247,-1232855255) .append (This.firstname) .append (this.id) .append (this.lastName). ToHashCode ( ); }
/** * @see java.lang.object#tostring () */public String toString () {return new Tostringbuilder (this). Append ("L Astname ", This.lastname). Append (" id ", this.id). Append (" FirstName ", This.firstname). toString (); }}
############################################################# #package Org.appfuse.dao;
Import Org.appfuse.model.person;import org.springframework.dao.DataAccessException;
public class Persondaotest extends Basedaotestcase {//a base initialization, destroy Persondao object's junit test private person person = null;
Private Persondao DAO = null;
protected void SetUp () throws Exception {Super.setup (); DAO = (Persondao) ctx.getbean ("Persondao"); }
The "CTX" object is a reference to spring's ApplicationContext, which is initialized in the static code block of the Basedaotestcase ' s//class. protected void teardown () throws Exception {Super.teardown (); DAO = null; }
//------------------------------------------------------------------------------------------------------------- ----------------public void Testgetperson () throws Exception {person = new person (); Person.setfirstname ("Matt"); Person.setlastname ("raible");
Dao.saveperson (person); Assertnotnull (Person.getid ());
person = Dao.getperson (Person.getid ()); Assertequals (Person.getfirstname (), "Matt"); }
public void Testsaveperson () throws Exception {person = Dao.getperson (new Long (1)); Person.setfirstname ("Matt");
Person.setlastname ("Last Name Updated");
Dao.saveperson (person);
if (log.isdebugenabled ()) {Log.debug ("Updated Person:" + person); }
Assertequals (Person.getlastname (), "Last Name Updated"); }
public void Testaddandremoveperson () throws Exception {person = new person (); Person.setfirstname ("Bill"); Person.setlastname ("Joy");
Dao.saveperson (person);
Assertequals (Person.getfirstname (), "Bill"); Assertnotnull (Person.getid ());
if (log.isdebugenabled ()) {log.debug ("removing person ..."); }
Dao.removeperson (Person.getid ());
try {person = Dao.getperson (Person.getid ()); Fail ("Person found in database"); catch (DataAccessException Dae) {log.debug ("expected exception:" + Dae.getmessage ()); Assertnotnull (DAE); } }
}