appfuse1.7協助中建立DAO段代碼

來源:互聯網
上載者:User
建立
/* * 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);  } }

}




相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。