2008-01-31
freemarker第一天
关键字: freemarker打字越来越累了,也开始玩一下代码生成。
今天仔细看了下freemarker,貌似还比较好玩的样子。试着写一写model的模板了。
/**
* 代码声明
*/
package ${package};
import org.youi.common.model.BaseObject;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.hibernate.validator.Pattern;
/**
* 功能说明:
* ${modelDescription}
* @author zhyi
* @version 1.0.1
*
*/
@Entity
@Table(name = "${tableName}")
public class ${className} extends BaseObject {
<#list attributes as attribute>
private ${attribute.type} ${attribute.name};<#if attribute.description??>//${attribute.description}</#if>
</#list>
<#list attributes as attribute>
public void set${attribute.FUName}(${attribute.type} ${attribute.name}){
this.${attribute.name} = ${attribute.name};
}
<#if attribute.notNull??&&attribute.notNull=="true">@NotNull</#if><#if attribute.id=="true">
@Id</#if><#if attribute.column??>
@Column(name = "${attribute.column}")</#if><#if attribute.length??>
@Length(<#if attribute.min??>min = ${attribute.min},</#if>max = ${attribute.length})</#if>
public ${attribute.type} get${attribute.FUName}(){
return this.${attribute.name};
}
</#list>
public boolean equals(Object o) {
if (o == null ) return false;
if (this == o ) return true;
if ( !(o instanceof ${className}) ) return false;
${className} obj = (${className}) o;
return <#list attributes as attribute>this.get${attribute.FUName}().equals(obj.get${attribute.FUName}())<#if attribute.size != "large">&&</#if></#list>;
}
public String toString(){
return ${toString};
}
public int hashCode() {
int hash = 0;
<#list attributes as attribute>
<#if attribute.id == "true">
hash+=this.get${attribute.FUName}().hashCode()*17+37;
</#if>
</#list>
return hash;
}
}今天学会的一些东西:
1、使用<#list></#list> 循环标签时,变量对应的顺序和集合的类型密切相关,适当的时机选择使用list还是set。用list的时候,为lis中t对象增加size属性,标志最后一个值为特定值以作些判断。比如我生成equals方法时,碰到最后一个属性不加&&符号。
2、<#if attribute.notNull??&&attribute.notNull=="true">@NotNull</#if><#if attribute.id=="true"> :if判断还真是有意思,居然这么干 attribute.notNull??
就用到了这两样东西,第一天的印象还不错,明天继续学习。
简单调用的代码:
Map root = new HashMap();
root.put("package", "org.youi.model");
root.put("className", "User");
root.put("tableName", "YOUI_USER");
root.put("modelDescription", "用户");
root.put("toString", "this.userName+this.userId");
List attributes = new ArrayList();
root.put("attributes", attributes);
Map attribute1 = new HashMap();
attributes.add(attribute1);
attribute1.put("type", "String");
attribute1.put("name", "userId");
attribute1.put("FUName", "UserId");
attribute1.put("column", "USER_ID");
attribute1.put("size", "fitst");
attribute1.put("id", "true");
attribute1.put("length", "20");
attribute1.put("min", "6");
attribute1.put("description", "用户ID");
attribute1.put("notNull", "true");
Map attribute2 = new HashMap();
attributes.add(attribute2);
attribute2.put("type", "String");
attribute2.put("name", "userName");
attribute2.put("FUName", "UserName");
attribute2.put("column", "USER_NAME");
attribute2.put("size", "2");
attribute2.put("id", "true");
attribute2.put("notNull", "true");
Map attribute3 = new HashMap();
attributes.add(attribute3);
attribute3.put("type", "String");
attribute3.put("name", "password");
attribute3.put("FUName", "Password");
attribute3.put("column", "PASSWORD");
attribute3.put("size", "large");
attribute3.put("id", "false");
/* Merge data-model with template */
ModelCode modelCode = new ModelCode();
modelCode.setClassName("User");
modelCode.setOutPath("E:/workspace/youi/src/org/youi/model/User.java");
modelCode.setData(root);
GeneratorFactory.getInstance().generatorCode("model",modelCode);
发表评论
- 浏览: 2044 次
- 性别:

- 来自: 武汉

- 详细资料
搜索本博客
我的相册
structure
共 1 张
共 1 张
最新评论
-
jQuery UI v1.5 Released ...
已经用上了,datepicker有点丑
-- by quaff -
jQuery UI v1.5 Released ...
文档变化不会很大,主要是进行了代码层面的重构,看以前的文档就行了
-- by zhyi_12 -
jQuery UI v1.5 Released ...
oo,唯有OO才能打救复杂的代码
-- by KKFC -
jQuery UI v1.5 Released ...
文档好像还没出来,搞得我折腾了一个晚上还搞不定个dialog。。。
-- by ayeah -
使用jquery做报表组件
我试了下也决定比较慢
-- by victorxiang






评论排行榜