利用freemarker生成java bean
發表於1年前(2014-03-27 11:35) 閱讀(367) | 評論(0) 1人收藏此文章, 我要收藏
贊0
慕課網,程序員升職加薪神器,點擊免費學習
摘要 利用freemarker生成java bean
freemarker bean
利用freemarker生成JAVA BEAN
項目結構如下:
Freemarker模板代碼如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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代碼如下
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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;
}
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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());
}
}
}
輸出結果如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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類的內部,導致不能正常運行,只能將其移除單獨成一個類時,便能正常運行了。