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
17 /**
18  * 自动生成action
19  * 
20  * @author chenjiahe 2019年09月08日16:57:47
21  * 
22  */
23 public class GeneratorActionUtil {
24
25     private static String templateDir = "/ftl";//获取模板路径
26     private static String templateName = "Action.tpl";//action模板名称
27     
28     /**生成action
29      * @param cl 实体类
30      * @param urlData 生成配置信息
31      * @param sqlParam 是否生成动态sql的文件(sqlSentence.java),已经生成就不用生成
32      * @throws Exception
33      */
34     public static void generatorAction(Class<?> cl, UrlData urlData, boolean sqlParam) throws Exception {
35         try {
36
37             // 反射start
38             // 类名
39             String entityName = cl.getSimpleName();
40             // 类名首字母小写
41             String initial = entityName.substring(0, 1).toLowerCase();
42             String entityNameSmall = initial + entityName.substring(1, entityName.length());
43
44             //判断是否存在
45             //生成文件路径
46             String targetFile = urlData.getActionUrl()[1].replace(".", "/")+"/";
47             //生成文件名称
48             targetFile += entityName + "Controller.java";
49             targetFile = "./"+urlData.getActionUrl()[0].replace(".", "/")+"/"+targetFile;
50             File file = new File(targetFile);
51             if(file.exists()){
52                 //存在就结束
53                 return;
54             }
55
56             //获取实体类包名
57             String[] strs = cl.getName().split("\\.");
58             String packageName = "";
59             //去掉类名
60             for(int i=0;i<strs.length-1;i++) {
61                 packageName += "."+strs[i];
62             }
63             packageName = packageName.replaceFirst(".", "");
64
65             //获取模板
66             Configuration configuration = new Configuration(Configuration.getVersion());
67             configuration.setClassForTemplateLoading(GeneratorActionUtil.class, templateDir);
68             //configuration.setTemplateLoader(new ClassTemplateLoader(this.getClass(), "填你的resource下的路径,比如/ftl"));
69             Template template = configuration.getTemplate(templateName);
70             
71             // 创建数据模型
72             HashMap<String, Object> root = new HashMap<String, Object>();
73             
74             //action包名
75             if(!CommonTool.checkNotNull(urlData.getActionUrl())) {
76                 System.err.println("没有生成action路径");
77                 return;
78             }
79             root.put("packageName",urlData.getActionUrl()[1]);
80             //实体类的类名
81             root.put("entityName", entityName);
82             //实体类的类名(首字母小写)
83             root.put("entityNameSmall", entityNameSmall);
84
85             if(!CommonTool.checkNotNull(urlData.getTotalUrl())) {
86                 System.err.println("没有设置总包路径");
87                 return;
88             }
89             //dao的包名
90             if(!CommonTool.checkNotNull(urlData.getDaoUrl())) {
91                 System.err.println("没有dao路径");
92                 return;
93             }
94             root.put("TotalPackageName",urlData.getTotalUrl());
95             root.put("DAOPackageName",urlData.getDaoUrl()[1]);
96             //实体类的包名
97             root.put("entityPackageName",packageName);
98             //service的包名
99             if(!CommonTool.checkNotNull(urlData.getServiceUrl())) {
100                 System.err.println("没有service路径");
101                 return;
102             }
103             root.put("servicePackageName",urlData.getServiceUrl()[1]);
104
105             // 将模板和数据模型合并 输出到Console
106             Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8"));
107             template.process(root, out);
108             out.flush();
109             out.close();
110         } catch (IOException e) {
111             e.printStackTrace();
112         } 
113     }
114 }