文章关键字 ‘PowerDesigner’

基于FMPP的代码生成器-根据hbm.xml生成pojo的java代码。

2007年12月24日,星期一

powerdesigner本身已经提供了生成 hibernate方法,但是生成的代码不是我喜欢的样,于是自用fmpp直接根据hbm.xml编写代码模板,可以灵活控制想要生成的代码。

比如 get,set里自动判断null,生成toString,hashcode,toString等方法。

我的代码是参考myeclipse的风格:

.config 为:

outputEncoding:UTF-8
sourceRoot: src
outputRoot: out
logFile: log.fmpp
data: {
ftlEncoding:”UTF-8″
ppOutputEncoding:”UTF-8″
importPackage:”com.lizongbo.commons”
projectPackage:”com.lizongbo.commons”
javasrcroot:”/src”
javasrcbak:”/srcbak”
jspDir:”/WEB-INF/jspx”
importPackage2:”net.lizongbo.commons”
project:{
{testdemo:{
{Testonly:xml(webapp/WEB-INF/hbms/Testonly.hbm.xml)}
{ActiveSession:xml(webapp/WEB-INF/hbms/ActiveSession.hbm.xml)}
{SessionLog:xml(webapp/WEB-INF/hbms/SessionLog.hbm.xml)}
}
}
}
}

联合主键的代码生成模板:

<#ftl encoding=”UTF-8″>
<@pp.setOutputEncoding encoding=”UTF-8″/>
<#assign bmkeys = project?keys>
<#list bmkeys as bmkey>
<#assign  crtbigmodule=project[bmkey]>
<#assign ekeys = crtbigmodule?keys>
<#list ekeys as ekey>
<#assign  crtEntity=crtbigmodule[ekey]>
<#list crtEntity["-mapping"]["class"] as module>
<#if module["composite-id"]?has_content>
<#assign x = crtEntity["hibernate-mapping"]["@package"]?replace(“.”, “/”) >
<@pp.changeOutputFile name=”/src/”+x+”/”+module["composite-id"]["@class"]+”.java” />
package ${crtEntity["hibernate-mapping"]["@package"]};

import java.io.*;
import java.util.Hashtable;
import org.apache.log4j.Logger;
import ${importPackage}.xmlrpc.*;
public class ${module["composite-id"]["@class"]} implements Serializable,XmlRpcType  {
private static final transient Logger log = Logger.getLogger(${module["composite-id"]["@class"]}.class);
private volatile int hashValue = 0;
<#list module["composite-id"]["key-property"] as formfield>
private  ${formfield.@type} ${formfield.@name};  <#if formfield.@node?has_content>// ${formfield.@node?if_exists}</#if>
</#list>

public ${module["composite-id"]["@class"]}() {
}
public ${module["composite-id"]["@class"]}(<#list module["composite-id"]["key-property"] as formfield>${formfield.@type} ${formfield.@name}<#if formfield_has_next>, </#if></#list>) {
<#list module["composite-id"]["key-property"] as formfield>
this.set${formfield.@name?cap_first}(${formfield.@name}) ;
</#list>
}
<#list module["composite-id"]["key-property"] as formfield>
public void set${formfield.@name?cap_first}(${formfield.@type} ${formfield.@name}) {
<#if formfield.@type?ends_with(“String”) && formfield['@length']?has_content >
if(${formfield.@name} != null  && ${formfield.@name}.length() > ${formfield['@length']}){
log.error(“${formfield.@name}  length is max than ${formfield['@length']}”);
${formfield.@name} = ${formfield.@name}.substring(0,${formfield['@length']});
}
</#if>
this.${formfield.@name} = ${formfield.@name};
}
public ${formfield.@type} get${formfield.@name?cap_first}() {
return ${formfield.@name};
}
</#list>
public boolean equals(Object rhs)
{
if (rhs == null)
return false;
if (! (rhs instanceof ${module["composite-id"]["@class"]}))
return false;
${module["composite-id"]["@class"]} that = (${module["composite-id"]["@class"]}) rhs;
<#list module["composite-id"]["key-property"] as formfield>
if (this.get${formfield.@name?cap_first}() == null || that.get${formfield.@name?cap_first}() == null)
{
return false;
}
if (! this.get${formfield.@name?cap_first}().equals(that.get${formfield.@name?cap_first}()))
{
return false;
}
</#list>
return true;
}

public int hashCode()
{
if (this.hashValue == 0)
{
int result = 17;
<#list module["composite-id"]["key-property"] as formfield>
int ${formfield.@name}Value = this.get${formfield.@name?cap_first}() == null ? 0 : this.get${formfield.@name?cap_first}().hashCode();
result = result * 37 + ${formfield.@name}Value;
</#list>
this.hashValue = result;
}
return this.hashValue;
}

public Object fromXmlRpc(Hashtable struct) {
${module["composite-id"]["@class"]} ${module["composite-id"]["@class"]?uncap_first} = new ${module["composite-id"]["@class"]}();
<#list module["composite-id"]["key-property"] as formfield>
if (struct.get(“${formfield.@name}”) != null) {
this.set${formfield.@name?cap_first}((${formfield.@type}) struct.get(“${formfield.@name}”));
}
</#list>
return ${module["composite-id"]["@class"]?uncap_first} ;
}

public Hashtable toXmlRpc() {
Hashtable rs = new Hashtable();
<#list module["composite-id"]["key-property"] as formfield>
if (this.get${formfield.@name?cap_first}() != null) {
rs.put(“${formfield.@name}”, this.get${formfield.@name?cap_first}());
}
</#list>
return rs;
}

}

