程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> appfuse1.7幫助中創建DAO段代碼

appfuse1.7幫助中創建DAO段代碼

編輯:關於JAVA

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

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved