Create DAO segment code in appfuse1.7 Help

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); } }

}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.