chenjiahe
5 天以前 826b66207dafbce24f441cb83fed1b241a6fba27
提交 | 用户 | age
826b66 1 package com.hx.auto;
C 2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.OutputStreamWriter;
8 import java.io.Writer;
9 import java.util.HashMap;
10
11 import com.hx.auto.common.UrlData;
12 import com.hx.auto.util.CommonTool;
13 import freemarker.template.Configuration;
14 import freemarker.template.DefaultObjectWrapper;
15 import freemarker.template.Template;
16 import freemarker.template.TemplateException;
17
18 /**
19  * 自动生成Service and ServiceImpl
20  * 
21  * @author chenjiahe 2019年09月08日16:57:47
22  * 
23  */
24 public class GeneratorServiceUtil {
25
26     private static String templateDir = "/ftl";//获取模板路径
27
28     private static String templateServiceName = "Service.tpl";//service名称
29     private static String templateServiceImplName = "ServiceImpl.tpl";//serviceImpl名称
30     /**
31      * @param cl 实体类
32      * @param urlData 配置信息
33      * @throws Exception
34      */
35     @SuppressWarnings("deprecation")
36     public static void generatorService(Class<?> cl, UrlData urlData) throws Exception {
37         try {
38             //获取类的包名
39             String className = cl.getSimpleName();
40             String[] strs = cl.getName().split("\\.");
41             String packageName = "";
42             for(int i=0;i<strs.length-1;i++) {
43                 packageName += "."+strs[i];
44             }
45             packageName = packageName.replaceFirst(".","");
46             
47             // 类名首字母小写
48             String initial = className.substring(0, 1).toLowerCase();
49             String classNamex = initial + className.substring(1, className.length());
50             // 取得本类的全部属性
51
52             String targetFile = urlData.getServiceUrl()[1].replace(".", "/")+"/";
53             targetFile += className + "Service.java";
54             //补全路径
55             targetFile = "./"+urlData.getServiceUrl()[0].replace(".", "/")+"/"+targetFile;
56             File fileService = new File(targetFile);
57             if(fileService.exists()){
58                 //存在就结束
59                 return;
60             }
61
62
63             String targetFile2 = urlData.getServiceImplUrl()[1].replace(".", "/")+"/";
64             targetFile2 += className + "ServiceImpl.java";
65             //补全路径
66             targetFile2 = "./"+urlData.getServiceImplUrl()[0].replace(".", "/")+"/"+targetFile2;
67             File fileServiceImple = new File(targetFile2);
68             if(fileServiceImple.exists()){
69                 System.out.println("fileServiceImple:"+targetFile2);
70                 //存在就结束
71                 return;
72             }
73
74             //获取模板
75             Configuration configuration = new Configuration(Configuration.getVersion());
76             configuration.setClassForTemplateLoading(GeneratorServiceUtil.class, templateDir);
77             //configuration.setTemplateLoader(new ClassTemplateLoader(this.getClass(), "填你的resource下的路径,比如/ftl"));
78
79             // 获取或创建模板
80             Template template = configuration.getTemplate(templateServiceName);
81             
82             // 创建数据模型
83             HashMap<String, Object> root = new HashMap<String, Object>();
84             root.put("className", classNamex);
85             root.put("classNameUP", className);
86             root.put("packageName", packageName);
87             root.put("servicePack", urlData.getServiceUrl()[1]);
88             root.put("actionPack", urlData.getActionUrl()[1]);
89             
90
91             System.out.println("serviceUrl:"+targetFile);
92             // 将模板和数据模型合并 输出到Console
93             Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8"));
94             template.process(root, out);
95             out.flush();
96             out.close();
97
98             if(!CommonTool.checkNotNull(urlData.getTotalUrl())) {
99                 System.err.println("没有设置总包路径");
100                 return;
101             }
102             root.put("TotalPackageName",urlData.getTotalUrl());
103             root.put("serviceImpPack", urlData.getServiceImplUrl()[1]);
104             root.put("daoPack", urlData.getDaoUrl()[1]);
105             //serviceImp文件
106             // 获取或创建模板
107             Template template2 = configuration.getTemplate(templateServiceImplName);
108
109             System.out.println("serviceImpUrl:"+targetFile2);
110             // 将模板和数据模型合并 输出到Console
111             Writer out2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile2), "UTF-8"));
112             template2.process(root, out2);
113             out2.flush();
114             out2.close();
115         } catch (IOException e) {
116             e.printStackTrace();
117         } catch (TemplateException e) {
118             e.printStackTrace();
119         }
120     }
121 }