使用XDoclet生成代码

   转EasyJF团队stef_wu的一篇文章!
在最近的一个项目中,使用了hibernate+struts,由于中间没有使用spring来管理bean,所以我使用了DAO+Service来做持久层和业务层。受spring对hibernate的template的封装的影响,我自己实现了一个简单的hibernateTemplate——MHibernateTemplate和调用接口MHibernateCallback。在其中包装了错误拦截等动作。所以我的DAO就不能直接从Myecipse生成了。但由于Domain bean太多,而且DAO中又是简单的代码重复,所以想到了使用代码生成。刚开始想直接在Myeclipse中生成DAO的时候使用自己的模版就可以了,但是在网上找了半天都没有相关的信息。后来模仿easyjtools使用velocity自己做代码生成,但感觉时间不够,所以,就选择了XDoclet。
       网上相关的内容不是很多,所以就只有啃En版的XDoclet in Action。由于我的需求很简单,所以只使用了简单的template(就是.xdt)来生成代码,搞了两天,效果还不错。
       比如一个model:(这个项目的需求比较奇怪,由于要同步,所以每个对象都必须有一个pkid(包括中间表),2就是每个对象有个dr标志,表示删除(而不是真正的删除))。
       package com.my.xdoclet;
/**
  * PubCompproper generated by MyEclipse - Hibernate Tools
  * @hasRef
  * 公司性质
  */
 
public class PubCompproper extends BaseDomain implements java.io.Serializable {
 
    // Fields
    /**
      * @pkid
      */
    private String cproPkid ;
    // 系统类型信息
    /**
      * @ref .model name="sytp"
      */
    private PubSystype sytp ;
    // 公司性质编码
    private String cproCode ;
    // 公司性质名称
    private String cproName ;
    // 删除表示
    /**
      * @del
      */
    private String cproDr ;
    // Constructors
 
    /** default constructor */
    public PubCompproper() {
       this .setCproDr( "0" );
    }
 
    // Property accessors
 
    public String getCproPkid() {
       return this . cproPkid ;
    }
 
    public void setCproPkid(String cproPkid ) {
       this . cproPkid = cproPkid ;
    }
 
    public String getCproCode() {
       return this . cproCode ;
    }
 
    public void setCproCode(String cproCode ) {
       this . cproCode = cproCode ;
    }
 
    public String getCproName() {
       return this . cproName ;
    }
 
    public void setCproName(String cproName ) {
       this . cproName = cproName ;
    }
 
    /**
      * @return the sytp
      */
    public PubSystype getSytp() {
       return sytp ;
    }
 
    /**
      * @param sytp the sytp to set
      */
    public void setSytp(PubSystype sytp ) {
       this . sytp = sytp ;
    }
 
    /**
      * @return the cproDr
      */
    public String getCproDr() {
       return cproDr ;
    }
 
    /**
      * @param cproDr the cproDr to set
      */
    public void setCproDr(String cproDr ) {
       this . cproDr = cproDr ;
    }
}
 
其中有一些相关的对象,在 DAO 中要使用到。
看看模版文件
package com.hycs.bs.client.itf;
 
import java.util.List;
 
import . ;
 
public interface DAO {
    // 添加
    boolean add( instance);
   
   
    // 添加
    boolean add( instance, String pkid );  
   
   
    // 删除
    boolean del(String pkid);
   
    // 更新
    boolean update( instance);
   
    // 列出所有
    List list();
   
    List list(boolean withDr);
   
    // 得到一个对象
    get(String pkid);
}
这个是 interface 的模版,其中的模版标签都很简单易懂;
 
package com.hycs.bs.client.call;
 
import com.hycs.bs.sys.MHibernateTemplate;
import com.hycs.util.Constant;
import com.hycs.util.OidHelper;
import com.hycs.bs.sys.HibernateCodeUtil;
import com.hycs.bs.sys.HibernateUtil;
 
public class DAOImpl implements DAO{
    private MHibernateTemplate template;
   
      
           private final DAO dao = new DAOImpl();
      
   
   
    public DAOImpl(){
       this.template=new MHibernateTemplate(HibernateUtil.getSessionFactory());
    }
   
    public boolean add( instance) {
       // TODO Auto-generated method stub
       // add your code and pkid generhere;
       //instance.setCproCode(HibernateCodeUtil.getLastCode("PubCompproper", "cproCode", "cproPkid"));
       //instance.setCproPkid(OidHelper.oidSingle());
      
       return this.template.save(instance);
    }
   
   
    public boolean add( instance, String pkid ) {
       // TODO Auto-generated method stub
      
          
              =this. dao.get( pkid);
              if( ==null){
                  return false;
              }
              instance.set ( );
          
      
       return this.add(instance);
    }
   
   
    public boolean del(String pkid) {
       // TODO Auto-generated method stub
       instance =this.get(pkid);
       if(instance==null||instance.get ==null){
           return false;
       }
       instance.set (Constant.MODEL_DEL);
       return this.template.update(instance);
    }
   
    public get(String pkid) {
       // TODO Auto-generated method stub
       return ( )this.template.get( .class, pkid);
    }
   
    public List list() {
       // TODO Auto-generated method stub
       return this.list(true);
    }
   
    public List list(boolean withDr) {
       // TODO Auto-generated method stub
       if(withDr){
           return HibernateCodeUtil.listWithDr(" "," " );
       }else{
           return this.template.getAll( .class);
       }
    }
   
    public boolean update( instance) {
       // TODO Auto-generated method stub
      
       temp=this.get(instance.get ());
      
                   
              if(instance.get ()==null){                
                  instance.set (temp.get ());
              }
          
      
      
      
       return this.template.update(instance);
    }
}
这是个比较复杂的模版了,是 DAO 的具体实现,
在写这个模版的时候,我遇到了几个问题,
1 就是标签的嵌套,比如 ,开始内部标签都使用转义符,结果搞不定,网上找没有任何相关的内容,后来直接在外层使用 (就象 js ),搞定。
2 就是对于标签的内容的首字符大写,没有提供这个功能的标签,使用了自定义的:
package com.my.xdoclet.customTags;
 
