chenjiahe
5 天以前 826b66207dafbce24f441cb83fed1b241a6fba27
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.hx.auto;
 
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
 
import com.hx.auto.common.UrlData;
import com.hx.auto.util.CommonTool;
 
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
 
/**
 * 自动生成DAO
 * @author chenjiahe 2019年09月08日16:57:47
 * 
 */
public class GeneratorDaoUtil {
 
    private static String templateDir = "/ftl";//获取模板路径
    private static String templateName = "Dao.tpl";//action模板名称
 
    /**生成Dao
     * @param cl 实体类
     * @param urlData 生成配置信息
     * @throws Exception
     */
    public static void generatorDao(Class<?> cl,UrlData urlData) throws Exception {
        try {
            // 反射start
            // 类名
            String entityName = cl.getSimpleName();
            // 类名首字母小写
            String initial = entityName.substring(0, 1).toLowerCase();
            String entityNameSmall = initial + entityName.substring(1, entityName.length());
 
            //生成文件路径
            String targetFile = urlData.getDaoUrl()[1].replace(".", "/")+"/";
            //生成文件名称
            targetFile += entityName + "Mapper.java";
            //映射文件的文件夹
            //补全路径
            targetFile = "./"+urlData.getDaoUrl()[0].replace(".", "/")+"/"+targetFile;
            File file = new File(targetFile);
            if(file.exists()){
                //System.out.println("存在333...:");
                //存在就结束
                return;
            }
 
            //获取实体类包名
            String[] strs = cl.getName().split("\\.");
            String packageName = "";
            //去掉类名
            for(int i=0;i<strs.length-1;i++) {
                packageName += "."+strs[i];
            }
            packageName = packageName.replaceFirst(".", "");
 
            //获取模板
            Configuration configuration = new Configuration(Configuration.getVersion());
            configuration.setClassForTemplateLoading(GeneratorDaoUtil.class, templateDir);
            //configuration.setTemplateLoader(new ClassTemplateLoader(this.getClass(), "填你的resource下的路径,比如/ftl"));
            Template template = configuration.getTemplate(templateName);
            
            // 创建数据模型
            HashMap<String, Object> root = new HashMap<String, Object>();
            
            //action包名
            if(!CommonTool.checkNotNull(urlData.getActionUrl())) {
                System.err.println("没有生成action路径");
                return;
            }
            root.put("packageName",urlData.getActionUrl()[1]);
            //实体类的类名
            root.put("entityName", entityName);
            //实体类的类名(首字母小写)
            root.put("entityNameSmall", entityNameSmall);
            
            //dao的包名
            if(!CommonTool.checkNotNull(urlData.getDaoUrl())) {
                System.err.println("没有dao路径");
                return;
            }
 
            if(!CommonTool.checkNotNull(urlData.getTotalUrl())) {
                System.err.println("没有设置总包路径");
                return;
            }
            root.put("TotalPackageName",urlData.getTotalUrl());
            root.put("DAOPackageName",urlData.getDaoUrl()[1]);
            //实体类的包名
            root.put("entityPackageName",packageName);
 
            System.out.println("actionUrl:"+targetFile);
            // 将模板和数据模型合并 输出到Console
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8"));
            template.process(root, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
}