利用ejb创建crm系统
林建刚、聂旭琰、薛瑛
本文主要是利用ejb(enterprise javabean)技术来开发一个crm系统,整个系统应用j2ee构架,以ibm websphere application server作为应用服务器来进行开发的。本文将介绍整个系统的构架以及整个系统中ejb的分析设计和实现。
系统结构
本系统运用了当今流行的j2ee构架,将整个crm系统分成四层,即客户层、web层、ejb层和数据层。其中ejb层主要处理系统的业务逻辑,系统运用session facade设计模式即利用session bean包装所有entity bean,来负责调用entity bean的方法,客户端只允许与session bean交互,这样可以缩短系统响应时间,减少资源利用。entity bean用来代表数据库中的数据,所有对entity bean的操作都代表了底层数据库中数据的变动,entitybean与数据库的同步过程由容器来管理。session bean用来处理业务逻辑和工作流,是客户端工作的抽象。由web层来处理企业的表示逻辑,用来处理与ejb层和客户端之间的交互,包括接收、响应客户端的请求,并向ejb层发出请求,接收它的响应结果。
系统以ibm vasualage for java作为开发工具,以ibm db2作为后台数据库,利用ibm websphere application server作为应用服务器。下图就是整个系统的结构图。
客户层(client tier):包括浏览器、笔记本等客户端。
web层(web tier):使用jsp(java server pages)来开发业务逻辑。
业务层(也叫ejb层):webserver / appserver,使用ibm http server 作为http server,ibm websphere server作为应用服务器。
数据层(data tier):使用ibm db2存储企业数据。
ejb设计
下面,我们将详细介绍系统中所需的实体ejb与会话bean的功能。
实体ejb设计
使用了ejb的实体bean类型,所有的数据都封装到实体ejb中,因此数据库设计就成了ejb的设计。对应于上面的数据库设计,这里可以抽象出来以下数据实体:
systemmanagerejb:主要用来描述整个系统的管理员信息的实体bean;
customerejb:主要用来描述整个系统的客户信息的实体bean;
contactejb:主要用来描述与客户接触洽谈的有关信息的相关资料的实体bean;
customerserviceejb:主要用来描述企业对客户提供服务的相关信息的实体bean;
productpurchaseejb:主要用来描述客户向企业购买产品的有关信息的实体bean;
produtcheckoutejb:主要用来描述客户向企业购买产品后,企业的产品出库的相关信息的实体bean;
productstorageejb:主要用来企业向销售厂家购买产品后,产品入库的相关信息的实体bean。
会话bean设计
会话bean(session bean)主要完成一些逻辑处理,包括以下会话bean :
facadeejb:主要用来封装所有实体bean;
statproductejb:主要是用来统计库存产品的会话bean;
statcustomerejb:主要是用来统计客户信息的会话bean。
程序示例
下面,笔者以customerejb为例,简述程序的开发过程:
编写home接口
home接口是用来创建、定位和删除对象的,每个实体bean都有自己的home接口。下面的代码包含了customerhome接口的具体代码:
import java.rmi.;
import javax.ejb.;
import java.util.;
public interface customerhome extends ejbhome {
public customer create(
string customername,
string customerpassword,
string customersex,
string customerdepartment,
string customerinfo)
throws remoteexception,createexception;
public customer findbyprimarykey(string primarykey)
throws remoteexception,finderexception;
public collection findall() throws remoteexception, finderexception;
……
}
编写remote接口
远程接口定义了客户端和bean交互使用的商务方法。下面的代码显示了customer接口的定义。
import java.rmi.;
import javax.ejb.;
public interface customer extends ejbobject {
string getcustomername() throws remoteexception;
void setcustomername(string customername) throws remoteexception;
……
}
编写bean类
开发任何bean都要以它的远程接口作向导。远程接口中定义的商务方法必须复制到bean类中。在一个cmp中,bean类必须有与home接口中的创建方法对应的方法.最后还必须实现javax.ejb.entitybean接口中定义的回调方法。下面是customerbean类的代码示例:
import java.rmi.;
import javax.ejb.;
public abstract class userinfobean implements entitybean {
entitycontext entitycontext;
string customername;
……
public customer ejbcreate(
string customername,
string customerpassword,
string customersex,
string customerdepartment,
string customerinfo)
throws remoteexception,createexception {
this.customername = customername;
……
}
public void ejbremove()
throws removeexception {
}
…… //其他回调方法
public string getcustomername(){
return customername;
}
…… //其他商务方法
}
在会话bean中包装实体
在facadeejb中包装实体bean,以便客户端调用。
下面代码演示了如何在会话bean的一个商务方法中包装实体bean:
public collection getallrecords(){
vector vectuserinfos = new vector();
try {
context ctx = new initialcontext();
object ref = ctx.lookup("customerhome");
customerhome customerhome = (customerhome) portableremoteobject.narrow(ref, customerhome.class);
collection collection = customerhome.findall();
return collection;
} catch(exception e) {
e.printstacktrace();
}
}
最后,我们只要在jsp页面中调用整个customer的数据资料即可。完成其他的设计工作后,一个用ejb技术开发的crm系统就初具规模了。