</#if>
</#list>
</#list>
</#list>

pojo的代码生成模板:

<#ftl encoding=”UTF-8″>
<@pp.setOutputEncoding encoding=”UTF-8″/>
<#assign bmkeys = project?keys>
<#list bmkeys as bmkey>
<#assign  crtbigmodule=project[bmkey]>
<#assign ekeys = crtbigmodule?keys>
<#list ekeys as ekey>
<#assign  crtEntity=crtbigmodule[ekey]>
<#list crtEntity["hibernate-mapping"]["class"] as module>
<#assign x = crtEntity["hibernate-mapping"]["@package"]?replace(“.”, “/”) >
<@pp.changeOutputFile name=”/src/”+x+”/”+module["@name"]+”.java” />
package ${crtEntity["hibernate-mapping"]["@package"]};

import java.io.*;

/**
* A class that represents a row in the ‘${module["@table"]}’ table.
*/

public class ${module["@name"]}
extends Abstract${module["@name"]}
implements Serializable
{
public ${module["@name"]}()
{
}

<#if module["id"]?has_content>
public ${module["@name"]}(${module["id"]["@type"]} ${module["id"]["@name"]})
{
super(${module["id"]["@name"]});
}
</#if>

<#if module["composite-id"]?has_content>
public ${module["@name"]}(${module["composite-id"]["@class"]} ${module["composite-id"]["@name"]})
{
super(${module["composite-id"]["@name"]});
}
</#if>

}
</#list>
</#list>
</#list>

Abstractpojo的代买模板为:

<#ftl encoding=”UTF-8″>
<@pp.setOutputEncoding encoding=”UTF-8″/>
<#assign bmkeys = project?keys>
<#list bmkeys as bmkey>
<#assign  crtbigmodule=project[bmkey]>
<#assign ekeys = crtbigmodule?keys>
<#list ekeys as ekey>
<#assign  crtEntity=crtbigmodule[ekey]>
<#list crtEntity["hibernate-mapping"]["class"] as module>

<#assign x = crtEntity["hibernate-mapping"]["@package"]?replace(“.”, “/”) >
<@pp.changeOutputFile name=”/src/”+x+”/Abstract”+module["@name"]+”.java” />
package ${crtEntity["hibernate-mapping"]["@package"]};

import java.io.*;
import java.util.Hashtable;
import org.apache.log4j.Logger;
import ${importPackage}.xmlrpc.*;
public class Abstract${module["@name"]} implements Serializable,XmlRpcType {
private static final transient Logger log = Logger.getLogger(Abstract${module["@name"]}.class);
private int hashValue = 0;
<#if module["composite-id"]?has_content>
private  ${module["composite-id"]["@class"]} ${module["composite-id"]["@name"]} = new ${module["composite-id"]["@class"]} ();  <#if module["composite-id"]["@node"]?has_content>// ${module["composite-id"]["@node"]?if_exists}</#if>
</#if>
<#if module["id"]?has_content>
private  ${module["id"]["@type"]} ${module["id"]["@name"]};  <#if module["id"]["@node"]?has_content>// ${module["id"]["@node"]?if_exists}</#if>
</#if>
<#if module["version"]?has_content>
private  ${module["version"]["@type"]} ${module["version"]["@name"]} = ${module["version"]["@type"]}.valueOf(0);  //<#if module["version"]["@node"]?has_content> ${module["version"]["@node"]?if_exists}<#else>${module["version"]["@name"]}</#if>
</#if>
<#list module["property"] as formfield>
private  ${formfield.@type} ${formfield.@name};  <#if formfield.@node?has_content>// ${formfield.@node?if_exists}</#if>
</#list>

public Abstract${module["@name"]}() {
}
<#if module["id"]?has_content>
public Abstract${module["@name"]}(${module["id"]["@type"]} ${module["id"]["@name"]})
{
this.set${module["id"]["@name"]?cap_first}(${module["id"]["@name"]});
}
public ${module["id"]["@type"]} get${module["id"]["@name"]?cap_first}()
{
return this.${module["id"]["@name"]};
}
public void set${module["id"]["@name"]?cap_first}(${module["id"]["@type"]} ${module["id"]["@name"]})
{
this.hashValue = 0;
this.${module["id"]["@name"]} = ${module["id"]["@name"]};
}
</#if>

<#if module["version"]?has_content>
public ${module["version"]["@type"]} get${module["version"]["@name"]?cap_first}()
{
return this.${module["version"]["@name"]} == null ? ${module["version"]["@type"]}.valueOf(0) : this.${module["version"]["@name"]};
}
public void set${module["version"]["@name"]?cap_first}(${module["version"]["@type"]} ${module["version"]["@name"]})
{
this.${module["version"]["@name"]} = ${module["version"]["@name"]};
}
</#if>

<#if module["composite-id"]?has_content>
public Abstract${module["@name"]}(${module["composite-id"]["@class"]} ${module["composite-id"]["@name"]})
{
this.set${module["composite-id"]["@name"]?cap_first}(${module["composite-id"]["@name"]});
}
public ${module["composite-id"]["@class"]} get${module["composite-id"]["@name"]?cap_first}()
{
if(this.${module["composite-id"]["@name"]} == null )
{
this.${module["composite-id"]["@name"]}= new ${module["composite-id"]["@class"]} ();
}

return this.${module["composite-id"]["@name"]}  ;
}
public void set${module["composite-id"]["@name"]?cap_first}(${module["composite-id"]["@class"]} ${module["composite-id"]["@name"]})
{
this.hashValue = 0;
this.${module["composite-id"]["@name"]} = ${module["composite-id"]["@name"]};
}
</#if>

<#list module["property"] as formfield>
public void set${formfield.@name?cap_first}(${formfield.@type} ${formfield.@name}) {
<#if formfield.@type?ends_with(“String”) && formfield['@length']?has_content >
if(${formfield.@name} != null  && ${formfield.@name}.length() > ${formfield['@length']}){
log.error(“${formfield.@name} : ” + ${formfield.@name} +”  length is max than ${formfield['@length']}”);
${formfield.@name} = ${formfield.@name}.substring(0,${formfield['@length']});
}
</#if>
this.${formfield.@name} = ${formfield.@name};
}
public ${formfield.@type} get${formfield.@name?cap_first}() {
<#if formfield.@column?has_content>
<#if formfield.@type?ends_with(“Date”) || formfield.@type?ends_with(“Timestamp”)  >
if(this.${formfield.@name} == null ){
this.${formfield.@name}= new ${formfield.@type}(System.currentTimeMillis()) ;
}
return this.${formfield.@name};
<#else>
<#if formfield.@type?ends_with(“Integer”)   >
return ${formfield.@name} == null ? Integer.valueOf(0) : this.${formfield.@name};
<#else>
<#if formfield.@type?ends_with(“Long”)   >
return this.${formfield.@name} == null ? Long.valueOf(0) : this.${formfield.@name};
<#else>
return this.${formfield.@name} == null ? “” : this.${formfield.@name};
</#if>
</#if>
</#if>
</#if>
}
</#list>

<#if module["id"]?has_content>
public boolean equals(Object obj)
{
if (obj == null)
return false;
if (! (obj instanceof ${module["@name"]}))
return false;
${module["@name"]} that = (${module["@name"]}) obj;
if (this.get${module["id"]["@name"]?cap_first}() == null || that.get${module["id"]["@name"]?cap_first}() == null)
return false;
return (this.get${module["id"]["@name"]?cap_first}().equals(that.get${module["id"]["@name"]?cap_first}()));
}

public int hashCode()
{
if (this.hashValue == 0)
{
int result = 17;
int idValue = this.get${module["id"]["@name"]?cap_first}() == null ? 0 : this.get${module["id"]["@name"]?cap_first}().hashCode();
result = result * 37 + idValue;
this.hashValue = result;
}
return this.hashValue;
}
</#if>

<#if module["composite-id"]?has_content>
public boolean equals(Object obj)
{
if (obj == null)
return false;
if (! (obj instanceof ${module["@name"]}))
return false;
${module["@name"]} that = (${module["@name"]}) obj;
if (this.get${module["composite-id"]["@name"]?cap_first}() == null || that.get${module["composite-id"]["@name"]?cap_first}() == null)
return false;
return (this.get${module["composite-id"]["@name"]?cap_first}().equals(that.get${module["composite-id"]["@name"]?cap_first}()));
}

public int hashCode()
{
if (this.hashValue == 0)
{
int result = 17;
int idValue = this.get${module["composite-id"]["@name"]?cap_first}() == null ? 0 : this.get${module["composite-id"]["@name"]?cap_first}().hashCode();
result = result * 37 + idValue;
this.hashValue = result;
}
return this.hashValue;
}
</#if>
public Object fromXmlRpc(Hashtable struct) {
// ${module["@name"]} ${module["@name"]?uncap_first} = new ${module["@name"]}();
<#if module["id"]?has_content>
if (struct.get(“${module["id"]["@name"]}”) != null) {
this.set${module["id"]["@name"]?cap_first}((${module["id"]["@type"]}) struct.get(“${module["id"]["@name"]}”));
}
</#if>
<#if module["composite-id"]?has_content>
if (struct.get(“${module["composite-id"]["@name"]}”) != null) {
this.set${module["composite-id"]["@name"]?cap_first}((${module["composite-id"]["@class"]})this.get${module["composite-id"]["@name"]?cap_first}().fromXmlRpc((Hashtable)struct.get(“${module["composite-id"]["@name"]}”)));
}
</#if>
<#list module["property"] as formfield>
if (struct.get(“${formfield.@name}”) != null) {
<#if formfield.@type?ends_with(“Date”) || formfield.@type?ends_with(“Timestamp”)  >
this.set${formfield.@name?cap_first}( new ${formfield.@type}(((java.util.Date)struct.get(“${formfield.@name}”)).getTime()) );//时间类型转换
<#else>
this.set${formfield.@name?cap_first}((${formfield.@type}) struct.get(“${formfield.@name}”));
</#if>
}
</#list>
return this;//${module["@name"]?uncap_first} ;
}

public Hashtable toXmlRpc() {
Hashtable rs = new Hashtable();
<#if module["id"]?has_content>
if (this.get${module["id"]["@name"]?cap_first}() != null) {
rs.put(“${module["id"]["@name"]}”, this.get${module["id"]["@name"]?cap_first}());
}
</#if>
<#if module["composite-id"]?has_content>
if (this.get${module["composite-id"]["@name"]?cap_first}() != null) {
rs.put(“${module["composite-id"]["@name"]}”, this.get${module["composite-id"]["@name"]?cap_first}().toXmlRpc());
}
</#if>
<#list module["property"] as formfield>
if (this.get${formfield.@name?cap_first}() != null) {
rs.put(“${formfield.@name}”, this.get${formfield.@name?cap_first}());
}
</#list>
return rs;
}

}
</#list>
</#list>
</#list>

Tags: , , ,

基于fmpp的代码生成器–根据PowerDesigner的实体模型生成生成hibernate的hbm.xml等文件

2007年12月20日,星期四

曾有段时间,对代码生成器非常感兴趣,于是使用fmpp做了个简单的代码生成器,

生成的流程是,根据 PowerDesigner的cmd实体模型文件,生成每张表的hbm.xml,

然后再根据hbm.xml,生成对应的相关java代码,xml配置,jsp页面等文件。

大概有以下代码模板: spring-.xml
struts-web_pojo.xml
Abstractpojo.java
pojo.java
pojoKey.java

IpojoLogic.java
pojoLogicImpl.java
spring-pojo.xml

IpojoDAO.java
pojoHibernateDAOImpl.java
compass-pojo.cmd.xml
compass-pojo.cpm.xml

pojoAddAction.java
pojoDelAction.java
pojoEditAction.java
pojoEditActionForm.java
pojoIndexAction.java
pojoSearchActionForm.java
pojoUpdateAction.java
struts-config_pojo.xml
struts-spring-pojo.xml
validator_pojo.xml
pojoindex.jspx

后来这些东西没有继续折腾了,这几天清理硬盘时发现了这些,因此在这里把代码贴出来,留作备忘。

cdm到hbm.xml 的fmpp 配置为:

config.

—————–

outputEncoding:UTF-8
sourceRoot: src
outputRoot: hbm2code
logFile: log.fmpp
data: {
pdcdmentity:xml(data/pdcdm.cdm)
}

hbm.xml的代码模板为(只实现基本的映射和复合主键判断,对于简单的单表够用,其它的没处理):

[code]

<#ftl encoding="UTF-8" ns_prefixes={"a":"attribute","c":"collection","o":"object"} >
<@pp.setOutputEncoding encoding="UTF-8"/>
<#list pdcdmentity["Model"]["o:RootObject"]["c:Children"]["o:Model"]["c:Entities"]["o:Entity"] as et>
<#assign x = et["a:Code"]?cap_first >
<@pp.changeOutputFile name="/src/webapp/WEB-INF/hbms/"+x+".hbm.xml" />
<?xml version="1.0" encoding='UTF-8'?>
<!--
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
-->
<hibernate-mapping package="net.share_info.dxw.hibernate">
<#if et["a:Comment"]?has_content><!-- ${et["a:Comment"]} --> </#if>
<class name="${et["a:Code"]?cap_first}" table="${et["a:Code"]?upper_case}" node="${et["a:Name"]}" optimistic-lock="version">
<#assign pkirefid = et["c:PrimaryIdentifier"]["o:Identifier"]["@Ref"]>
<#assign pkoi = getPKI(pdcdmentity["Model"]["o:RootObject"]["c:Children"]["o:Model"]["c:Entities"]["o:Entity"]["c:Identifiers"],pkirefid)>
<#assign pknum = pkoi["c:Identifier.Attributes"]["o:EntityAttribute"]?size>
<#if pknum >= 2 >
<composite-id name="id" class="${et["a:Code"]?cap_first}Key">
<#list pkoi["c:Identifier.Attributes"]["o:EntityAttribute"] as pkp>
<#assign oeRef = pkp["@Ref"] >
<#assign oEntityAttribute = getoEntityAttribute(et,oeRef) >
<#assign oModel = pdcdmentity["Model"]["o:RootObject"]["c:Children"]["o:Model"] >
<#assign oeDRef=oEntityAttribute["c:DataItem"]["o:DataItem"]["@Ref"] >
<#assign oDataItem = getoDataItem(oModel,oeDRef) >
<key-property name="${oDataItem["a:Code"]?uncap_first}" column="${oDataItem["a:Code"]?upper_case}" node="${oDataItem["a:Name"]}" type="${getJavaType(oDataItem["a:DataType"])}" <#if oDataItem["a:Length"]?has_content > length="${oDataItem["a:Length"]}"</#if> />
</#list>
</composite-id>
<#else>
<#list pkoi["c:Identifier.Attributes"]["o:EntityAttribute"] as pkp>
<#assign oeRef = pkp["@Ref"] >
<#assign oEntityAttribute = getoEntityAttribute(et,oeRef) >
<#assign oModel = pdcdmentity["Model"]["o:RootObject"]["c:Children"]["o:Model"] >
<#assign oeDRef=oEntityAttribute["c:DataItem"]["o:DataItem"]["@Ref"] >
<#assign oDataItem = getoDataItem(oModel,oeDRef) >
<id name="${oDataItem["a:Code"]?uncap_first}" column="${oDataItem["a:Code"]?upper_case}" node="${oDataItem["a:Name"]}" type="${getJavaType(oDataItem["a:DataType"])}" <#if oDataItem["a:Length"]?has_content > length="${oDataItem["a:Length"]}"</#if> >
<generator class="assigned"/>
</id>
</#list>
</#if>
<version name="version" column="VERSION" node="version" type="java.lang.Integer"/>
<property name="orderindex" column="ORDERINDEX" type="java.lang.Integer" node="orderindex"/>
<property name="addtime" column="ADDTIME" type="java.util.Date" node="addtime" update="false"/>
<property name="lastupdatetime" column="LASTUPDATETIME" type="java.util.Date" node="lastupdatetime"/>
<#list et["c:Attributes"]["o:EntityAttribute"] as oEntityAttribute >
<#assign oModel = pdcdmentity["Model"]["o:RootObject"]["c:Children"]["o:Model"] >
<#assign oeDRef=oEntityAttribute["c:DataItem"]["o:DataItem"]["@Ref"] >

