欢迎光临
我们一直在努力

appfuse1.7帮助中创建DAO段代码-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

/* * 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的一个引用,它在basedaotestcases // 类的静态代码块中被初始化。 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);  } }

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » appfuse1.7帮助中创建DAO段代码-JSP教程,Java技巧及代码
分享到: 更多 (0)