Archive for 12月 20th, 2007

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

星期四, 12月 20th, 2007

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

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

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

大概有以下代码模板: spring-Hibernate.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.fmpp

—————–

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: fmpp, Hibernate, PowerDesigner, 代码生成器

Related posts