import java.util.Properties;
 
import xdoclet.XDocletTagSupport;
 
public class UpperName extends XDocletTagSupport {
    public String upperName(Properties attribute){
       String value=attribute.getProperty("value");
       String upper= upper(value);
       return upper;
    }
   
    private static String upper(String value){
       return value.toUpperCase().substring(0,1)+value.substring(1);
    }
}
并在模版文件中使用 来应用就直接能在模版文件中使用 来使用了,
下面是 bulid.xml 文件:
 
name= "xdocletExample" default= "doall" basedir= "." >
    name= "xdoclet.lib.dir" location= "${basedir}/lib" />
    name= "gen.src.dir" location= "${basedir}/target" />
    name= "src.dir" location= "${basedir}/src" />
    name= "template.dir" location= "${basedir}/template" />
    name= "customtag.dir" location= "${basedir}/bin" />
 
    id= "xdoclet.lib.path" >
       dir= "${xdoclet.lib.dir}" includes= "*.jar" />
   
 
    name= "xdoclet" classname= "xdoclet.DocletTask" classpathref= "xdoclet.lib.path" />    
   
    name= "init" />
 
    name= "daogener" depends= "init" >
       destdir= "${gen.src.dir}" >
           dir= "${src.dir}" includes= "**/*.java" />
           templateFile= "${template.dir}/daointerface.xdt" acceptInterfaces= "false" acceptAbstractClasses= "false" destinationfile= "{0}DAO.java" />
      
   
 
    name= "daoimplgener" depends= "init" >
       destdir= "${gen.src.dir}" >
           dir= "${src.dir}" includes= "**/*.java" />
           templateFile= "${template.dir}/daoimpl.xdt" acceptInterfaces= "false" acceptAbstractClasses= "false" destinationfile= "{0}DAOImpl.java" />
      
   
 
    name= "doall" depends= "daogener,daoimplgener" />
 
build 一下:
生成的代码如下:
package com.hycs.bs.client.itf;
 
import java.util.List;
 
import com.my.xdoclet.PubCompproper;
 
public interface PubCompproperDAO {
    // 添加
    boolean add(PubCompproper instance);
    // 添加
    boolean add(PubCompproper instance, String sytppkid);  
    // 删除
    boolean del(String pkid);
    // 更新
    boolean update(PubCompproper instance);
    // 列出所有
    List list();
    List list(boolean withDr);
    // 得到一个对象
    PubCompproper get(String pkid);
}
这个是接口
 
package com.hycs.bs.client.call;
 
import com.hycs.bs.sys.MHibernateTemplate;
import com.hycs.util.Constant;
import com.hycs.util.OidHelper;
import com.hycs.bs.sys.HibernateCodeUtil;
import com.hycs.bs.sys.HibernateUtil;
 
public class PubCompproperDAOImpl implements PubCompproperDAO{
    private MHibernateTemplate template;
           private final com.my.xdoclet.PubSystypeDAO sytpdao = new com.my.xdoclet.PubSystypeDAOImpl();
    public PubCompproperDAOImpl(){
       this.template=new MHibernateTemplate(HibernateUtil.getSessionFactory());
    }
    public boolean add(PubCompproper instance) {
       // TODO Auto-generated method stub
       // add your code and pkid generhere;
       //instance.setCproCode(HibernateCodeUtil.getLastCode("PubCompproper", "cproCode", "cproPkid"));
       //instance.setCproPkid(OidHelper.oidSingle());
       return this.template.save(instance);
    }
    public boolean add(PubCompproper instance, String sytppkid) {
       // TODO Auto-generated method stub
              com.my.xdoclet.PubSystype sytp=this.sytpdao.get(sytppkid);
              if(sytp==null){
                  return false;
              }
              instance.setSytp(sytp);
       return this.add(instance);
    }
    public boolean del(String pkid) {
       // TODO Auto-generated method stub
       PubCompproper instance =this.get(pkid);
       if(instance==null||instance.getCproPkid==null){
           return false;
       }
       instance.setCproDr(Constant.MODEL_DEL);
       return this.template.update(instance);
    }
    public PubCompproper get(String pkid) {
       // TODO Auto-generated method stub
       return (PubCompproper)this.template.get(PubCompproper.class, pkid);
    }
    public List list() {
       // TODO Auto-generated method stub
       return this.list(true);
    }
    public List list(boolean withDr) {
       // TODO Auto-generated method stub
       if(withDr){
           return HibernateCodeUtil.listWithDr("PubCompproper","cproDr" );
       }else{
           return this.template.getAll(PubCompproper.class);
       }
    }
    public boolean update(PubCompproper instance) {
       // TODO Auto-generated method stub
       PubCompproper temp=this.get(instance.getCproPkid());
              if(instance.getSytp()==null){                
                  instance.setSytp(temp.getSytp());
              }
       return this.template.update(instance);
    }
}
这个是代码。
于是我的工作就很简单了,适用 Myeclipse 直接从 DataExplor 中生成 Domain bean 和映射文件,改一下关联,在 domain 中添加必要的 XDoclet 标记, build ,就可以专著于具体的业务了。
但这个代码还有点问题就是当遇到一个类有多个关联对象的时候,在生成的一些方法上,要自己手动增加或者删除一个 ”,” 。这个还要继续学习。
同时 XDoclet 提供了很好的扩展机制,这个也要继续研究。
再次就是我在想 XDoclet 中有没有直接使用标签来定义标签的功能,或者在模版内定义变量??
easyjweb 使用 XDoclet 来生成代码也会是很简单而且稳定的。

 (注:本文作者,EasyJF开源团队 stef_wu,转载请保留作者声明!)




   转EasyJF团队stef_wu的一篇文章!
在最近的一个项目中,使用了hibernate+struts,由于中间没有使用spring来管理bean,所以我使用了DAO+Service来做持久层和业务层。受spring对hibernate的template的封装的影响,我自己实现了一个简单的hibernateTemplate——MHibernateTemplate和调用接口MHibernateCallback。在其中包装了错误拦截等动作。所以我的DAO就不能直接从Myecipse生成了。但由于Domain bean太多,而且DAO中又是简单的代码重复,所以想到了使用代码生成。刚开始想直接在Myeclipse中生成DAO的时候使用自己的模版就可以了,但是在网上找了半天都没有相关的信息。后来模仿easyjtools使用velocity自己做代码生成,但感觉时间不够,所以,就选择了XDoclet。
       网上相关的内容不是很多,所以就只有啃En版的XDoclet in Action。由于我的需求很简单,所以只使用了简单的template(就是.xdt)来生成代码,搞了两天,效果还不错。
       比如一个model:(这个项目的需求比较奇怪,由于要同步,所以每个对象都必须有一个pkid(包括中间表),2就是每个对象有个dr标志,表示删除(而不是真正的删除))。
       package com.my.xdoclet;
/**
  * PubCompproper generated by MyEclipse - Hibernate Tools
  * @hasRef
  * 公司性质
  */
 
public class PubCompproper extends BaseDomain implements java.io.Serializable {
 
    // Fields
    /**
      * @pkid
      */
    private String cproPkid ;
    // 系统类型信息
    /**
      * @ref .model name="sytp"
      */
    private PubSystype sytp ;
    // 公司性质编码
    private String cproCode ;
    // 公司性质名称
    private String cproName ;
    // 删除表示
    /**
      * @del
      */
    private String cproDr ;
    // Constructors
 
    /** default constructor */
    public PubCompproper() {
       this .setCproDr( "0" );
    }
 
    // Property accessors
 
    public String getCproPkid() {
       return this . cproPkid ;
    }
 
    public void setCproPkid(String cproPkid ) {
       this . cproPkid = cproPkid ;
    }
 
    public String getCproCode() {
       return this . cproCode ;
    }
 
    public void setCproCode(String cproCode ) {
       this . cproCode = cproCode ;
    }
 
    public String getCproName() {
       return this . cproName ;
    }
 
    public void setCproName(String cproName ) {
       this . cproName = cproName ;
    }
 
    /**
      * @return the sytp
      */
    public PubSystype getSytp() {
       return sytp ;
    }
 
    /**
      * @param sytp the sytp to set
      */
    public void setSytp(PubSystype sytp ) {
       this . sytp = sytp ;
    }
 
    /**
      * @return the cproDr
      */
    public String getCproDr() {
       return cproDr ;
    }
 
    /**
      * @param cproDr the cproDr to set
      */
    public void setCproDr(String cproDr ) {
       this . cproDr = cproDr ;
    }
}
 
其中有一些相关的对象,在 DAO 中要使用到。
看看模版文件
package com.hycs.bs.client.itf;
 
import java.util.List;
 
import . ;
 
public interface DAO {
    // 添加
    boolean add( instance);
   
   
    // 添加
    boolean add( instance, String pkid );  
   
   
    // 删除
    boolean del(String pkid);
   
    // 更新
    boolean update( instance);
   
    // 列出所有
    List list();
   
    List list(boolean withDr);
   
    // 得到一个对象
    get(String pkid);
}
这个是 interface 的模版,其中的模版标签都很简单易懂;
 
package com.hycs.bs.client.call;
 
import com.hycs.bs.sys.MHibernateTemplate;
import com.hycs.util.Constant;
import com.hycs.util.OidHelper;
import com.hycs.bs.sys.HibernateCodeUtil;
import com.hycs.bs.sys.HibernateUtil;
 
public class DAOImpl implements DAO{
    private MHibernateTemplate template;
   
      
           private final DAO dao = new DAOImpl();
      
   
   
    public DAOImpl(){
       this.template=new MHibernateTemplate(HibernateUtil.getSessionFactory());
    }
   
    public boolean add( instance) {
       // TODO Auto-generated method stub
       // add your code and pkid generhere;
       //instance.setCproCode(HibernateCodeUtil.getLastCode("PubCompproper", "cproCode", "cproPkid"));
       //instance.setCproPkid(OidHelper.oidSingle());
      
       return this.template.save(instance);
    }
   
   
    public boolean add( instance, String pkid ) {
       // TODO Auto-generated method stub
      
          
              =this. dao.get( pkid);
              if( ==null){
                  return false;
              }
              instance.set ( );
          
      
       return this.add(instance);
    }
   
   
    public boolean del(String pkid) {
       // TODO Auto-generated method stub
       instance =this.get(pkid);
       if(instance==null||instance.get ==null){
           return false;
       }
       instance.set (Constant.MODEL_DEL);
       return this.template.update(instance);
    }
   
    public get(String pkid) {
       // TODO Auto-generated method stub
       return ( )this.template.get( .class, pkid);
    }
   
    public List list() {
       // TODO Auto-generated method stub
       return this.list(true);
    }
   
    public List list(boolean withDr) {
       // TODO Auto-generated method stub
       if(withDr){
           return HibernateCodeUtil.listWithDr(" "," " );
       }else{
           return this.template.getAll( .class);
       }
    }
   
    public boolean update( instance) {
       // TODO Auto-generated method stub
      
       temp=this.get(instance.get ());
      
                   
              if(instance.get ()==null){                
                  instance.set (temp.get ());
              }
          
      
      
      
       return this.template.update(instance);
    }
}
这是个比较复杂的模版了,是 DAO 的具体实现,
在写这个模版的时候,我遇到了几个问题,
1 就是标签的嵌套,比如 ,开始内部标签都使用转义符,结果搞不定,网上找没有任何相关的内容,后来直接在外层使用 (就象 js ),搞定。
2 就是对于标签的内容的首字符大写,没有提供这个功能的标签,使用了自定义的:
package com.my.xdoclet.customTags;
 
import java.util.Properties;
 
import xdoclet.XDocletTagSupport;
 
public class UpperName extends XDocletTagSupport {
    public String upperName(Properties attribute){
       String value=attribute.getProperty("value");
       String upper= upper(value);
       return upper;
    }
   
    private static String upper(String value){
       return value.toUpperCase().substring(0,1)+value.substring(1);
    }
}
并在模版文件中使用 来应用就直接能在模版文件中使用 来使用了,
下面是 bulid.xml 文件:
 
name= "xdocletExample" default= "doall" basedir= "." >
    name= "xdoclet.lib.dir" location= "${basedir}/lib" />
    name= "gen.src.dir" location= "${basedir}/target" />
    name= "src.dir" location= "${basedir}/src" />
    name= "template.dir" location= "${basedir}/template" />
    name= "customtag.dir" location= "${basedir}/bin" />
 
    id= "xdoclet.lib.path" >
       dir= "${xdoclet.lib.dir}" includes= "*.jar" />
   
 
    name= "xdoclet" classname= "xdoclet.DocletTask" classpathref= "xdoclet.lib.path" />    
   
    name= "init" />
 
    name= "daogener" depends= "init" >
       destdir= "${gen.src.dir}" >
           dir= "${src.dir}" includes= "**/*.java" />
           templateFile= "${template.dir}/daointerface.xdt" acceptInterfaces= "false" acceptAbstractClasses= "false" destinationfile= "{0}DAO.java" />
      
   
 
    name= "daoimplgener" depends= "init" >
       destdir= "${gen.src.dir}" >
           dir= "${src.dir}" includes= "**/*.java" />
           templateFile= "${template.dir}/daoimpl.xdt" acceptInterfaces= "false" acceptAbstractClasses= "false" destinationfile= "{0}DAOImpl.java" />
      
   
 
    name= "doall" depends= "daogener,daoimplgener" />
 
build 一下:
生成的代码如下:
package com.hycs.bs.client.itf;
 
import java.util.List;
 
import com.my.xdoclet.PubCompproper;
 
public interface PubCompproperDAO {
    // 添加
    boolean add(PubCompproper instance);
    // 添加
    boolean add(PubCompproper instance, String sytppkid);  
    // 删除
    boolean del(String pkid);
    // 更新
    boolean update(PubCompproper instance);
    // 列出所有
    List list();
    List list(boolean withDr);
    // 得到一个对象
    PubCompproper get(String pkid);
}
这个是接口
 
package com.hycs.bs.client.call;
 
import com.hycs.bs.sys.MHibernateTemplate;
import com.hycs.util.Constant;
import com.hycs.util.OidHelper;
import com.hycs.bs.sys.HibernateCodeUtil;
import com.hycs.bs.sys.HibernateUtil;
 
public class PubCompproperDAOImpl implements PubCompproperDAO{
    private MHibernateTemplate template;
           private final com.my.xdoclet.PubSystypeDAO sytpdao = new com.my.xdoclet.PubSystypeDAOImpl();
    public PubCompproperDAOImpl(){
       this.template=new MHibernateTemplate(HibernateUtil.getSessionFactory());
    }
    public boolean add(PubCompproper instance) {
       // TODO Auto-generated method stub
       // add your code and pkid generhere;
       //instance.setCproCode(HibernateCodeUtil.getLastCode("PubCompproper", "cproCode", "cproPkid"));
       //instance.setCproPkid(OidHelper.oidSingle());
       return this.template.save(instance);
    }
    public boolean add(PubCompproper instance, String sytppkid) {
       // TODO Auto-generated method stub
              com.my.xdoclet.PubSystype sytp=this.sytpdao.get(sytppkid);
              if(sytp==null){
                  return false;
              }
              instance.setSytp(sytp);
       return this.add(instance);
    }
    public boolean del(String pkid) {
       // TODO Auto-generated method stub
       PubCompproper instance =this.get(pkid);
       if(instance==null||instance.getCproPkid==null){
           return false;
       }
       instance.setCproDr(Constant.MODEL_DEL);
       return this.template.update(instance);
    }
    public PubCompproper get(String pkid) {
       // TODO Auto-generated method stub
       return (PubCompproper)this.template.get(PubCompproper.class, pkid);
    }
    public List list() {
       // TODO Auto-generated method stub
       return this.list(true);
    }
    public List list(boolean withDr) {
       // TODO Auto-generated method stub
       if(withDr){
           return HibernateCodeUtil.listWithDr("PubCompproper","cproDr" );
       }else{
           return this.template.getAll(PubCompproper.class);
       }
    }
    public boolean update(PubCompproper instance) {
       // TODO Auto-generated method stub
       PubCompproper temp=this.get(instance.getCproPkid());
              if(instance.getSytp()==null){                
                  instance.setSytp(temp.getSytp());
              }
       return this.template.update(instance);
    }
}
这个是代码。
于是我的工作就很简单了,适用 Myeclipse 直接从 DataExplor 中生成 Domain bean 和映射文件,改一下关联,在 domain 中添加必要的 XDoclet 标记, build ,就可以专著于具体的业务了。
但这个代码还有点问题就是当遇到一个类有多个关联对象的时候,在生成的一些方法上,要自己手动增加或者删除一个 ”,” 。这个还要继续学习。
同时 XDoclet 提供了很好的扩展机制,这个也要继续研究。
再次就是我在想 XDoclet 中有没有直接使用标签来定义标签的功能,或者在模版内定义变量??
easyjweb 使用 XDoclet 来生成代码也会是很简单而且稳定的。

 (注:本文作者,EasyJF开源团队 stef_wu,转载请保留作者声明!)




   转EasyJF团队stef_wu的一篇文章!
