·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » appfuse1.7帮助中创建DAO段代码

appfuse1.7帮助中创建DAO段代码

类别: JAVA教程  评论数:0 总得分:0

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

}





-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1