分析zooma敏捷框架的优点和缺陷

最初想写zooma的原因是因为自己感到对经常性的重复劳动的厌烦,诸如一些po和vo的转换,针对不同entity的dao和service的实现,以及基本的action中crud操作,即使对dao和service以及action做了泛型的封装,还是要写不少的重复代码,前台的页面也是要来回的粘贴复制,于是我就有了zooma的原始模型:

就是通过hibernate的mapping文件自动的生成对entity的包括po,vo,dao,service,action,actionform和前台的页面,一次性生成对该实体的包括c、s两端的基本操作代码,当然包括spring和struts的配置文件的更新,使得我可以把大部分的精力放在对于特定实体的特定逻辑的设计上。

到zooma0.3版本,以上的模型已经全部实现,使用zooma为基础的山西某煤矿生产调度信息系统也同时完成收尾工作,在开发过程中遇到了zooma的先天缺陷,在后面会给大家介绍,希望使用它的人能够避免犯同样的错误。下面我来分析一下zooma的优点和缺点:

首先我对后台的代码做了优化,使用泛型对dao,service和action做了基本操作的封装,这样可以看到dao实现的代码如下:

public class ZsxResumeDAO extends BabyDao <ZsxResume,Integer>{

}

service实现的代码如下:
(1)oracle下的代码

public class ZsxResumeService extends BabyService<ZsxResumeDAO,ZsxResume,ZsxResumeVO>{

}

(2)mysql下的代码

public class ZsxResumeService extends BabyService<ZsxResumeDAO,ZsxResume,ZsxResumeVO>{

public boolean delete(String[] ids){
try {
for(int i=0;i<=ids.length;i++){
this.getDao().delete(Integer.parseInt(ids[i]));
}
return true;
} catch (Exception e) {
return false;
}
}
public ZsxResumeVO findById(String id) {
CoypUtil.copyProperties(this.getVo(),this.getDao().findById(Integer.parseInt(id)));
return this.getVo();
}

}


action实现的代码:

public class ZsxResumeAllAction extends BabyActionAdvan<ZsxResumeService,ZsxResume,ZsxResumeVO,ZsxResumeForm>{
/**
* Struts execute
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String action=request.getParameter("action");
String actionForward="404Failed";
if ("insert".equals(action)) {
/*insert表示插入,由xxx_add.jsp跳入,跳转xxx_index.jsp*/
if (insert(form))actionForward="zsxResume_insert";
}else if("update".equals(action)) {
/*update表示修改,由xxx_update.jsp跳入,跳转xxx_index.jsp*/
if(update(form))actionForward="zsxResume_update";//xxx_index.jsp
}else if("show".equals(action)) {
/*show表示修改显示,由xxx_index.jsp或xxx_query.jsp跳入,跳转xxx_update.jsp*/
String id=request.getParameter("id");
vo= (ZsxResumeVO)(service).findById(id);
Vector<ZsxResumeVO> v=new Vector<ZsxResumeVO>();
v.add(vo);
request.setAttribute("result",v );
actionForward="zsxResume_show";
}else if("delete".equals(action)) {
/*delete表示删除,由xxx_index.jsp或xxx_query.jsp跳入,跳转xxx_index.jsp或xxx_query.jsp*/
String[] ids=request.getParameterValues("id");
if (!delete(ids))actionForward="zsxResume_delete";
}else{
/*query,由xxx_index.jsp或xxx_query.jsp跳入*/
Page p=query(form, request, "zsxResume");/*第三个参数需要相应修改,值为当前action的path名称*/
request.setAttribute("result",p.getItems());
request.setAttribute("page",p);
/*判断是主页跳入,还是查询页跳入*/
if ("index".equals(this.form)){
//主页跳入,应该跳回主页
actionForward = "zsxResume_index";//xxx_index.jsp
}else if("query".equals(this.form)){
//查询页跳入,应该跳回查询页
actionForward = "zsxResume_query";//xxx_query.jsp
}
}
return mapping.findForward(actionForward);
}
/**
* overrided begin
*/

/**
* ActionForm to VO
*/
public void copyForm(ZsxResumeForm form) {
ZsxResumeForm f = form;

if(f.getId()!=null) {
if(!f.getId().equals(""))vo.setId(new Integer(f.getId()));
}

if(f.getAge()!=null) {
if(!f.getAge().equals(""))vo.setAge(new Integer(f.getAge()));
}

if(f.getSalary()!=null) {
if(!f.getSalary().equals(""))vo.setSalary(new Float(f.getSalary()));
}

if(f.getBrithday()!=null) {
if(!f.getBrithday().equals(""))vo.setBrithday(DateUtil.stringToDate(f.getBrithday()));
}

vo.setName(f.getName());

vo.setCity(f.getCity());

}
/**
* query rules
*/
public DetachedCriteria getDetachedCriteria(ZsxResume po){
//建立查询规则
DetachedCriteria dc=DetachedCriteria.forClass(ZsxResume.class);

if(po.getName()!=null&&!po.getName().equals("")){
dc.add(Restrictions.like("name",po.getName(),MatchMode.ANYWHERE));
}

if(po.getCity()!=null&&!po.getCity().equals("")){
dc.add(Restrictions.like("city",po.getCity(),MatchMode.ANYWHERE));
}

if(po.getId()!=null&&!po.getId().equals("")){
dc.add(Restrictions.eq("id",new Integer(po.getId())));
}

if(po.getAge()!=null&&!po.getAge().equals("")){
dc.add(Restrictions.eq("age",new Integer(po.getAge())));
}

if(po.getSalary()!=null&&!po.getSalary().equals("")){
dc.add(Restrictions.eq("salary",new Float(po.getSalary())));
}

if(po.getBrithday()!=null&&!po.getBrithday().equals("")){
dc.add(Restrictions.eq("brithday",po.getBrithday()));
}

dc.addOrder(Order.desc("id"));
return dc;
}
}


这些代码相对在量上有了明显的压缩,也更加的适合扩展,比如我希望在dao中添加一个按照name来查询的方法,直接在dao里添加如下方法就可以了:

public List findByName(String name){
return this.getHibernateTemplate().find("from ZsxResume where name=?",name);
}

再比如我想使用其它实体的操作,只要在这个实体中用geter和seter注入就可以使用,当然相应的要在spring的配置文件中加入参数信息。

在action中只实现了单独实体的crud操作,想实现批量操作,可以自己重写insert(),delete(),update()方法,同样也可以通过ioc使用其他实体的crud操作
在查询方面使用hibernate的QBC进行动态混合查询,查询规则在action中的getDetachedCriteria方法中定义,同时将分页封装了进去,可以自己修改org.zooma.base.Page类的toString()方法,来改变翻页显示条。

页面中尽量将java代码最少化,使得代码看起来更加易懂,所以使用了struts的标签,[b]这里有个缺陷就是[/b]页面上按钮使用的js跳转,所以大家可以看到用我提供的模板生成的jsp文件需要使用js,css和images三个文件夹中的文件。

在WEB-INF\templete下存放的是xml和xsl模板,zooma会先将mapping文件里的信息保存在xml\bean.xml中,然后使用它和xsl文件夹下的模板进行文件生成,一次前台页面的生成样式是完全可以自己定义的,只要按照bean.xml中的数据进行编写就可以了,当然名字还是要叫那个名字。

[b]zooma的缺陷:[/b]

在项目中我是这样使用zooma的,单表直接只用zooma生成就ok了,多表关联的时候,先建立视图,然后对视图做生成——这里对表和视图有一定的限制,就是一定要以id为主键,并且id在oracle中使用number、在mysql中使用integer,否则会出错误,这个应该算是个缺陷吧——生成的视图用来显示关联后的实体数据,对视图修改的时候,却修改要被修改的被关联的单表,如果说对于比较简单的逻辑关系这样操作还是没有问题的,但是我们在实际项目中遇到的比较复杂的逻辑,使得实现的十分复杂,所以建议对多表关联使用zooma对单边生成后自己修改mapping文件和po、vo,使用hibernate的many-to-many进行操作,这是zooma得最大缺陷,它只能为开发者在比较简单的实体逻辑(一般我会在这样的逻辑中使用zooma:单表,many-to-one,one-to-many)下生成应用,使用它来做一些小型项目我绝得还是会很有益处的。

还有就是zooma是基于struts1.2的,这令很多网友感到失望,我在演示视频中使用myeclipse进行项目搭建,有让很多人感到绝望,实际上完全可以除了基本架构的搭建,其他操作完全可以不使用myeclipse,但是需要自己手动写mapping文件和po。

在zooma的下个版本中希望实现:
1、ant的项目导入,摆脱Myeclipse
2、升级为Struts2.0,有可能的话会添加ibates支持
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值