代码生成器

代码生成器

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("自动生成代码完毕,刷新工程,修改映射文件,运行测试!");
		}
	}

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值