Jason Rudolph 关于Groovy的新教程:Grails + EJB Step by Step (前言)

本文转引自http://www.infoq.com/cn/articles/grails-ejb-tutorial
作者 Jason Rudolph 译者 胡键
这是grails很好的一个入门教程,对比JAVA应用可以省略大量的代码(下面是前言部分,转引者略有改动)
前言
Ruby on Rails不断地受到软件工程世界的关注,但企业依旧对其表示怀疑。为什么会这样?我们怀疑:“构建于脚本语言之上的框架,怎能适合我的企业应用?!”针对Ruby on Rails,典型的论调就是缺少对企业服务(如分布式事务、消息传递等)的支持。对很多企业而言,如果平台没有这些服务,那么它将不可能被考虑。

Grails旨在解决那些关注点,并证明快速应用开发(RAD)对企业是可行的。Grails建构于Groovy之上,提供了与Java的无缝集成。它能直接访问你的业务所依赖的那些企业服务,同时为你的工具集增添强大的动态语言结构。

作为展示它企业集成能力的令人印象深刻的一个例子,Grails可让你快速而简单的基于已有EJB3实体Bean构建一个Web应用。但是,它的能力并不是仅此而已。Grails大幅增强了你的实体Bean的能力,而且这些完全是动态做到的,没有更改任何你的EJB源码。Grails对象关系映射(GORM)建立在Hibernate3(最终将提供对Java持久化API的支持)之上,利用Groovy的元对象协议(MOP)为你的不同静态实体Bean增加了各种方便的动态方法。这些方法不仅能从Grails和Groovy访问,而且你的Java代码同样能访问它们!我们瞬间就拥有了JEE/EJB3的企业级能力和RAD Web应用开发的全部好处!

那么,让我们看看基于EJB3实体Bean构建Grails应用要做哪些工作。在以下步骤中,我们将创建一个新的Grails应用,将实体Bean导入到应用,为实体Bean产生快速构建缺省Web界面的脚手架代码(scaffolding),然后再探索Grails为实体Bean增加的一些方便的动态方法。

首先,我们需要从一个EJB3应用开始。(Grails并不需要你以一个EJB3应用作为起点。但是,本文的一般性假设是你有兴趣将RAD Web开发并入你的EJB3项目。)假设我们有一些EJB3实体Bean,它们代表一个公司里的员工(EmployeeBean)和分配给员工的计算机(ComputerBean)。(如果遇到任何问题,请参见资源小节。你将在那儿找到该Grails应用的完整源代码——一个使用这些实体Bean的简单JEE应用——以及其它有用的东西。)
【附件一为类结构图】


以下是支持我们实体Bean的两张表。(例子中,我们将使用MySQL 5.0。你可以使用这个脚本创建一个名为ejb3example的新数据库,它同时将生成这些表。)
【附件二为表脚本】
构建与这些数据交互的Web用户界面,并和已有代码集成要花多长时间呢?它这不应该花费太多的时间,但是,我们肯定已经接受了这样的观点,即这个过程需要重大的努力。使用Grails,无需如此。
下面是POJO代码
[code]package com.jasonrudolph.ejb3example.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "employees")
public class EmployeeBean implements Serializable {

/** The primary key for this employee. */
private int id;

/** The network ID (i.e., user ID) for this employee. */
private String networkId;

/** The first name for this employee. */
private String firstName;

/** The last name for this employee. */
private String lastName;

/** The date on which this employee began his/her employment. */
private Date startDate;

/** The collection of computers assigned to this employee. */
private Collection<ComputerBean> computers = new ArrayList();

public EmployeeBean() {
}

public EmployeeBean(String networkId, String firstName, String lastName,
Date startDate) {
this.networkId = networkId;
this.firstName = firstName;
this.lastName = lastName;
this.startDate = startDate;
}

/**
* @return the system-generated primary key for this employee
*/
@Id
@GeneratedValue
public int getId() {
return this.id;
}

/**
* Sets the primary key for this employee. Only the EJB container should
* invoke this method.
*
* @param id
* the primary key for this employee
*/
public void setId(int id) {
this.id = id;
}

@Column(name = "network_id")
public String getNetworkId() {
return this.networkId;
}

public void setNetworkId(String networkId) {
this.networkId = networkId;
}

@Column(name = "first_name")
public String getFirstName() {
return this.firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

@Column(name = "last_name")
public String getLastName() {
return this.lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

@Column(name = "start_date")
public Date getStartDate() {
return startDate;
}

public void setStartDate(Date startDate) {
this.startDate = startDate;
}

/**
* @return the collection of computers assigned to this employee
*/
@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, mappedBy = "employeeBean")
public Collection<ComputerBean> getComputers() {
return this.computers;
}

/**
* Sets the collection of computers assigned to this employee.
*
* @param computers
* the collection of computers assigned to this employee
*/
public void setComputers(Collection<ComputerBean> computers) {
this.computers = computers;
}
}[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值