fwq
昨天 1dd767dcc9919e3a0068835cd0dbca57f94debd5
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
111
112
113
114
115
116
117
118
119
120
121
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;
import freemarker.template.TemplateException;
 
/**
 * 自动生成Service and ServiceImpl
 * 
 * @author chenjiahe 2019年09月08日16:57:47
 * 
 */
public class GeneratorServiceUtil {
 
    private static String templateDir = "/ftl";//获取模板路径
 
    private static String templateServiceName = "Service.tpl";//service名称
    private static String templateServiceImplName = "ServiceImpl.tpl";//serviceImpl名称
    /**
     * @param cl 实体类
     * @param urlData 配置信息
     * @throws Exception
     */
    @SuppressWarnings("deprecation")
    public static void generatorService(Class<?> cl, UrlData urlData) throws Exception {
        try {
            //获取类的包名
            String className = cl.getSimpleName();
            String[] strs = cl.getName().split("\\.");
            String packageName = "";
            for(int i=0;i<strs.length-1;i++) {
                packageName += "."+strs[i];
            }
            packageName = packageName.replaceFirst(".","");
            
            // 类名首字母小写
            String initial = className.substring(0, 1).toLowerCase();
            String classNamex = initial + className.substring(1, className.length());
            // 取得本类的全部属性
 
            String targetFile = urlData.getServiceUrl()[1].replace(".", "/")+"/";
            targetFile += className + "Service.java";
            //补全路径
            targetFile = "./"+urlData.getServiceUrl()[0].replace(".", "/")+"/"+targetFile;
            File fileService = new File(targetFile);
            if(fileService.exists()){
                //存在就结束
                return;
            }
 
 
            String targetFile2 = urlData.getServiceImplUrl()[1].replace(".", "/")+"/";
            targetFile2 += className + "ServiceImpl.java";
            //补全路径
            targetFile2 = "./"+urlData.getServiceImplUrl()[0].replace(".", "/")+"/"+targetFile2;
            File fileServiceImple = new File(targetFile2);
            if(fileServiceImple.exists()){
                System.out.println("fileServiceImple:"+targetFile2);
                //存在就结束
                return;
            }
 
            //获取模板
            Configuration configuration = new Configuration(Configuration.getVersion());
            configuration.setClassForTemplateLoading(GeneratorServiceUtil.class, templateDir);
            //configuration.setTemplateLoader(new ClassTemplateLoader(this.getClass(), "填你的resource下的路径,比如/ftl"));
 
            // 获取或创建模板
            Template template = configuration.getTemplate(templateServiceName);
            
            // 创建数据模型
            HashMap<String, Object> root = new HashMap<String, Object>();
            root.put("className", classNamex);
            root.put("classNameUP", className);
            root.put("packageName", packageName);
            root.put("servicePack", urlData.getServiceUrl()[1]);
            root.put("actionPack", urlData.getActionUrl()[1]);
            
 
            System.out.println("serviceUrl:"+targetFile);
            // 将模板和数据模型合并 输出到Console
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8"));
            template.process(root, out);
            out.flush();
            out.close();
 
            if(!CommonTool.checkNotNull(urlData.getTotalUrl())) {
                System.err.println("没有设置总包路径");
                return;
            }
            root.put("TotalPackageName",urlData.getTotalUrl());
            root.put("serviceImpPack", urlData.getServiceImplUrl()[1]);
            root.put("daoPack", urlData.getDaoUrl()[1]);
            //serviceImp文件
            // 获取或创建模板
            Template template2 = configuration.getTemplate(templateServiceImplName);
 
            System.out.println("serviceImpUrl:"+targetFile2);
            // 将模板和数据模型合并 输出到Console
            Writer out2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile2), "UTF-8"));
            template2.process(root, out2);
            out2.flush();
            out2.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }
}