package io.renren.utils;
import io.renren.config.MongoManager;
import io.renren.entity.ColumnEntity;
import io.renren.entity.TableEntity;
import io.renren.entity.mongo.MongoDefinition;
import io.renren.entity.mongo.MongoGeneratorEntity;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 代码生成器 工具类
*
* @author chenshun
* @email sunlightcs@gmail.com
* @date 2016年12月19日 下午11:40:24
*/
public class GenUtils {
private static String currentTableName;
public static List<String> getTemplates() {
List<String> templates = new ArrayList<String>();
templates.add("template/Entity.java.vm");
templates.add("template/Dao.xml.vm");
templates.add("template/menu.sql.vm");
templates.add("template/Service.java.vm");
templates.add("template/ServiceImpl.java.vm");
templates.add("template/Controller.java.vm");
templates.add("template/Dao.java.vm");
templates.add("template/index.vue.vm");
templates.add("template/add-or-update.vue.vm");
if (MongoManager.isMongo()) {
// mongo不需要mapper、sql 实体类需要替换
templates.remove(0);
templates.remove(1);
templates.remove(2);
templates.add("template/MongoEntity.java.vm");
}
return templates;
}
public static List<String> getMongoChildTemplates() {
List<String> templates = new ArrayList<String>();
templates.add("template/MongoChildrenEntity.java.vm");
return templates;
}
/**
* 生成代码
*/
public static void generatorCode(Map<String, String> table,
List<Map<String, String>> columns, ZipOutputStream zip) {
//配置信息
Configuration config = getConfig();
boolean hasBigDecimal = false;
boolean hasList = false;
//表信息
TableEntity tableEntity = new TableEntity();
tableEntity.setTableName(table.get("tableName"));
tableEntity.setComments(table.get("tableComment"));
//表名转换成Java类名
String className = tableToJava(tableEntity.getTableName(), config.getStringArray("tablePrefix"));
tableEntity.setClassName(className);
tableEntity.setClassname(StringUtils.uncapitalize(className));
//列信息
List<ColumnEntity> columsList = new ArrayList<>();
for (Map<String, String> column : columns) {
ColumnEntity columnEntity = new ColumnEntity();
columnEntity.setColumnName(column.get("columnName"));
columnEntity.setDataType(column.get("dataType"));
columnEntity.setComments(column.get("columnComment"));
columnEntity.setExtra(column.get("extra"));
//列名转换成Java属性名
String attrName = columnToJava(columnEntity.getColumnName());
columnEntity.setAttrName(attrName);
columnEntity.setAttrname(StringUtils.uncapitalize(attrName));
//列的数据类型,转换成Java类型
String attrType = config.getString(columnEntity.getDataType(), columnToJava(columnEntity.getDataType()));
columnEntity.setAttrType(attrType);
if (!hasBigDecimal && attrType.equals("BigDecimal")) {
hasBigDecimal = true;
}
if (!hasList && "array".equals(columnEntity.getExtra())) {
hasList = true;
}
//是否主键
if ("PRI".equalsIgnoreCase(column.get("columnKey")) && tableEntity.getPk() == null) {
tableEntity.setPk(columnEntity);
}
columsList.add(columnEntity);
}
tableEntity.setColumns(columsList);
//没主键,则第一个字段为主键
if (tableEntity.getPk() == null) {
tableEntity.setPk(tableEntity.getColumns().get(0));
}
//设置velocity资源加载器
Properties prop = new Properties();
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(prop);
String mainPath = config.getString("mainPath");
mainPath = StringUtils.isBlank(mainPath) ? "io.renren" : mainPath;
//封装模板数据
Map<String, Object> map = new HashMap<>();
map.put("tableName", tableEntity.getTableName());
map.put("comments", tableEntity.getComments());
map.put("pk", tableEntity.getPk());
map.put("className", tableEntity.getClassName());
map.put("classname", tableEntity.getClassname());
map.put("pathName", tableEntity.getClassname().toLowerCase());
map.put("columns", tableEntity.getColumns());
map.put("hasBigDecimal", hasBigDecimal);
map.put("hasList", hasList);
map.put("mainPath", mainPath);
map.put("package", config.getString("package"));
map.put("moduleName", config.getString("moduleName"));
map.put("author", config.getString("author"));
map.put("email", config.getString("email"));
map.put("datetime", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
VelocityContext context = new VelocityContext(map);
//获取模板列表
List<String> templates = getTemplates();
for (String template : templates) {
//渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, "UTF-8");
tpl.merge(context, sw);
try {
//添加到zip
zip.putNextEntry(new ZipEntry(getFileName(template, tableEntity.getClassName(), config.getString("package"), config.getString("moduleName"))));
IOUtils.write(sw.toString(), zip, "UTF-8");
IOUtils.closeQuietly(sw);
zip.closeEntry();
} catch (IOException e) {
throw new RRException("渲染模板失败,表名:" + tableEntity.getTableName(), e);
}
}
}
/**
* 生成mongo其他实体类的代码
*/
public static void generatorMongoCode(String[] tableNames, ZipOutputStream zip) {
for (String tableName : tableNames) {
MongoDefinition info = MongoManager.getInfo(tableName);
currentTableName = tableName;
List<MongoGeneratorEntity> childrenInfo = info.getChildrenInfo(tableName);
childrenInfo.remove(0);
for (MongoGeneratorEntity mongoGeneratorEntity : childrenInfo) {
generatorChildrenBeanCode(mongoGeneratorEntity, zip);
}
}
}
private static void generatorChildrenBeanCode(MongoGeneratorEntity mongoGeneratorEntity, ZipOutputStream zip) {
//配置信息
Configuration config = getConfig();
boolean hasList = false;
//表信息
TableEntity tableEntity = mongoGeneratorEntity.toTableEntity();
//表名转换成Java类名
String className = tableToJava(tableEntity.getTableName(), config.getStringArray("tablePrefix"));
tableEntity.setClassName(className);
tableEntity.setClassname(StringUtils.uncapitalize(className));
//列信息
List<ColumnEntity> columsList = new ArrayList<>();
for (Map<String, String> column : mongoGeneratorEntity.getColumns()) {
没有合适的资源?快使用搜索试试~ 我知道了~
人人开源项目的代码生成器,可在线生成entity、xml、dao、service、vue、sql代码,减少70%以上的开发任务

共98个文件
java:31个
js:15个
vm:11个

0 下载量 198 浏览量
2024-09-03
12:17:33
上传
评论
收藏 1.04MB ZIP 举报
温馨提示
人人开源项目的代码生成器,可在线生成entity、xml、dao、service、vue、sql代码,减少70%以上的开发任务
资源推荐
资源详情
资源评论
















收起资源包目录







































































































































共 98 条
- 1
资源评论


Java程序员-张凯
- 粉丝: 1w+
- 资源: 7754
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- S7-300 和S7-400编程语句表(STL)参考手册
- Deepseek智能助手本地化部署及其应用:Linux环境下的安装、Docker容器部署与对话系统集成
- 机器学习_AndrewNG_课程资料_学习辅助_1741397822.zip
- 机器学习与深度学习实验教程_Python_入门_教育_1741397125.zip
- 机器学习_快速学习要点_理论实战转换_入门教程_1741397080.zip
- COMP639 Flask Web应用程序设计:林肯社区露营地问题跟踪系统
- anaconda配置pytorch环境.md
- 机器视觉_OpenCV_图像处理与识别_教育与实践_1741397761.zip
- Web开发_Python_Flask_共享单车需求预测系统_1741397989.zip
- 机器学习_Python基础教程代码改写_学习工具_1741397853.zip
- anaconda配置pytorch环境.md
- 乐尚代驾项目总结文档.docx
- 知识领域_Python3_爬虫学习_入门教程_1741397340.zip
- anaconda配置pytorch环境.md
- 网络安全_AI_恶意代码分析_教学笔记_1741397779.zip
- ximinng_chatbot_1741397572.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