<#if isPKProperty(et,oEntityAttribute["@Id"]) = "false">
<#assign oDataItem = getoDataItem(oModel,oeDRef) >
<property name="${oDataItem["a:Code"]?uncap_first}" column="${oDataItem["a:Code"]?upper_case}" node="${oDataItem["a:Name"]}" type="${getJavaType(oDataItem["a:DataType"])}" <#if oEntityAttribute["a:BaseAttribute.Mandatory"]?has_content >not-null="true"</#if> <#if oDataItem["a:Length"]?has_content > length="${oDataItem["a:Length"]}"</#if> />
</#if>
</#list>
</class>

</hibernate-mapping>
</#list>
<#function getPKI oids pkoid>
<#list oids["o:Identifier"] as oid>
<#if oid["@Id"] = pkoid >
<#return oid>
</#if>
</#list>
</#function>

<#function getoEntityAttribute entity eid>
<#list entity["c:Attributes"]["o:EntityAttribute"] as oid>
<#if oid["@Id"] = eid >
<#return oid>
</#if>
</#list>
</#function>

<#function getoDataItem model eid>
<#list model["c:DataItems"]["o:DataItem"] as odi>
<#if odi["@Id"] = eid >
<#return odi>
</#if>
</#list>
</#function>

<#function isPKProperty et attRef>
<#assign pkirefid = et["c:PrimaryIdentifier"]["o:Identifier"]["@Ref"]>
<#assign pkoi = getPKI(pdcdmentity["Model"]["o:RootObject"]["c:Children"]["o:Model"]["c:Entities"]["o:Entity"]["c:Identifiers"],pkirefid)>
<#assign pknum = pkoi["c:Identifier.Attributes"]["o:EntityAttribute"]?size>
<#list pkoi["c:Identifier.Attributes"]["o:EntityAttribute"] as pkp>
<#assign oeRef = pkp["@Ref"] >
<#if oeRef = attRef >
<#return "true">
</#if>
</#list>
<#return "false">
</#function>

<#function getJavaType dataType>
<#if dataType?has_content >
<#if dataType?index_of("VA") =0 >
<#return "java.lang.String">
</#if>
</#if>
<#switch dataType>
<#case "DT">
<#return "java.sql.Timestamp">
<#case "I">
<#return "java.lang.Integer">
<#case "SI">
<#return "java.lang.Short">
<#case "LI">
<#return "java.lang.Long">
<#case "BL">
<#return "java.lang.Boolean">
<#case "TXT">
<#return "java.lang.String">
<#case "D">
<#return "java.sql.Date">
<#case "T">
<#return "java.sql.Time">
<#default>
<#return "java.lang.String">
</#switch>
</#function>

[/code]

Tags: , , ,