在最近的一个项目中,使用了hibernate+struts,由于中间没有使用spring来管理bean,所以我使用了DAO+Service来做持久层和业务层。受spring对hibernate的template的封装的影响,我自己实现了一个简单的hibernateTemplate——MHibernateTemplate和调用接口MHibernateCallback。在其中包装了错误拦截等动作。所以我的DAO就不能直接从Myecipse生成了。但由于Domain bean太多,而且DAO中又是简单的代码重复,所以想到了使用代码生成。刚开始想直接在Myeclipse中生成DAO的时候使用自己的模版就可以了,但是在网上找了半天都没有相关的信息。后来模仿easyjtools使用velocity自己做代码生成,但感觉时间不够,所以,就选择了XDoclet。
       网上相关的内容不是很多,所以就只有啃En版的XDoclet in Action。由于我的需求很简单,所以只使用了简单的template(就是.xdt)来生成代码,搞了两天,效果还不错。
       比如一个model:(这个项目的需求比较奇怪,由于要同步,所以每个对象都必须有一个pkid(包括中间表),2就是每个对象有个dr标志,表示删除(而不是真正的删除))。
       package com.my.xdoclet;
/**
  * PubCompproper generated by MyEclipse - Hibernate Tools
  * @hasRef
  * 公司性质
  */
 
public class PubCompproper extends BaseDomain implements java.io.Serializable {
 
    // Fields
    /**
      * @pkid
      */
    private String cproPkid ;
    // 系统类型信息
    /**
      * @ref .model name="sytp"
      */
    private PubSystype sytp ;
    // 公司性质编码
    private String cproCode ;
    // 公司性质名称
    private String cproName ;
    // 删除表示
    /**
      * @del
      */
    private String cproDr ;
    // Constructors
 
    /** default constructor */
    public PubCompproper() {
       this .setCproDr( "0" );
    }
 
    // Property accessors
 
    public String getCproPkid() {
       return this . cproPkid ;
    }
 
    public void setCproPkid(String cproPkid ) {
       this . cproPkid = cproPkid ;
    }
 
    public String getCproCode() {
       return this . cproCode ;
    }
 
    public void setCproCode(String cproCode ) {
       this . cproCode = cproCode ;
    }
 
    public String getCproName() {
       return this . cproName ;
    }
 
    public void setCproName(String cproName ) {
       this . cproName = cproName ;
    }
 
    /**
      * @return the sytp
      */
    public PubSystype getSytp() {
       return sytp ;
    }
 
    /**
      * @param sytp the sytp to set
      */
    public void setSytp(PubSystype sytp ) {
       this . sytp = sytp ;
    }
 
    /**
      * @return the cproDr
      */
    public String getCproDr() {
       return cproDr ;
    }
 
    /**
      * @param cproDr the cproDr to set
      */
    public void setCproDr(String cproDr ) {
       this . cproDr = cproDr ;
    }
}
 
其中有一些相关的对象,在 DAO 中要使用到。
看看模版文件
package com.hycs.bs.client.itf;
 
import java.util.List;
 
import . ;
 
public interface DAO {
    // 添加
    boolean add( instance);
   
   
    // 添加
    boolean add( instance, String pkid );  
   
   
    // 删除
    boolean del(String pkid);
   
    // 更新
    boolean update( instance);
   
    // 列出所有
    List list();
   
    List list(boolean withDr);
   
    // 得到一个对象
    get(String pkid);
}
这个是 interface 的模版,其中的模版标签都很简单易懂;
 
package com.hycs.bs.client.call;
 
import com.hycs.bs.sys.MHibernateTemplate;
import com.hycs.util.Constant;
import com.hycs.util.OidHelper;
import com.hycs.bs.sys.HibernateCodeUtil;
import com.hycs.bs.sys.HibernateUtil;
 
public class DAOImpl implements DAO{
    private MHibernateTemplate template;
   
      
           private final DAO dao = new DAOImpl();
      
   
   
    public DAOImpl(){
       this.template=new MHibernateTemplate(HibernateUtil.getSessionFactory());
    }
   
    public boolean add( instance) {
       // TODO Auto-generated method stub
       // add your code and pkid generhere;
       //instance.setCproCode(HibernateCodeUtil.getLastCode("PubCompproper", "cproCode", "cproPkid"));
       //instance.setCproPkid(OidHelper.oidSingle());
      
       return this.template.save(instance);
    }
   
   
    public boolean add( instance, String pkid ) {
       // TODO Auto-generated method stub
      
          
              =this. dao.get( pkid);
              if( ==null){
                  return false;
              }
              instance.set ( );
          
      
       return this.add(instance);
    }
   
   
    public boolean del(String pkid) {
       // TODO Auto-generated method stub
       instance =this.get(pkid);
       if(instance==null||instance.get ==null){
           return false;
       }
       instance.set (Constant.MODEL_DEL);
       return this.template.update(instance);
    }
   
    public get(String pkid) {
       // TODO Auto-generated method stub
       return ( )this.template.get( .class, pkid);
    }
   
    public List list() {
       // TODO Auto-generated method stub
       return this.list(true);
    }
   
    public List list(boolean withDr) {
       // TODO Auto-generated method stub
       if(withDr){
           return HibernateCodeUtil.listWithDr(" "," " );
       }else{
           return this.template.getAll( .class);
       }
    }
   
    public boolean update( instance) {
       // TODO Auto-generated method stub
      
       temp=this.get(instance.get ());
      
                   
              if(instance.get ()==null){                
                  instance.set (temp.get ());
              }
          
      
      
      
       return this.template.update(instance);
    }
}
这是个比较复杂的模版了,是 DAO 的具体实现,
在写这个模版的时候,我遇到了几个问题,
1 就是标签的嵌套,比如 ,开始内部标签都使用转义符,结果搞不定,网上找没有任何相关的内容,后来直接在外层使用 (就象 js ),搞定。
2 就是对于标签的内容的首字符大写,没有提供这个功能的标签,使用了自定义的:
package com.my.xdoclet.customTags;
 
