2007年12月24日 存档

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

2007年12月24日,星期一

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

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

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

fmpp.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["hibernate-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, Hibernate, PowerDesigner, 代码生成器

Related posts