首页(162) xml(5) spring(1) 生活(8) java(70) 代码(10) 英语(4) 数据库(7) c#(14) 成长(10) 软件工程(27)  写新日志
 
 

玻璃杯中的花生壳

  真爱的事业和真正的爱情一生只有一次,都值得我们温柔地相待,因为那种感觉是永远都无法复制的, 这世界真正属于你的东西其实并不多,你不好好珍惜,它便会离你而去,包括机遇,包括爱情,包括生命。
   不要找任何理由,  当幸福在你身边的时候就抓住它,你就一定会很幸福! 
   

时 间 记 忆
«September 2025»
123456
78910111213
14151617181920
21222324252627
282930

最 新 评 论
回复:xml的Jdom解析过程详解
回复:突然想到的几句话!
 Boyle came out of n
回复:xml的Jdom解析过程详解
回复:配置Spring数据源
回复:使用SAX解析XML
回复:java中写文件操作时FileOu
回复:关联和依赖关系的区分
回复:HttpSessionListen
回复:Spring AOP四种创建通知(

最 新 日 志
Java开发者的十大戒律
配置Spring数据源
java多线程设计模式
java中switch的使用
性格,编码,测试
突然想到的几句话!
理解Spring AOP中的关键概念
Spring AOP四种创建通知(拦截器
xml的四种解析方法 比较 sax,do
xml的Jdom解析过程详解

最 新 留 言
签写新留言

我渴望知识
很好的东东
帖子不错,道声谢
想拜师学艺
我的呼喊

搜 索


用 户 登 录
用户名称:
登陆密码:
密码保存:

友 情 连 接

模板设计:部落窝模板世界

blog名称:玻璃杯中的花生壳
日志总数:162
评论数量:249
留言数量:1
访问次数:828480
建立时间:2004年11月4日
 
 
 
[java]Appfuse开发实践(四)—— 创建Webwork 框架的 Actions和JSP
[ 2006/8/8 15:30:36 | By: 玻璃杯中的花生壳 ]
 
  第一部分: 在Appfuse中创建新的DAO对象 - 本部分描述如何创建一个POJO并且创建一个Java类把这个对象存储到数据库中实现持久化。        我们将使用Hibernate作为示例,如果需要使用其他的持久化方案,可以参考Appfuse的相关文档。 内容提要       第一部分: 在Appfuse中创建新的DAO对象 - 本部分描述如何创建一个POJO并且创建一个Java类把这个对象存储到数据库中实现持久化。        我们将使用Hibernate作为示例,如果需要使用其他的持久化方案,可以参考Appfuse的相关文档。 内容提要 [1] 创建一个新的POJO对象并且加入xdoclet标签 [2] Create a new database table from the object using Ant [3] Create a new DAOTest to run JUnit tests on the DAO [4] Create a new DAO to perform CRUD on the object [5] Configure Spring for the Person object and PersonDAO [6] Run the DAOTest 1、创建一个新的POJO对象并且加入xdoclet标签      首先要创建一个需要持久化的对象。我们将创建一个简单的Person对象(包括id,firstName,lastName三个属性)把这个对象放在src/dao/**/model目录中   500)this.width=500'>package org.myApp.model;500)this.width=500'>500)this.width=500'>500)this.width=500'>public class Person extends BaseObject 500)this.width=500'>{500)this.width=500'>  private Long id;500)this.width=500'>  private String firstName;500)this.width=500'>  private String lastName;500)this.width=500'>   500)this.width=500'>500)this.width=500'>500)this.width=500'>    /**//*产生三个属性对应的setter/getter方法500)this.width=500'>     产   */500)this.width=500'>}  500)this.width=500'>500)this.width=500'>       这个类必须继承基类org.appfuse.model.BaseObject,必须实现三个抽象方法equals(), hashCode() and toString().前两个方法是Hibernate需要的.比较容易的方法是使用Commonclipse 插件自动产生着几个方法的实现.       使用IntelliJ IDEA可以产生generate equals() 和 hashCode()方法, 不过没有toString()方法. 可以使用 ToStringPlugin 插件。 现在创建了POJO对象,接下来需要加入XDoclet标签以便产生Hibernate映射文件。这个映射文件描述了对象→表和属性 →字段的映射关系。 用@hibernate.class标签说明表和对象的对应关系:   500)this.width=500'>500)this.width=500'>/**//**500)this.width=500'> * @hibernate.class table="person"500)this.width=500'> */500)this.width=500'>500)this.width=500'>public class Person extends BaseObject 500)this.width=500'>{  500)this.width=500'>500)this.width=500'>We also have to add a primary key mapping or XDoclet will puke when generating the mapping file. Note that all @hibernate.* tags should be placed in the getters' Javadocs of your POJOs. 500)this.width=500'>500)this.width=500'>500)this.width=500'>500)this.width=500'>    /**//**500)this.width=500'>     * @return Returns the id.500)this.width=500'>     * @hibernate.id column="id"500)this.width=500'>     *  generator-class="increment" unsaved-value="null"500)this.width=500'>     */500)this.width=500'>500)this.width=500'>500)this.width=500'>    public Long getId() 500)this.width=500'>{500)this.width=500'>        return this.id;500)this.width=500'>    }  500)this.width=500'>500)this.width=500'>           这里使用generator-class="increment" 代替 generate-class="native" 因为作者在使用某些数据库时发现了一些问题。如果仅仅使用MySQL,建议使用"native" value。 2、使用Ant创建数据库表       这是,可以运行"ant setup-db"任务来创建Person表,这个任务将创建Person.hbm.xml 文件并且在数据库中创建"person."表。 在控制台上,你可以看见Hibernate创建的table schema: [schemaexport] create table person ([schemaexport]    id bigint not null,[schemaexport]    primary key (id)[schemaexport] );     如果你Hibernate产生的Person.hbm.xml的文件,可以在build/dao/gen/**/hibernate 目录下找到它,这是这个文件的具体内容 :   500)this.width=500'><?xml version="1.0"?>500)this.width=500'>500)this.width=500'><!DOCTYPE hibernate-mapping PUBLIC500)this.width=500'>    "-//Hibernate/Hibernate Mapping DTD 2.0//EN" 500)this.width=500'>    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">500)this.width=500'>500)this.width=500'><hibernate-mapping>500)this.width=500'>    <class500)this.width=500'>        name="org.myApp.model.Person"500)this.width=500'>        table="person"500)this.width=500'>        dynamic-update="false"500)this.width=500'>        dynamic-insert="false"500)this.width=500'>    >500)this.width=500'>500)this.width=500'>        <id500)this.width=500'>            name="id"500)this.width=500'>            column="id"500)this.width=500'>            type="java.lang.Long"500)this.width=500'>            unsaved-value="null"500)this.width=500'>        >500)this.width=500'>            <generator class="increment">500)this.width=500'>            </generator>500)this.width=500'>        </id>500)this.width=500'>500)this.width=500'>        <!--500)this.width=500'>            To add non XDoclet property mappings, create a file named500)this.width=500'>                hibernate-properties-Person.xml500)this.width=500'>            containing the additional properties and place it in your merge dir.500)this.width=500'>        -->500)this.width=500'>500)this.width=500'>    </class>500)this.width=500'>500)this.width=500'></hibernate-mapping>  500)this.width=500'>        现在加入@hibernate.property描述我们的其他字段信息:      500)this.width=500'>500)this.width=500'>/**//**500)this.width=500'>     * @hibernate.property column="first_name" length="50"500)this.width=500'>     */500)this.width=500'>500)this.width=500'>    public String getFirstName() 500)this.width=500'>{500)this.width=500'>        return this.firstName;500)this.width=500'>    }500)this.width=500'>500)this.width=500'>500)this.width=500'>    /**//**500)this.width=500'>     * @hibernate.property column="last_name" length="50"500)this.width=500'>     */500)this.width=500'>500)this.width=500'>    public String getLastName() 500)this.width=500'>{500)this.width=500'>        return this.lastName;500)this.width=500'>    }  500)this.width=500'>500)this.width=500'>       在这个例子中,加入column属性是因为创建的数据表的字段名和对应的类中的属性名不一样,如果是一样的,不需要加入这个属性说明。 运行 "ant setup-db"创建数据表. [schemaexport] create table person ([schemaexport]    id bigint not null,[schemaexport]    first_name varchar(50),[schemaexport]    last_name varchar(50),[schemaexport]    primary key (id)[schemaexport] );        如果需要改变字段的长度,修改@hibernate.property 标签对应的属性值。如果希望他是一个必须的字段(NOT NULL),,加上not-null="true"这样的属性。 3、创建一个DAOTest对象并且用JUnit测试你的DAO对象       现在我们创建一个DAOTest类来测试我们的DAO的工作状况。 “等一下”你说,“我们还没有创建一个DAO对象”你是对的。然而我们发现测试驱动开发(Test-Driven Development)是开发高质量软件的有效手段。在过去的几年里,我认为先编写测试再编写实现是无稽之谈。现在看起来我错了。现在我发现这是一个很伟大的方法。我现在推崇测试驱动的开发方式是因为我发现使用这种方法可以极大地加速软件开发过程。       首先,在test/dao/**/dao目录下创建一个PersonDAOTest.java对象。这个对象必须继承 BaseDAOTestCase对象。这个父对象用来加载一个Spring的.properties文件 (资源邦定文件),此文件的文件名和*Test.class一样。在这个例子里面如果你把PersonDAOTest.properties、PersonDAOTest.java放在一个目录下面, 这个文件的属性可以自动被赋值。   500)this.width=500'>package org.myApp.dao;500)this.width=500'>500)this.width=500'>import org.myApp.model.Person;500)this.width=500'>import org.springframework.dao.DataAccessException;500)this.width=500'>500)this.width=500'>500)this.width=500'>public class PersonDAOTest extends BaseDAOTestCase 500)this.width=500'>{500)this.width=500'>    500)this.width=500'>    private Person person = null;500)this.width=500'>    private PersonDAO dao = null;500)this.width=500'>500)this.width=500'>500)this.width=500'>    protected void setUp() throws Exception 500)this.width=500'>{500)this.width=500'>        super.setUp();500)this.width=500'>        dao = (PersonDAO) ctx.getBean("personDAO");500)this.width=500'>    }500)this.width=500'>500)this.width=500'>500)this.width=500'>    protected void tearDown() throws Exception 500)this.width=500'>{500)this.width=500'>        super.tearDown();500)this.width=500'>        dao = null;500)this.width=500'>    }500)this.width=500'>}  500)this.width=500'>500)this.width=500'>           现在我们需要编写代码测试DAO对象中的CRUD (create, retrieve, update, delete)方法。我们创建的这个方法必须以"test" (all 全部小写字母)开头。只要这些方法是public类型的,它必须是一个void类型返回值,并且不带任何参数,这些测试方法将会被定义在Ant build.xml的<junit>任务自动调用。这是一个对CRUD方法的简单测试方法。一个重要的需要记住的事情是每个方法应该是独立的。在这个例子里我们在PersonDAOTest.java文件中加入下面的代码:      500)this.width=500'>500)this.width=500'>public void testGetPerson() throws Exception 500)this.width=500'>{500)this.width=500'>        person = new Person();500)this.width=500'>        person.setFirstName("Matt");500)this.width=500'>        person.setLastName("Raible");500)this.width=500'>500)this.width=500'>        dao.savePerson(person);500)this.width=500'>        assertNotNull(person.getId());500)this.width=500'>500)this.width=500'>        person = dao.getPerson(person.getId());500)this.width=500'>        assertEquals(person.getFirstName(), "Matt");500)this.width=500'>    }500)this.width=500'>500)this.width=500'>500)this.width=500'>    public void testSavePerson() throws Exception 500)this.width=500'>{500)this.width=500'>        person = dao.getPerson(new Long(1));500)this.width=500'>        person.setFirstName("Matt");500)this.width=500'>500)this.width=500'>        person.setLastName("Last Name Updated");500)this.width=500'>500)this.width=500'>        dao.savePerson(person);500)this.width=500'>500)this.width=500'>500)this.width=500'>        if (log.isDebugEnabled()) 500)this.width=500'>{500)this.width=500'>            log.debug("updated Person: " + person);500)this.width=500'>        }500)this.width=500'>500)this.width=500'>        assertEquals(person.getLastName(), "Last Name Updated");500)this.width=500'>    }500)this.width=500'>500)this.width=500'>
 

阅读全文(1522) | 回复(0) | 编辑 | 精华
 

发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)
 
部落窝Blog模板世界部落窝Blog模板世界
站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.049 second(s), page refreshed 144786103 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号