如何利用SQL中数据使用FreeMarker生成JAVA实体bean代码

 我来答
huanglenzhi
2014-12-08 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117538 获赞数:517200
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部
利用freemarker生成JAVA BEAN

Freemarker模板代码如下:

package ${packageName};

/**
* <#if author == "adams"> @author adams </#if>
*/
pulic class ${className} {
<#list attrs as a>
private ${a.type} ${a.field};
</#list>

<#list attrs as a>
public void set${a.field?cap_first}(${a.type} ${a.field}){
this.${a.field} = ${a.field};
}

public ${a.type} get${a.field?cap_first}(){
return this.${a.field};
}

</#list>
}

Java代码如下

package com.my.learn.freemarker;
public class Attr{
public String field;
public String type;

public Attr(String field, String type){
this.field = field;
this.type = type;
}

public String getField(){
return this.field;
}

public String getType(){
return this.type;
}

public void setField(String field){
this.field = field;
}

public void setType(String type){
this.type = type;
}
}

package com.my.learn.freemarker;
import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException;
public class FmAppUseage {

public static void main(String[] args){
List<Object> list = new ArrayList<Object>();
list.add(new Attr("username", "String"));
list.add(new Attr("password", "String"));
list.add(new Attr("age", "int"));
list.add(new Attr("hobby", "String"));

Map<String,Object> root = new HashMap<String, Object>();
root.put("packageName", "com.my.learn.freemarker");
root.put("className", "User");
root.put("attrs", list);
root.put("author", "adams");

Configuration cfg = new Configuration();
String path = FmAppUseage.class.getResource("/").getPath()+"template";
try {
cfg.setDirectoryForTemplateLoading(new File(path));
Template template = cfg.getTemplate("/demo.ftl");
StringWriter out = new StringWriter();
template.process(root, out);

System.out.println(out.toString());
} catch (IOException e) {
System.out.println("Cause==>" + e.getCause());
} catch (TemplateException e) {
System.out.println("Cause==>" + e.getCause());
}
}
}

输出结果如下:

package com.my.learn.freemarker;
/**
* @author adams
*/
pulic class User {
private String username;
private String password;
private int age;
private String hobby;

public void setUsername(String username){
this.username = username;
}

public String getUsername(){
return this.username;
}

public void setPassword(String password){
this.password = password;
}

public String getPassword(){
return this.password;
}

public void setAge(int age){
this.age = age;
}

public int getAge(){
return this.age;
}

public void setHobby(String hobby){
this.hobby = hobby;
}

public String getHobby(){
return this.hobby;
}

}

当在笔者刚做测试时,将Attr的类定义在了FmAppUseage类的内部,导致不能正常运行,只能将其移除单独成一个类时,便能正常运行了。 转载仅供参考,版权属于原作者。祝你愉快,满意请采纳哦
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式