當前位置:開發者網絡 >> 技術教程 >> JSP教程 >> Java技巧及代碼 >> 內容
精彩推薦
分類最新教程
分類熱點教程
    
appfuse1.7幫助中創建DAO段代碼
作者:未知
日期:2005-04-30
人氣:
投稿:(轉貼)
來源:未知
字體:
收藏:加入瀏覽器收藏
以下正文:

/* * 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="50"  */ 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="50"  */ 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 object) {  if (!(object instanceof Person)) {   return false;  }  Person rhs = (Person) object;  return new EqualsBuilder()     .append(this.firstName, rhs.firstName)     .append(this.id, rhs.id)     .append(this.lastName, 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("lastName", 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 { //一個基本的初始化、銷毀PersonDAO對象的Junit測試 private Person person = null;

 private PersonDAO dao = null;

 protected void setUp() throws Exception {  super.setUp();  dao = (PersonDAO) ctx.getBean("personDAO"); }

 //  "ctx" 對象是對Spring的ApplicationContext的一個引用,它在BaseDAOTestCase's // 類的靜態代碼塊中被初始化。 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);  } }

}


相關文章: