利用freemarker生成java bean




利用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类的内部,导致不能正常运行,只能将其移除单独成一个类时,便能正常运行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值