import java.util.Properties;
 
import xdoclet.XDocletTagSupport;
 
public class UpperName extends XDocletTagSupport {
    public String upperName(Properties attribute){
       String value=attribute.getProperty("value");
       String upper= upper(value);
       return upper;
    }
   
    private static String upper(String value){
       return value.toUpperCase().substring(0,1)+value.substring(1);
    }
}
并在模版文件中使用 来应用就直接能在模版文件中使用 来使用了,
下面是 bulid.xml 文件:
 
name= "xdocletExample" default= "doall" basedir= "." >
    name= "xdoclet.lib.dir" location= "${basedir}/lib" />
    name= "gen.src.dir" location= "${basedir}/target" />
    name= "src.dir" location= "${basedir}/src" />
    name= "template.dir" location= "${basedir}/template" />
    name= "customtag.dir" location= "${basedir}/bin" />
 
    id= "xdoclet.lib.path" >
       dir= "${xdoclet.lib.dir}" includes= "*.jar" />
   
 
    name= "xdoclet" classname= "xdoclet.DocletTask" classpathref= "xdoclet.lib.path" />    
   
    name= "init" />
 
    name= "daogener" depends= "init" >
       destdir= "${gen.src.dir}" >
           dir= "${src.dir}" includes= "**/*.java" />
           templateFile= "${template.dir}/daointerface.xdt" acceptInterfaces= "false" acceptAbstractClasses= "false" destinationfile= "{0}DAO.java" />
      
   
 
    name= "daoimplgener" depends= "init" >
       destdir= "${gen.src.dir}" >
           dir= "${src.dir}" includes= "**/*.java" />
           templateFile= "${template.dir}/daoimpl.xdt" acceptInterfaces= "false" acceptAbstractClasses= "false" destinationfile= "{0}DAOImpl.java" />
      
   
 
    name= "doall" depends= "daogener,daoimplgener" />
 
build 一下:
生成的代码如下:
package com.hycs.bs.client.itf;
 
import java.util.List;
 
import com.my.xdoclet.PubCompproper;
 
public interface PubCompproperDAO {
    // 添加
    boolean add(PubCompproper instance);
    // 添加
    boolean add(PubCompproper instance, String sytppkid);  
    // 删除
    boolean del(String pkid);
    // 更新
    boolean update(PubCompproper instance);
    // 列出所有
    List list();
    List list(boolean withDr);
    // 得到一个对象
    PubCompproper get(String pkid);
}
这个是接口
 
package com.hycs.bs.client.call;
 
import com.hycs.bs.sys.MHibernateTemplate;
import com.hycs.util.Constant;
import com.hycs.util.OidHelper;
import com.hycs.bs.sys.HibernateCodeUtil;
import com.hycs.bs.sys.HibernateUtil;
 
public class PubCompproperDAOImpl implements PubCompproperDAO{
    private MHibernateTemplate template;
           private final com.my.xdoclet.PubSystypeDAO sytpdao = new com.my.xdoclet.PubSystypeDAOImpl();
    public PubCompproperDAOImpl(){
       this.template=new MHibernateTemplate(HibernateUtil.getSessionFactory());
    }
    public boolean add(PubCompproper instance) {
       // TODO Auto-generated method stub
       // add your code and pkid generhere;
       //instance.setCproCode(HibernateCodeUtil.getLastCode("PubCompproper", "cproCode", "cproPkid"));
       //instance.setCproPkid(OidHelper.oidSingle());
       return this.template.save(instance);
    }
    public boolean add(PubCompproper instance, String sytppkid) {
       // TODO Auto-generated method stub
              com.my.xdoclet.PubSystype sytp=this.sytpdao.get(sytppkid);
              if(sytp==null){
                  return false;
              }
              instance.setSytp(sytp);
       return this.add(instance);
    }
    public boolean del(String pkid) {
       // TODO Auto-generated method stub
       PubCompproper instance =this.get(pkid);
       if(instance==null||instance.getCproPkid==null){
           return false;
       }
       instance.setCproDr(Constant.MODEL_DEL);
       return this.template.update(instance);
    }
    public PubCompproper get(String pkid) {
       // TODO Auto-generated method stub
       return (PubCompproper)this.template.get(PubCompproper.class, pkid);
    }
    public List list() {
       // TODO Auto-generated method stub
       return this.list(true);
    }
    public List list(boolean withDr) {
       // TODO Auto-generated method stub
       if(withDr){
           return HibernateCodeUtil.listWithDr("PubCompproper","cproDr" );
       }else{
           return this.template.getAll(PubCompproper.class);
       }
    }
    public boolean update(PubCompproper instance) {
       // TODO Auto-generated method stub
       PubCompproper temp=this.get(instance.getCproPkid());
              if(instance.getSytp()==null){                
                  instance.setSytp(temp.getSytp());
              }
       return this.template.update(instance);
    }
}
这个是代码。
于是我的工作就很简单了,适用 Myeclipse 直接从 DataExplor 中生成 Domain bean 和映射文件,改一下关联,在 domain 中添加必要的 XDoclet 标记, build ,就可以专著于具体的业务了。
但这个代码还有点问题就是当遇到一个类有多个关联对象的时候,在生成的一些方法上,要自己手动增加或者删除一个 ”,” 。这个还要继续学习。
同时 XDoclet 提供了很好的扩展机制,这个也要继续研究。
再次就是我在想 XDoclet 中有没有直接使用标签来定义标签的功能,或者在模版内定义变量??
easyjweb 使用 XDoclet 来生成代码也会是很简单而且稳定的。

 (注:本文作者,EasyJF开源团队 stef_wu,转载请保留作者声明!)




   转EasyJF团队stef_wu的一篇文章!
