`
Algernoon
  • 浏览: 22888 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

代码生成器

 
阅读更多

代码生成器

package com.liuzm.mypss.test;

import java.io.File;
import java.io.FileWriter;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.junit.Test;

public class TemplateTest {
	// 开关:默认情况下已经存在的文件不需要生成代码
	// true:覆盖所有代码
	private static final boolean flag = false;
	// 1.那些domain需要生成代码
	private String[] domains = { "Product", "ProductType", "PurchaseBill",
			"PurchaseBillItem", "Supplier","SystemDictionaryDetail","SystemDictionaryType" };
	// 2.定义固定的目录路径:都是使用相对路径,规范:路径前面都不加/,路径的后面都加/
	private static final String JAVA = "src/main/java/";
	private static final String RESOURCES = "src/main/resources/";
	private static final String TEST = "test/main/java/";
	private static final String PACKAGE = "com/liuzm/mypss/";
	private static final String JS = "src/main/webapp/js/";
	private static final String VIEWS = "src/main/webapp/WEB-INF/views/";
	// 3.有那些模板需要生成
	private String[] templates = { "Action.java", "hbm.xml", "input.jsp",
			"list.js", "list.jsp", "Manager.xml", "Query.java", "Service.java",
			"ServiceImpl.java", "ServiceTest.java" };
	// 4.模板文件对应的生成文件路径
	private String[] files = { JAVA + PACKAGE + "web/",
			RESOURCES + PACKAGE + "domain/", VIEWS, JS, VIEWS,
			RESOURCES + "manager/", JAVA + PACKAGE + "query/",
			JAVA + PACKAGE + "service/", JAVA + PACKAGE + "service/impl/",
			TEST + PACKAGE + "test/" };

	@Test
	public void creatTemplate() throws Exception {
		if (templates.length != files.length) {
			throw new RuntimeException("模板数目和文件路径数目不匹配!");
		}

		VelocityContext context = new VelocityContext();
		// 5.外循环:domains
		for (int i = 0; i < domains.length; i++) {
			context.put("entity", domains[i]);
			// 6.处理domain首字母小写
			String lowerEntity = domains[i].substring(0, 1).toLowerCase()
					+ domains[i].substring(1);
			context.put("lowerEntity", lowerEntity);
			// 7.内循环:templates和files
			for (int j = 0; j < templates.length; j++) {
				Template template = Velocity.getTemplate("template/"
						+ templates[j], "UTF-8");
				// 8.实例化文件存放的路径
				File file = new File(files[j] + domains[i] + templates[j]);
				// 9.处理特殊的文件名称l
				if ("hbm.xml".equals(templates[j])) {
					file = new File(files[j] + domains[i] + "." + templates[j]);
				} else if ("input.jsp".equals(templates[j])) {
					file = new File(files[j] + lowerEntity + "/" + lowerEntity
							+ "-input.jsp");
				} else if ("list.js".equals(templates[j])) {
					file = new File(files[j] + lowerEntity + ".js");
				} else if ("list.jsp".equals(templates[j])) {
					file = new File(files[j] + lowerEntity + "/" + lowerEntity
							+ ".jsp");
				} else if ("Service.java".equals(templates[j])) {
					file = new File(files[j] + "I" + domains[i] + templates[j]);
				}
				System.out.println(file.getAbsolutePath());

				// 开关:默认情况下已经存在的文件不需要生成代码
				// true:覆盖所有代码
				// 表示代码已经存在 并且flag=false,不生成代码
				// 12.开关:默认情况下已经存在的文件不需要生成代码 true:覆盖所有代码
				if (file.exists() && !flag) {
					continue;
				}
				// 10.判断父目录是否存在
				File parentFile = file.getParentFile();
				if (!parentFile.exists()) {
					parentFile.mkdirs();
				}
				// 11.必须关闭流,写入内容
				FileWriter writer = new FileWriter(file);
				template.merge(context, writer);
				writer.close();

			}
			System.out.println("自动生成代码完毕,刷新工程,修改映射文件,运行测试!");
		}
	}

}

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics