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
14 import freemarker.template.Configuration;
15 import freemarker.template.DefaultObjectWrapper;
16 import freemarker.template.Template;
17
18 /**
19  * 自动生成DAO
20  * @author chenjiahe 2019年09月08日16:57:47
21  * 
22  */
23 public class GeneratorDaoUtil {
24
25     private static String templateDir = "/ftl";//获取模板路径
26     private static String templateName = "Dao.tpl";//action模板名称
27
28     /**生成Dao
29      * @param cl 实体类
30      * @param urlData 生成配置信息
31      * @throws Exception
32      */
33     public static void generatorDao(Class<?> cl,UrlData urlData) throws Exception {
34         try {
35             // 反射start
36             // 类名
37             String entityName = cl.getSimpleName();
38             // 类名首字母小写
39             String initial = entityName.substring(0, 1).toLowerCase();
40             String entityNameSmall = initial + entityName.substring(1, entityName.length());
41
42             //生成文件路径
43             String targetFile = urlData.getDaoUrl()[1].replace(".", "/")+"/";
44             //生成文件名称
45             targetFile += entityName + "Mapper.java";
46             //映射文件的文件夹
47             //补全路径
48             targetFile = "./"+urlData.getDaoUrl()[0].replace(".", "/")+"/"+targetFile;
49             File file = new File(targetFile);
50             if(file.exists()){
51                 //System.out.println("存在333...:");
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(GeneratorDaoUtil.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             //dao的包名
86             if(!CommonTool.checkNotNull(urlData.getDaoUrl())) {
87                 System.err.println("没有dao路径");
88                 return;
89             }
90
91             if(!CommonTool.checkNotNull(urlData.getTotalUrl())) {
92                 System.err.println("没有设置总包路径");
93                 return;
94             }
95             root.put("TotalPackageName",urlData.getTotalUrl());
96             root.put("DAOPackageName",urlData.getDaoUrl()[1]);
97             //实体类的包名
98             root.put("entityPackageName",packageName);
99
100             System.out.println("actionUrl:"+targetFile);
101             // 将模板和数据模型合并 输出到Console
102             Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8"));
103             template.process(root, out);
104             out.flush();
105             out.close();
106         } catch (IOException e) {
107             e.printStackTrace();
108         } 
109     }
110 }