在最近的一个项目中,使用了hibernate+struts,由于中间没有使用spring来管理bean,所以我使用了DAO+Service来做持久层和业务层。受spring对hibernate的template的封装的影响,我自己实现了一个简单的hibernateTemplate——MHibernateTemplate和调用接口MHibernateCallback。在其中包装了错误拦截等动作。所以我的DAO就不能直接从Myecipse生成了。但由于Domain bean太多,而且DAO中又是简单的代码重复,所以想到了使用代码生成。刚开始想直接在Myeclipse中生成DAO的时候使用自己的模版就可以了,但是在网上找了半天都没有相关的信息。后来模仿easyjtools使用velocity自己做代码生成,但感觉时间不够,所以,就选择了XDoclet。
       网上相关的内容不是很多,所以就只有啃En版的XDoclet in Action。由于我的需求很简单,所以只使用了简单的template(就是.xdt)来生成代码,搞了两天,效果还不错。
       比如一个model:(这个项目的需求比较奇怪,由于要同步,所以每个对象都必须有一个pkid(包括中间表),2就是每个对象有个dr标志,表示删除(而不是真正的删除))。
       package com.my.xdoclet;
/**
  * PubCompproper generated by MyEclipse - Hibernate Tools
  * @hasRef
  * 公司性质
  */
 
public class PubCompproper extends BaseDomain implements java.io.Serializable {
 
    // Fields
    /**
      * @pkid
      */
    private String cproPkid ;
    // 系统类型信息
    /**
      * @ref .model name="sytp"
      */
    private PubSystype sytp ;
    // 公司性质编码
    private String cproCode ;
    // 公司性质名称
    private String cproName ;
    // 删除表示
    /**
      * @del
      */
    private String cproDr ;
    // Constructors
 
    /** default constructor */
    public PubCompproper() {
       this .setCproDr( "0" );
    }
 
    // Property accessors
 
    public String getCproPkid() {
       return this . cproPkid ;
    }
 
    public void setCproPkid(String cproPkid ) {
       this . cproPkid = cproPkid ;
    }
 
    public String getCproCode() {
       return this . cproCode ;
    }
 
    public void setCproCode(String cproCode ) {
       this . cproCode = cproCode ;
    }
 
    public String getCproName() {
       return this . cproName ;
    }
 
    public void setCproName(String cproName ) {
       this . cproName = cproName ;
    }
 
    /**
      * @return the sytp
      */
    public PubSystype getSytp() {
       return sytp ;
    }
 
    /**
      * @param sytp the sytp to set
      */
    public void setSytp(PubSystype sytp ) {
       this . sytp = sytp ;
    }
 
    /**
      * @return the cproDr
      */
    public String getCproDr() {
       return cproDr ;
    }
 
    /**
      * @param cproDr the cproDr to set
      */
    public void setCproDr(String cproDr ) {
       this . cproDr = cproDr ;
    }
}
 
其中有一些相关的对象,在 DAO 中要使用到。
看看模版文件
package com.hycs.bs.client.itf;
 
import java.util.List;
 
import . ;
 
public interface DAO {
    // 添加
    boolean add( instance);
   
   
    // 添加
    boolean add( instance, String pkid );  
   
   
    // 删除
    boolean del(String pkid);
   
    // 更新
    boolean update( instance);
   
    // 列出所有
    List list();
   
    List list(boolean withDr);
   
    // 得到一个对象
    get(String pkid);
}
这个是 interface 的模版,其中的模版标签都很简单易懂;
 
package com.hycs.bs.client.call;
 
import com.hycs.bs.sys.MHibernateTemplate;
import com.hycs.util.Constant;
import com.hycs.util.OidHelper;
import com.hycs.bs.sys.HibernateCodeUtil;
import com.hycs.bs.sys.HibernateUtil;
 
public class DAOImpl implements DAO{
    private MHibernateTemplate template;
   
      
           private final DAO dao = new DAOImpl();
      
   
   
    public DAOImpl(){
       this.template=new MHibernateTemplate(HibernateUtil.getSessionFactory());
    }
   
    public boolean add( instance) {
       // TODO Auto-generated method stub
       // add your code and pkid generhere;
       //instance.setCproCode(HibernateCodeUtil.getLastCode("PubCompproper", "cproCode", "cproPkid"));
       //instance.setCproPkid(OidHelper.oidSingle());
      
       return this.template.save(instance);
    }
   
   
    public boolean add( instance, String pkid ) {
       // TODO Auto-generated method stub
      
          
              =this. dao.get( pkid);
              if( ==null){
                  return false;
              }
              instance.set ( );
          
      
       return this.add(instance);
    }
   
   
    public boolean del(String pkid) {
       // TODO Auto-generated method stub
       instance =this.get(pkid);
       if(instance==null||instance.get ==null){
           return false;
       }
       instance.set (Constant.MODEL_DEL);
       return this.template.update(instance);
    }
   
    public get(String pkid) {
       // TODO Auto-generated method stub
       return ( )this.template.get( .class, pkid);
    }
   
    public List list() {
       // TODO Auto-generated method stub
       return this.list(true);
    }
   
    public List list(boolean withDr) {
       // TODO Auto-generated method stub
       if(withDr){
           return HibernateCodeUtil.listWithDr(" "," " );
       }else{
           return this.template.getAll( .class);
       }
    }
   
    public boolean update( instance) {
       // TODO Auto-generated method stub
      
       temp=this.get(instance.get ());
      
                   
              if(instance.get ()==null){                
                  instance.set (temp.get ());
              }
          
      
      
      
       return this.template.update(instance);
    }
}
这是个比较复杂的模版了,是 DAO 的具体实现,
在写这个模版的时候,我遇到了几个问题,
1 就是标签的嵌套,比如 ,开始内部标签都使用转义符,结果搞不定,网上找没有任何相关的内容,后来直接在外层使用 (就象 js ),搞定。
2 就是对于标签的内容的首字符大写,没有提供这个功能的标签,使用了自定义的:
package com.my.xdoclet.customTags;
 
