利用Freemarker实现表到功能界面的一键生成
2015-04-19 07:42
435 查看
利用Freemarker实现表到功能界面的一键生成
2012-04-20 17:50:03
标签:执行文件 程序编写 表 工程 开发
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。/article/4461317.html
利用Freemarker产生项目所需的常用文件,可以根据一个表产生对应的ibtais的java及xml文件,及前台的JSP文件,这样可以大大的提高普通程序编写人员的开发效率,及从表到界面及CURD功能利用这个工程可以直接生成好,下面提供下最核心的文件产生类,代码如下:
另外提供编译过的工程文件,可执行文件,在附件中,由于大小控制,已经删除里面的freemarker-2.3.15.jar文件。
01.package com.hundsun.pms.generator.util;
02.
03.import com.hundsun.pms.generator.api.Constants;
04.import com.hundsun.pms.generator.model.page.Page;
05.import freemarker.template.Configuration;
06.import freemarker.template.DefaultObjectWrapper;
07.import freemarker.template.Template;
08.import freemarker.template.TemplateException;
09.import org.apache.commons.lang.StringUtils;
10.import org.springframework.util.ResourceUtils;
11.
12.import java.io.*;
13.import java.net.URL;
14.import java.util.Locale;
15.import java.util.Map;
16.
17./**
18. * Created by IntelliJ IDEA.
19. * User: pangj
20. * Date: 2012-3-6
21. * Time: 10:05:26
22. * 文件操作工具类
23. */
24.public class FreemarkerUtil {
25.
26. public static String PROJECT_ROOT = "projectRoot";
27.
28. public static String PACKAGE_NAME = "packageName";
29.
30. public static String CLASS_SIMPLE_NAME = "classSimpleName";
31.
32. public static String MODULES_PACKAGE = "modulesPackage";
33.
34. public static String METHOD_NAME = "methodName";
35.
36. //模板根目录
37. private static final String templateRootWithClasspath = "classpath*:com/hundsun/pms/generator/resource/template";
38.
39. private static final String templateRoot = "com/hundsun/pms/generator/resource/template";
40. //工程根目录
41. private static String projectRoot;
42. //包名
43. private static String packageName;
44. //服务所在的包路径
45. private static String modulesPackage;
46.
47. private static String classSimpleName;
48.
49. private static String templateName;
50.
51. private static String methodName;
52.
53. public static final String FILE_BO_NAME_SUFFIX = "Bo.java";
54.
55. public static final String FILE_BOIMPL_NAME_SUFFIX = "BoImpl.java";
56.
57. public static final String FILE_DAO_NAME_SUFFIX = "Dao.java";
58.
59. public static final String FILE_DAOIMPL_NAME_SUFFIX = "DaoImpl.java";
60.
61. public static final String FILE_DTO_NAME_SUFFIX = ".java";
62.
63. public static final String FILE_SQL_MAP_SUFFIX = ".xml";
64.
65. public static final String FILE_SERVICE_SUFFIX = ".service";
66.
67. public static final String FILE_COMPONENT_SUFFIX = ".component";
68.
69. public static final String FILE_MODULE_NAME = "module.xml";
70.
71. public static final String FILE_PAGE_SUFFIX = ".jsp";
72.
73.
74. /**
75. * 数据绑定至模板
76. *
77. * @param tpName 模板名
78. * @param param 参数
79. * @return
80. */
81. public static void process(String tpName, Map<String, Object> param) {
82. init(tpName, param);
83. Writer out = null;
84. try {
85. Template temp = getTemplate(templateName);
86. String fileDir = getFileDir();
87. File dir = new File(fileDir);
88. if (!dir.exists()) {
89. if (!dir.mkdirs()) {
90. MsgUtil.print("创建目录:" + fileDir + "失败");
91. return;
92. }
93. }
94. File f = new File(fileDir + getFileName());
95. out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));
96. try {
97. temp.setEncoding("utf-8");
98. temp.process(param, out);
99. MsgUtil.print("文件:" + getFileDir() + getFileName() + "生成完成!");
100. } catch (TemplateException e) {
101. MsgUtil.print("文件:" + getFileDir() + getFileName() + "生成失败,请检查原因!");
102. MsgUtil.print(e.getMessage());
103. }
104. out.flush();
105. out.close();
106. } catch (IOException e) {
107. e.printStackTrace();
108. MsgUtil.print(e.getMessage());
109. }
110. }
111.
112. /**
113. * 生成页面
114. *
115. * @param tpName 模板名
116. * @param jsp jsp页面全路径
117. * @param page 页面对象
118. */
119. public static void process(String tpName, String jsp, Page page) {
120. Writer writer = null;
121. try {
122. Template tmp = FreemarkerUtil.getTemplate(tpName);
123. String dir = jsp.substring(0, jsp.lastIndexOf("\\"));
124. File fdir = new File(dir);
125. if (!fdir.exists()) {
126. if (!fdir.mkdirs()) {
127. MsgUtil.print("创建目录:" + fdir + "失败");
128. return;
129. }
130. }
131. File file = new File(jsp);
132. writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
133. tmp.setEncoding("utf-8");
134. tmp.process(page, writer);
135. writer.flush();
136. writer.close();
137. MsgUtil.print("页面:" + jsp + " 生成成功!");
138. } catch (TemplateException e) {
139. e.printStackTrace();
140. MsgUtil.print(e.getMessage());
141. } catch (IOException e) {
142. e.printStackTrace();
143. MsgUtil.print(e.getMessage());
144. }
145. }
146.
147. public static Template getTemplate(String templateName) {
148. Configuration cfg = new Configuration();
149. Template temp = null;
150. URL path = FreemarkerUtil.class.getClassLoader().getSystemResource("");
151. File tmpRootFile = null;
152. if (path == null) {
153. tmpRootFile = getResourceURL("resource/template");
154. } else {
155. String tmpRoot = path + templateRoot;
156. tmpRoot = tmpRoot.replaceAll("file:/", "");
157. tmpRootFile = new File(tmpRoot);
158. }
159. if (tmpRootFile == null) {
160. throw new RuntimeException("无法取得模板根路径!");
161. }
162. try {
163. cfg.setDefaultEncoding("utf-8");
164. cfg.setOutputEncoding("utf-8");
165. cfg.setDirectoryForTemplateLoading(tmpRootFile);
166. /*cfg.setDirectoryForTemplateLoading(getResourceURL());*/
167. cfg.setObjectWrapper(new DefaultObjectWrapper());
168. temp = cfg.getTemplate(templateName);
169. } catch (IOException e) {
170. e.printStackTrace();
171. MsgUtil.print(e.getMessage());
172. }
173. return temp;
174. }
175.
176. public static File getResourceURL(String templatePath) {
177. try {
178. URL url = ResourceUtils.getURL(templateRootWithClasspath);
179. String path = url.getPath();
180. path = path.replace(templateRootWithClasspath, templatePath);
181. MsgUtil.print("File path is:"+path);
182. path = StringUtils.replace(path, "%20", " ");
183. return new File(path);
184. } catch (FileNotFoundException e) {
185. throw new RuntimeException("不能获取路径:" + templateRootWithClasspath);
186. }
187. }
188.
189. private static void init(String tpName, Map<String, Object> param) {
190. projectRoot = (String) param.get(PROJECT_ROOT);
191. packageName = (String) param.get(PACKAGE_NAME);
192. classSimpleName = (String) param.get(CLASS_SIMPLE_NAME);
193. modulesPackage = (String) param.get(MODULES_PACKAGE);
194. methodName = (String) param.get(METHOD_NAME);
195. templateName = tpName;
196. }
197.
198. private static String getFileName() {
199. if (Constants.TEMPLATE_BO.equals(templateName)) {
200. return classSimpleName + FILE_BO_NAME_SUFFIX;
201. } else if (Constants.TEMPLATE_BOIMPL.equals(templateName)) {
202. return classSimpleName + FILE_BOIMPL_NAME_SUFFIX;
203. } else if (Constants.TEMPLATE_DAO.equals(templateName)) {
204. return classSimpleName + FILE_DAO_NAME_SUFFIX;
205. } else if (Constants.TEMPLATE_DAOIMPL.equals(templateName)) {
206. return classSimpleName + FILE_DAOIMPL_NAME_SUFFIX;
207. } else if (Constants.TEMPLATE_DTO.equals(templateName)) {
208. return classSimpleName + FILE_DTO_NAME_SUFFIX;
209. } else if (Constants.TEMPLATE_SQLMAP.equals(templateName)) {
210. return classSimpleName + FILE_SQL_MAP_SUFFIX;
211. } else if (Constants.TEMPLATE_SERVICE.equals(templateName)) {
212. return methodName + FILE_SERVICE_SUFFIX;
213. } else if (Constants.TEMPLATE_COMPONENT.equals(templateName)) {
214. return NamingRuleConvert.firstLetterToLowerCase(classSimpleName) + FILE_COMPONENT_SUFFIX;
215. } else if (Constants.TEMPLATE_CS.equals(templateName) || Constants.TEMPLATE_DS.equals(templateName) ||
216. Constants.TEMPLATE_MODULE.equals(templateName)) {
217. return FILE_MODULE_NAME;
218. }
219. return null;
220. }
221.
222. private static String getFileDir() {
223. String path = null;
224. if (packageName != null)
225. path = packageName.replaceAll("\\.", "/");
226. String servicePath = null;
227. if (!StringUtils.isEmpty(modulesPackage)) {
228. servicePath = modulesPackage.replaceAll("\\.", "/");
229. }
230. if (Constants.TEMPLATE_BO.equals(templateName)) {
231. return projectRoot + Constants.SRC_DIR + path + "/bo/";
232. } else if (Constants.TEMPLATE_BOIMPL.equals(templateName)) {
233. return projectRoot + Constants.SRC_DIR + path + "/bo/impl/";
234. } else if (Constants.TEMPLATE_DAO.equals(templateName)) {
235. return projectRoot + Constants.SRC_DIR + path + "/dao/";
236. } else if (Constants.TEMPLATE_DAOIMPL.equals(templateName)) {
237. return projectRoot + Constants.SRC_DIR + path + "/dao/impl/";
238. } else if (Constants.TEMPLATE_DTO.equals(templateName)) {
239. return projectRoot + Constants.SRC_DIR + path + "/dto/";
240. } else if (Constants.TEMPLATE_SQLMAP.equals(templateName)) {
241. return projectRoot + Constants.SRC_DIR + path + "/dao/maps/";
242. } else if (Constants.TEMPLATE_SERVICE.equals(templateName) || (Constants.TEMPLATE_CS.equals(templateName))) {
243. return projectRoot + "/modules/" + servicePath + "/cs/";
244. } else if (Constants.TEMPLATE_COMPONENT.equals(templateName) || Constants.TEMPLATE_DS.equals(templateName)) {
245. return projectRoot + "/modules/" + servicePath + "/ds/";
246. } else if (Constants.TEMPLATE_MODULE.equals(templateName)) {
247. return projectRoot + "/modules/" + servicePath + "/";
248. }
249. return null;
250. }
251.}
利用Freemarker实现表到功能界面的一键生成
2012-04-20 17:50:03
标签:执行文件 程序编写 表 工程 开发
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。/article/4461317.html
利用Freemarker产生项目所需的常用文件,可以根据一个表产生对应的ibtais的java及xml文件,及前台的JSP文件,这样可以大大的提高普通程序编写人员的开发效率,及从表到界面及CURD功能利用这个工程可以直接生成好,下面提供下最核心的文件产生类,代码如下:
另外提供编译过的工程文件,可执行文件,在附件中,由于大小控制,已经删除里面的freemarker-2.3.15.jar文件。
01.package com.hundsun.pms.generator.util;
02.
03.import com.hundsun.pms.generator.api.Constants;
04.import com.hundsun.pms.generator.model.page.Page;
05.import freemarker.template.Configuration;
06.import freemarker.template.DefaultObjectWrapper;
07.import freemarker.template.Template;
08.import freemarker.template.TemplateException;
09.import org.apache.commons.lang.StringUtils;
10.import org.springframework.util.ResourceUtils;
11.
12.import java.io.*;
13.import java.net.URL;
14.import java.util.Locale;
15.import java.util.Map;
16.
17./**
18. * Created by IntelliJ IDEA.
19. * User: pangj
20. * Date: 2012-3-6
21. * Time: 10:05:26
22. * 文件操作工具类
23. */
24.public class FreemarkerUtil {
25.
26. public static String PROJECT_ROOT = "projectRoot";
27.
28. public static String PACKAGE_NAME = "packageName";
29.
30. public static String CLASS_SIMPLE_NAME = "classSimpleName";
31.
32. public static String MODULES_PACKAGE = "modulesPackage";
33.
34. public static String METHOD_NAME = "methodName";
35.
36. //模板根目录
37. private static final String templateRootWithClasspath = "classpath*:com/hundsun/pms/generator/resource/template";
38.
39. private static final String templateRoot = "com/hundsun/pms/generator/resource/template";
40. //工程根目录
41. private static String projectRoot;
42. //包名
43. private static String packageName;
44. //服务所在的包路径
45. private static String modulesPackage;
46.
47. private static String classSimpleName;
48.
49. private static String templateName;
50.
51. private static String methodName;
52.
53. public static final String FILE_BO_NAME_SUFFIX = "Bo.java";
54.
55. public static final String FILE_BOIMPL_NAME_SUFFIX = "BoImpl.java";
56.
57. public static final String FILE_DAO_NAME_SUFFIX = "Dao.java";
58.
59. public static final String FILE_DAOIMPL_NAME_SUFFIX = "DaoImpl.java";
60.
61. public static final String FILE_DTO_NAME_SUFFIX = ".java";
62.
63. public static final String FILE_SQL_MAP_SUFFIX = ".xml";
64.
65. public static final String FILE_SERVICE_SUFFIX = ".service";
66.
67. public static final String FILE_COMPONENT_SUFFIX = ".component";
68.
69. public static final String FILE_MODULE_NAME = "module.xml";
70.
71. public static final String FILE_PAGE_SUFFIX = ".jsp";
72.
73.
74. /**
75. * 数据绑定至模板
76. *
77. * @param tpName 模板名
78. * @param param 参数
79. * @return
80. */
81. public static void process(String tpName, Map<String, Object> param) {
82. init(tpName, param);
83. Writer out = null;
84. try {
85. Template temp = getTemplate(templateName);
86. String fileDir = getFileDir();
87. File dir = new File(fileDir);
88. if (!dir.exists()) {
89. if (!dir.mkdirs()) {
90. MsgUtil.print("创建目录:" + fileDir + "失败");
91. return;
92. }
93. }
94. File f = new File(fileDir + getFileName());
95. out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));
96. try {
97. temp.setEncoding("utf-8");
98. temp.process(param, out);
99. MsgUtil.print("文件:" + getFileDir() + getFileName() + "生成完成!");
100. } catch (TemplateException e) {
101. MsgUtil.print("文件:" + getFileDir() + getFileName() + "生成失败,请检查原因!");
102. MsgUtil.print(e.getMessage());
103. }
104. out.flush();
105. out.close();
106. } catch (IOException e) {
107. e.printStackTrace();
108. MsgUtil.print(e.getMessage());
109. }
110. }
111.
112. /**
113. * 生成页面
114. *
115. * @param tpName 模板名
116. * @param jsp jsp页面全路径
117. * @param page 页面对象
118. */
119. public static void process(String tpName, String jsp, Page page) {
120. Writer writer = null;
121. try {
122. Template tmp = FreemarkerUtil.getTemplate(tpName);
123. String dir = jsp.substring(0, jsp.lastIndexOf("\\"));
124. File fdir = new File(dir);
125. if (!fdir.exists()) {
126. if (!fdir.mkdirs()) {
127. MsgUtil.print("创建目录:" + fdir + "失败");
128. return;
129. }
130. }
131. File file = new File(jsp);
132. writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
133. tmp.setEncoding("utf-8");
134. tmp.process(page, writer);
135. writer.flush();
136. writer.close();
137. MsgUtil.print("页面:" + jsp + " 生成成功!");
138. } catch (TemplateException e) {
139. e.printStackTrace();
140. MsgUtil.print(e.getMessage());
141. } catch (IOException e) {
142. e.printStackTrace();
143. MsgUtil.print(e.getMessage());
144. }
145. }
146.
147. public static Template getTemplate(String templateName) {
148. Configuration cfg = new Configuration();
149. Template temp = null;
150. URL path = FreemarkerUtil.class.getClassLoader().getSystemResource("");
151. File tmpRootFile = null;
152. if (path == null) {
153. tmpRootFile = getResourceURL("resource/template");
154. } else {
155. String tmpRoot = path + templateRoot;
156. tmpRoot = tmpRoot.replaceAll("file:/", "");
157. tmpRootFile = new File(tmpRoot);
158. }
159. if (tmpRootFile == null) {
160. throw new RuntimeException("无法取得模板根路径!");
161. }
162. try {
163. cfg.setDefaultEncoding("utf-8");
164. cfg.setOutputEncoding("utf-8");
165. cfg.setDirectoryForTemplateLoading(tmpRootFile);
166. /*cfg.setDirectoryForTemplateLoading(getResourceURL());*/
167. cfg.setObjectWrapper(new DefaultObjectWrapper());
168. temp = cfg.getTemplate(templateName);
169. } catch (IOException e) {
170. e.printStackTrace();
171. MsgUtil.print(e.getMessage());
172. }
173. return temp;
174. }
175.
176. public static File getResourceURL(String templatePath) {
177. try {
178. URL url = ResourceUtils.getURL(templateRootWithClasspath);
179. String path = url.getPath();
180. path = path.replace(templateRootWithClasspath, templatePath);
181. MsgUtil.print("File path is:"+path);
182. path = StringUtils.replace(path, "%20", " ");
183. return new File(path);
184. } catch (FileNotFoundException e) {
185. throw new RuntimeException("不能获取路径:" + templateRootWithClasspath);
186. }
187. }
188.
189. private static void init(String tpName, Map<String, Object> param) {
190. projectRoot = (String) param.get(PROJECT_ROOT);
191. packageName = (String) param.get(PACKAGE_NAME);
192. classSimpleName = (String) param.get(CLASS_SIMPLE_NAME);
193. modulesPackage = (String) param.get(MODULES_PACKAGE);
194. methodName = (String) param.get(METHOD_NAME);
195. templateName = tpName;
196. }
197.
198. private static String getFileName() {
199. if (Constants.TEMPLATE_BO.equals(templateName)) {
200. return classSimpleName + FILE_BO_NAME_SUFFIX;
201. } else if (Constants.TEMPLATE_BOIMPL.equals(templateName)) {
202. return classSimpleName + FILE_BOIMPL_NAME_SUFFIX;
203. } else if (Constants.TEMPLATE_DAO.equals(templateName)) {
204. return classSimpleName + FILE_DAO_NAME_SUFFIX;
205. } else if (Constants.TEMPLATE_DAOIMPL.equals(templateName)) {
206. return classSimpleName + FILE_DAOIMPL_NAME_SUFFIX;
207. } else if (Constants.TEMPLATE_DTO.equals(templateName)) {
208. return classSimpleName + FILE_DTO_NAME_SUFFIX;
209. } else if (Constants.TEMPLATE_SQLMAP.equals(templateName)) {
210. return classSimpleName + FILE_SQL_MAP_SUFFIX;
211. } else if (Constants.TEMPLATE_SERVICE.equals(templateName)) {
212. return methodName + FILE_SERVICE_SUFFIX;
213. } else if (Constants.TEMPLATE_COMPONENT.equals(templateName)) {
214. return NamingRuleConvert.firstLetterToLowerCase(classSimpleName) + FILE_COMPONENT_SUFFIX;
215. } else if (Constants.TEMPLATE_CS.equals(templateName) || Constants.TEMPLATE_DS.equals(templateName) ||
216. Constants.TEMPLATE_MODULE.equals(templateName)) {
217. return FILE_MODULE_NAME;
218. }
219. return null;
220. }
221.
222. private static String getFileDir() {
223. String path = null;
224. if (packageName != null)
225. path = packageName.replaceAll("\\.", "/");
226. String servicePath = null;
227. if (!StringUtils.isEmpty(modulesPackage)) {
228. servicePath = modulesPackage.replaceAll("\\.", "/");
229. }
230. if (Constants.TEMPLATE_BO.equals(templateName)) {
231. return projectRoot + Constants.SRC_DIR + path + "/bo/";
232. } else if (Constants.TEMPLATE_BOIMPL.equals(templateName)) {
233. return projectRoot + Constants.SRC_DIR + path + "/bo/impl/";
234. } else if (Constants.TEMPLATE_DAO.equals(templateName)) {
235. return projectRoot + Constants.SRC_DIR + path + "/dao/";
236. } else if (Constants.TEMPLATE_DAOIMPL.equals(templateName)) {
237. return projectRoot + Constants.SRC_DIR + path + "/dao/impl/";
238. } else if (Constants.TEMPLATE_DTO.equals(templateName)) {
239. return projectRoot + Constants.SRC_DIR + path + "/dto/";
240. } else if (Constants.TEMPLATE_SQLMAP.equals(templateName)) {
241. return projectRoot + Constants.SRC_DIR + path + "/dao/maps/";
242. } else if (Constants.TEMPLATE_SERVICE.equals(templateName) || (Constants.TEMPLATE_CS.equals(templateName))) {
243. return projectRoot + "/modules/" + servicePath + "/cs/";
244. } else if (Constants.TEMPLATE_COMPONENT.equals(templateName) || Constants.TEMPLATE_DS.equals(templateName)) {
245. return projectRoot + "/modules/" + servicePath + "/ds/";
246. } else if (Constants.TEMPLATE_MODULE.equals(templateName)) {
247. return projectRoot + "/modules/" + servicePath + "/";
248. }
249. return null;
250. }
251.}
相关文章推荐
- 利用Freemarker实现表到功能界面的一键生成 推荐
- 利用Freemarker生成html静态页面,也可利用模版功能生成不同格式的文件
- C#利用zxing.net生成二维码和条形码并实现打印的功能
- Android 利用工具实现一键自动findViewById功能
- Android-利用设备管理器来实现锁屏功能并可一键自我卸载
- 利用FreeMarker实现网页到Word文档的生成
- C#利用Zxing.net生成二维码和条形码并实现打印的功能
- 如何通过动态生成Html灵活实现DataGrid分类统计的界面显示功能 activeandbadboy [原作]
- 利用Freemarker实现网页静态化的功能
- C#利用Zxing.net生成条形码和二维码并实现打印的功能
- 利用DevExpress的LayoutControl控件实现界面的自动生成
- 如何通过动态生成Html灵活实现DataGrid分类统计的界面显示功能
- 利用GridView实现功能界面
- 如何通过动态生成Html灵活实现DataGrid分类统计的界面显示功能
- ssm 自定义注解实现mybatis自动维护表结构以及利用freemarker生成代码
- 利用Struts2进行Excel文件的生成及下载功能实现。
- 利用批处理实现一键搞定SVN更新、CMake生成工程文件、VS打开sln、开文件夹的偷懒目的
- 利用List集合实现一键退出功能
- ,有一款RESTFUL接口的文档在线自动生成+功能测试功能软件——Swagger UI,具体配置过程可移步《Spring Boot 利用 Swagger 实现restful测试》
- 如何通过动态生成Html灵活实现DataGrid分类统计的界面显示功能