package org.unique.generator;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.unique.generator.model.Database;
import org.unique.generator.model.Table;

/**
 * 代码生成类
 * 
 * @ClassName: DomainGenerator
 * @author AndrewWen
 * @date 2013-1-14 下午8:45:30
 */
public class CodeGenerator {

	private Logger logger = Logger.getLogger(CodeGenerator.class);
	
	//是否去掉模块前面的编号
	public static String prefix = "";
	
	public boolean generator(final String classDriver, final  String url, final String username, final String password, 
			final String classPackage, final String author, final String contact, final String codePath) {

		String sourcePath = codePath + File.separator + "src/";
		Long start = System.currentTimeMillis();
		try {
			// 获取数据库名
			String schema = url.substring(url.lastIndexOf("/") + 1);
			
			// 获取数据库信息
			Database databaseBean = new DatabaseInfoOp(classDriver, url, username, password, schema).getDbInfo();
			
			// 获取该库所有表
			List<Table> tableList = databaseBean.getTableList();
			
			logger.info("---------------start---------------");
			
			/**
			 * 遍历生成代码
			 */
			for (Table table : tableList) {

				table.setPackageName(classPackage);
				
				Map<String, Object> map = new HashMap<String, Object>();

				map.put("table", table);
				map.put("contact", contact);
				map.put("author", author);

				VelocityInfoOp.generatorCode("model.vm", map, sourcePath + table.getPackagePath() + "/model", table.getClassName() + ".java");
				VelocityInfoOp.generatorCode("service.vm", map, sourcePath + table.getPackagePath() + "/service", table.getClassName() + "Service.java");
				VelocityInfoOp.generatorCode("serviceImpl.vm", map, sourcePath + table.getPackagePath() + "/service/impl", table.getClassName() + "ServiceImpl.java");
				VelocityInfoOp.generatorCode("controller.vm", map, sourcePath + table.getPackagePath() + "/controller", table.getClassName() + "Controller.java");
				
				logger.info("表:" + table.getTableName() + "成功");
			}

			logger.info("--------------- end time:" + (System.currentTimeMillis() - start) + "ms-----");
			logger.info("代码路径:" + codePath);
			logger.info("包:" + classPackage);
			logger.info("作者:" + author);
			logger.info("联系:" + contact);
			logger.info("------------------------------------");
			return true;
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return false;
	}
	
}
最近下载更多
978806986  LV16 2022年3月25日
675104182  LV14 2020年9月22日
Zhangshuzi  LV1 2020年6月25日
wei112233  LV15 2020年5月11日
mingpl  LV9 2020年2月16日
a243933863  LV1 2019年11月25日
werwrer  LV6 2019年11月4日
1165166678  LV1 2019年10月8日
648020894  LV1 2019年7月9日
398163660sj  LV1 2019年6月27日
最近浏览更多
微信网友_6234675614470144  LV1 2024年10月28日
sswert  LV2 2024年9月12日
3334004690  LV10 2024年6月24日
hellolu21  LV10 2024年5月16日
泓鼎168  LV20 2024年3月6日
漫步的海星  LV4 2023年9月25日
guviva  LV6 2023年6月12日
zzuljh  LV9 2023年4月25日
taoshen95  LV16 2023年4月13日
痴汉hug个人民 2023年2月21日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友