import java.util.Properties;
 
import xdoclet.XDocletTagSupport;
 
public class UpperName extends XDocletTagSupport {
    public String upperName(Properties attribute){
       String value=attribute.getProperty("value");
       String upper= upper(value);
       return upper;
    }
   
    private static String upper(String value){
       return value.toUpperCase().substring(0,1)+value.substring(1);
    }
}
并在模版文件中使用 来应用就直接能在模版文件中使用 来使用了,
下面是 bulid.xml 文件:
 
name= "xdocletExample" default= "doall" basedir= "." >
    name= "xdoclet.lib.dir" location= "${basedir}/lib" />
    name= "gen.src.dir" location= "${basedir}/target" />
    name= "src.dir" location= "${basedir}/src" />
    name= "template.dir" location= "${basedir}/template" />
    name= "customtag.dir" location= "${basedir}/bin" />
 
    id= "xdoclet.lib.path" >
       dir= "${xdoclet.lib.dir}" includes= "*.jar" />
   
 
    name= "xdoclet" classname= "xdoclet.DocletTask" classpathref= "xdoclet.lib.path" />    
   
    name= "init" />
 
    name= "daogener" depends= "init" >
       destdir= "${gen.src.dir}" >
           dir= "${src.dir}" includes= "**/*.java" />
           templateFile= "${template.dir}/daointerface.xdt" acceptInterfaces= "false" acceptAbstractClasses= "false" destinationfile= "{0}DAO.java" />
      
   
 
    name= "daoimplgener" depends= "init" >
       destdir= "${gen.src.dir}" >
           dir= "${src.dir}" includes= "**/*.java" />
           templateFile= "${template.dir}/daoimpl.xdt" acceptInterfaces= "false" acceptAbstractClasses= "false" destinationfile= "{0}DAOImpl.java" />
      
   
 
    name= "doall" depends= "daogener,daoimplgener" />
 
build 一下:
生成的代码如下:
package com.hycs.bs.client.itf;
 
import java.util.List;
 
import com.my.xdoclet.PubCompproper;
 
public interface PubCompproperDAO {
    // 添加
    boolean add(PubCompproper instance);
    // 添加
    boolean add(PubCompproper instance, String sytppkid);  
    // 删除
    boolean del(String pkid);
    // 更新
    boolean update(PubCompproper instance);
    // 列出所有
    List list();
    List list(boolean withDr);
    // 得到一个对象
    PubCompproper get(String pkid);
}
这个是接口
 
package com.hycs.bs.client.call;
 
import com.hycs.bs.sys.MHibernateTemplate;
import com.hycs.util.Constant;
import com.hycs.util.OidHelper;
import com.hycs.bs.sys.HibernateCodeUtil;
import com.hycs.bs.sys.HibernateUtil;
 
public class PubCompproperDAOImpl implements PubCompproperDAO{
    private MHibernateTemplate template;
           private final com.my.xdoclet.PubSystypeDAO sytpdao = new com.my.xdoclet.PubSystypeDAOImpl();
    public PubCompproperDAOImpl(){
       this.template=new MHibernateTemplate(HibernateUtil.getSessionFactory());
    }
    public boolean add(PubCompproper instance) {
       // TODO Auto-generated method stub
       // add your code and pkid generhere;
       //instance.setCproCode(HibernateCodeUtil.getLastCode("PubCompproper", "cproCode", "cproPkid"));
       //instance.setCproPkid(OidHelper.oidSingle());
       return this.template.save(instance);
    }
    public boolean add(PubCompproper instance, String sytppkid) {
       // TODO Auto-generated method stub
              com.my.xdoclet.PubSystype sytp=this.sytpdao.get(sytppkid);
              if(sytp==null){
                  return false;
              }
              instance.setSytp(sytp);
       return this.add(instance);
    }
    public boolean del(String pkid) {
       // TODO Auto-generated method stub
       PubCompproper instance =this.get(pkid);
       if(instance==null||instance.getCproPkid==null){
           return false;
       }
       instance.setCproDr(Constant.MODEL_DEL);
       return this.template.update(instance);
    }
    public PubCompproper get(String pkid) {
       // TODO Auto-generated method stub
       return (PubCompproper)this.template.get(PubCompproper.class, pkid);
    }
    public List list() {
       // TODO Auto-generated method stub
       return this.list(true);
    }
    public List list(boolean withDr) {
       // TODO Auto-generated method stub
       if(withDr){
           return HibernateCodeUtil.listWithDr("PubCompproper","cproDr" );
       }else{
           return this.template.getAll(PubCompproper.class);
       }
    }
    public boolean update(PubCompproper instance) {
       // TODO Auto-generated method stub
       PubCompproper temp=this.get(instance.getCproPkid());
              if(instance.getSytp()==null){                
                  instance.setSytp(temp.getSytp());
              }
       return this.template.update(instance);
    }
}
这个是代码。
于是我的工作就很简单了,适用 Myeclipse 直接从 DataExplor 中生成 Domain bean 和映射文件,改一下关联,在 domain 中添加必要的 XDoclet 标记, build ,就可以专著于具体的业务了。
但这个代码还有点问题就是当遇到一个类有多个关联对象的时候,在生成的一些方法上,要自己手动增加或者删除一个 ”,” 。这个还要继续学习。
同时 XDoclet 提供了很好的扩展机制,这个也要继续研究。
再次就是我在想 XDoclet 中有没有直接使用标签来定义标签的功能,或者在模版内定义变量??
easyjweb 使用 XDoclet 来生成代码也会是很简单而且稳定的。

 (注:本文作者,EasyJF开源团队 stef_wu,转载请保留作者声明!)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值