若依 3.0
This commit is contained in:
@ -0,0 +1,87 @@
|
||||
package com.ruoyi.generator.domain;
|
||||
|
||||
/**
|
||||
* ry数据库表列信息
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class ColumnInfo
|
||||
{
|
||||
/** 字段名称 */
|
||||
private String columnName;
|
||||
|
||||
/** 字段类型 */
|
||||
private String dataType;
|
||||
|
||||
/** 列描述 */
|
||||
private String columnComment;
|
||||
|
||||
/** Java属性类型 */
|
||||
private String attrType;
|
||||
|
||||
/** Java属性名称(第一个字母大写),如:user_name => UserName */
|
||||
private String attrName;
|
||||
|
||||
/** Java属性名称(第一个字母小写),如:user_name => userName */
|
||||
private String attrname;
|
||||
|
||||
public String getColumnName()
|
||||
{
|
||||
return columnName;
|
||||
}
|
||||
|
||||
public void setColumnName(String columnName)
|
||||
{
|
||||
this.columnName = columnName;
|
||||
}
|
||||
|
||||
public String getDataType()
|
||||
{
|
||||
return dataType;
|
||||
}
|
||||
|
||||
public void setDataType(String dataType)
|
||||
{
|
||||
this.dataType = dataType;
|
||||
}
|
||||
|
||||
public String getColumnComment()
|
||||
{
|
||||
return columnComment;
|
||||
}
|
||||
|
||||
public void setColumnComment(String columnComment)
|
||||
{
|
||||
this.columnComment = columnComment;
|
||||
}
|
||||
|
||||
public String getAttrName()
|
||||
{
|
||||
return attrName;
|
||||
}
|
||||
|
||||
public void setAttrName(String attrName)
|
||||
{
|
||||
this.attrName = attrName;
|
||||
}
|
||||
|
||||
public String getAttrname()
|
||||
{
|
||||
return attrname;
|
||||
}
|
||||
|
||||
public void setAttrname(String attrname)
|
||||
{
|
||||
this.attrname = attrname;
|
||||
}
|
||||
|
||||
public String getAttrType()
|
||||
{
|
||||
return attrType;
|
||||
}
|
||||
|
||||
public void setAttrType(String attrType)
|
||||
{
|
||||
this.attrType = attrType;
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
package com.ruoyi.generator.domain;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.common.base.BaseEntity;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
|
||||
/**
|
||||
* ry 数据库表
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class TableInfo extends BaseEntity
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 表名称 */
|
||||
private String tableName;
|
||||
|
||||
/** 表描述 */
|
||||
private String tableComment;
|
||||
|
||||
/** 表的主键列信息 */
|
||||
private ColumnInfo primaryKey;
|
||||
|
||||
/** 表的列名(不包含主键) */
|
||||
private List<ColumnInfo> columns;
|
||||
|
||||
/** 类名(第一个字母大写) */
|
||||
private String className;
|
||||
|
||||
/** 类名(第一个字母小写) */
|
||||
private String classname;
|
||||
|
||||
public String getTableName()
|
||||
{
|
||||
return tableName;
|
||||
}
|
||||
|
||||
public void setTableName(String tableName)
|
||||
{
|
||||
this.tableName = tableName;
|
||||
}
|
||||
|
||||
public String getTableComment()
|
||||
{
|
||||
return tableComment;
|
||||
}
|
||||
|
||||
public void setTableComment(String tableComment)
|
||||
{
|
||||
this.tableComment = tableComment;
|
||||
}
|
||||
|
||||
public List<ColumnInfo> getColumns()
|
||||
{
|
||||
return columns;
|
||||
}
|
||||
|
||||
public ColumnInfo getColumnsLast()
|
||||
{
|
||||
ColumnInfo columnInfo = null;
|
||||
if (StringUtils.isNotNull(columns) && columns.size() > 0)
|
||||
{
|
||||
columnInfo = columns.get(0);
|
||||
}
|
||||
return columnInfo;
|
||||
}
|
||||
|
||||
public void setColumns(List<ColumnInfo> columns)
|
||||
{
|
||||
this.columns = columns;
|
||||
}
|
||||
|
||||
public String getClassName()
|
||||
{
|
||||
return className;
|
||||
}
|
||||
|
||||
public void setClassName(String className)
|
||||
{
|
||||
this.className = className;
|
||||
}
|
||||
|
||||
public String getClassname()
|
||||
{
|
||||
return classname;
|
||||
}
|
||||
|
||||
public void setClassname(String classname)
|
||||
{
|
||||
this.classname = classname;
|
||||
}
|
||||
|
||||
public ColumnInfo getPrimaryKey()
|
||||
{
|
||||
return primaryKey;
|
||||
}
|
||||
|
||||
public void setPrimaryKey(ColumnInfo primaryKey)
|
||||
{
|
||||
this.primaryKey = primaryKey;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.ruoyi.generator.mapper;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.generator.domain.ColumnInfo;
|
||||
import com.ruoyi.generator.domain.TableInfo;
|
||||
|
||||
/**
|
||||
* 代码生成 数据层
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public interface GenMapper
|
||||
{
|
||||
/**
|
||||
* 查询ry数据库表信息
|
||||
*
|
||||
* @param tableInfo 表信息
|
||||
* @return 数据库表列表
|
||||
*/
|
||||
public List<TableInfo> selectTableList(TableInfo tableInfo);
|
||||
|
||||
/**
|
||||
* 根据表名称查询信息
|
||||
*
|
||||
* @param tableName 表名称
|
||||
* @return 表信息
|
||||
*/
|
||||
public TableInfo selectTableByName(String tableName);
|
||||
|
||||
/**
|
||||
* 根据表名称查询列信息
|
||||
*
|
||||
* @param tableName 表名称
|
||||
* @return 列信息
|
||||
*/
|
||||
public List<ColumnInfo> selectTableColumnsByName(String tableName);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.ruoyi.generator.service;
|
||||
|
||||
import java.util.List;
|
||||
import com.ruoyi.generator.domain.TableInfo;
|
||||
|
||||
/**
|
||||
* 代码生成 服务层
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public interface IGenService
|
||||
{
|
||||
/**
|
||||
* 查询ry数据库表信息
|
||||
*
|
||||
* @param tableInfo 表信息
|
||||
* @return 数据库表列表
|
||||
*/
|
||||
public List<TableInfo> selectTableList(TableInfo tableInfo);
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
*
|
||||
* @param tableName 表名称
|
||||
* @return 数据
|
||||
*/
|
||||
public byte[] generatorCode(String tableName);
|
||||
|
||||
/**
|
||||
* 批量生成代码
|
||||
*
|
||||
* @param tableNames 表数组
|
||||
* @return 数据
|
||||
*/
|
||||
public byte[] generatorCode(String[] tableNames);
|
||||
}
|
@ -0,0 +1,140 @@
|
||||
package com.ruoyi.generator.service.impl;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.app.Velocity;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.ruoyi.common.config.Global;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.generator.domain.ColumnInfo;
|
||||
import com.ruoyi.generator.domain.TableInfo;
|
||||
import com.ruoyi.generator.mapper.GenMapper;
|
||||
import com.ruoyi.generator.service.IGenService;
|
||||
import com.ruoyi.generator.util.GenUtils;
|
||||
import com.ruoyi.generator.util.VelocityInitializer;
|
||||
|
||||
/**
|
||||
* 代码生成 服务层处理
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Service
|
||||
public class GenServiceImpl implements IGenService
|
||||
{
|
||||
private static final Logger log = LoggerFactory.getLogger(GenServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private GenMapper genMapper;
|
||||
|
||||
/**
|
||||
* 查询ry数据库表信息
|
||||
*
|
||||
* @param tableInfo 表信息
|
||||
* @return 数据库表列表
|
||||
*/
|
||||
@Override
|
||||
public List<TableInfo> selectTableList(TableInfo tableInfo)
|
||||
{
|
||||
return genMapper.selectTableList(tableInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
*
|
||||
* @param tableName 表名称
|
||||
* @return 数据
|
||||
*/
|
||||
@Override
|
||||
public byte[] generatorCode(String tableName)
|
||||
{
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
||||
// 查询表信息
|
||||
TableInfo table = genMapper.selectTableByName(tableName);
|
||||
// 查询列信息
|
||||
List<ColumnInfo> columns = genMapper.selectTableColumnsByName(tableName);
|
||||
// 生成代码
|
||||
generatorCode(table, columns, zip);
|
||||
IOUtils.closeQuietly(zip);
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量生成代码
|
||||
*
|
||||
* @param tableNames 表数组
|
||||
* @return 数据
|
||||
*/
|
||||
@Override
|
||||
public byte[] generatorCode(String[] tableNames)
|
||||
{
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
||||
for (String tableName : tableNames)
|
||||
{
|
||||
// 查询表信息
|
||||
TableInfo table = genMapper.selectTableByName(tableName);
|
||||
// 查询列信息
|
||||
List<ColumnInfo> columns = genMapper.selectTableColumnsByName(tableName);
|
||||
// 生成代码
|
||||
generatorCode(table, columns, zip);
|
||||
}
|
||||
IOUtils.closeQuietly(zip);
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
*/
|
||||
public void generatorCode(TableInfo table, List<ColumnInfo> columns, ZipOutputStream zip)
|
||||
{
|
||||
// 表名转换成Java属性名
|
||||
String className = GenUtils.tableToJava(table.getTableName());
|
||||
table.setClassName(className);
|
||||
table.setClassname(StringUtils.uncapitalize(className));
|
||||
// 列信息
|
||||
table.setColumns(GenUtils.transColums(columns));
|
||||
// 设置主键
|
||||
table.setPrimaryKey(table.getColumnsLast());
|
||||
|
||||
VelocityInitializer.initVelocity();
|
||||
|
||||
String packageName = Global.getPackageName();
|
||||
String moduleName = GenUtils.getModuleName(packageName);
|
||||
|
||||
VelocityContext context = GenUtils.getVelocityContext(table);
|
||||
|
||||
// 获取模板列表
|
||||
List<String> templates = GenUtils.getTemplates();
|
||||
for (String template : templates)
|
||||
{
|
||||
// 渲染模板
|
||||
StringWriter sw = new StringWriter();
|
||||
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
|
||||
tpl.merge(context, sw);
|
||||
try
|
||||
{
|
||||
// 添加到zip
|
||||
zip.putNextEntry(new ZipEntry(GenUtils.getFileName(template, table, moduleName)));
|
||||
IOUtils.write(sw.toString(), zip, Constants.UTF8);
|
||||
IOUtils.closeQuietly(sw);
|
||||
zip.closeEntry();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
log.error("渲染模板失败,表名:" + table.getTableName(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,234 @@
|
||||
package com.ruoyi.generator.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import com.ruoyi.common.config.Global;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.utils.DateUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.generator.domain.ColumnInfo;
|
||||
import com.ruoyi.generator.domain.TableInfo;
|
||||
|
||||
/**
|
||||
* 代码生成器 工具类
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class GenUtils
|
||||
{
|
||||
/** 项目空间路径 */
|
||||
private static final String PROJECT_PATH = "main/java/com/ruoyi/project";
|
||||
|
||||
/** mybatis空间路径 */
|
||||
private static final String MYBATIS_PATH = "main/resources/mybatis";
|
||||
|
||||
/** html空间路径 */
|
||||
private static final String TEMPLATES_PATH = "main/resources/templates";
|
||||
|
||||
/** 类型转换 */
|
||||
public static Map<String, String> javaTypeMap = new HashMap<String, String>();
|
||||
|
||||
/**
|
||||
* 设置列信息
|
||||
*/
|
||||
public static List<ColumnInfo> transColums(List<ColumnInfo> columns)
|
||||
{
|
||||
// 列信息
|
||||
List<ColumnInfo> columsList = new ArrayList<>();
|
||||
for (ColumnInfo column : columns)
|
||||
{
|
||||
// 列名转换成Java属性名
|
||||
String attrName = StringUtils.convertToCamelCase(column.getColumnName());
|
||||
column.setAttrName(attrName);
|
||||
column.setAttrname(StringUtils.uncapitalize(attrName));
|
||||
|
||||
// 列的数据类型,转换成Java类型
|
||||
String attrType = javaTypeMap.get(column.getDataType());
|
||||
column.setAttrType(attrType);
|
||||
|
||||
columsList.add(column);
|
||||
}
|
||||
return columsList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模板信息
|
||||
*
|
||||
* @return 模板列表
|
||||
*/
|
||||
public static VelocityContext getVelocityContext(TableInfo table)
|
||||
{
|
||||
// java对象数据传递到模板文件vm
|
||||
VelocityContext velocityContext = new VelocityContext();
|
||||
String packageName = Global.getPackageName();
|
||||
velocityContext.put("tableName", table.getTableName());
|
||||
velocityContext.put("tableComment", replaceKeyword(table.getTableComment()));
|
||||
velocityContext.put("primaryKey", table.getPrimaryKey());
|
||||
velocityContext.put("className", table.getClassName());
|
||||
velocityContext.put("classname", table.getClassname());
|
||||
velocityContext.put("moduleName", GenUtils.getModuleName(packageName));
|
||||
velocityContext.put("columns", table.getColumns());
|
||||
velocityContext.put("package", packageName + "." + table.getClassname());
|
||||
velocityContext.put("author", Global.getAuthor());
|
||||
velocityContext.put("datetime", DateUtils.getDate());
|
||||
return velocityContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模板信息
|
||||
*
|
||||
* @return 模板列表
|
||||
*/
|
||||
public static List<String> getTemplates()
|
||||
{
|
||||
List<String> templates = new ArrayList<String>();
|
||||
templates.add("templates/vm/java/domain.java.vm");
|
||||
templates.add("templates/vm/java/Mapper.java.vm");
|
||||
templates.add("templates/vm/java/Service.java.vm");
|
||||
templates.add("templates/vm/java/ServiceImpl.java.vm");
|
||||
templates.add("templates/vm/java/Controller.java.vm");
|
||||
templates.add("templates/vm/xml/Mapper.xml.vm");
|
||||
templates.add("templates/vm/html/list.html.vm");
|
||||
templates.add("templates/vm/html/add.html.vm");
|
||||
templates.add("templates/vm/html/edit.html.vm");
|
||||
templates.add("templates/vm/sql/sql.vm");
|
||||
return templates;
|
||||
}
|
||||
|
||||
/**
|
||||
* 表名转换成Java类名
|
||||
*/
|
||||
public static String tableToJava(String tableName)
|
||||
{
|
||||
if (Constants.AUTO_REOMVE_PRE.equals(Global.getAutoRemovePre()))
|
||||
{
|
||||
tableName = tableName.substring(tableName.indexOf("_") + 1);
|
||||
}
|
||||
if (StringUtils.isNotEmpty(Global.getTablePrefix()))
|
||||
{
|
||||
tableName = tableName.replace(Global.getTablePrefix(), "");
|
||||
}
|
||||
return StringUtils.convertToCamelCase(tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件名
|
||||
*/
|
||||
public static String getFileName(String template, TableInfo table, String moduleName)
|
||||
{
|
||||
// 小写类名
|
||||
String classname = table.getClassname();
|
||||
// 大写类名
|
||||
String className = table.getClassName();
|
||||
String javaPath = PROJECT_PATH + "/" + moduleName + "/";
|
||||
String mybatisPath = MYBATIS_PATH + "/" + moduleName + "/" + className;
|
||||
String htmlPath = TEMPLATES_PATH + "/" + moduleName + "/" + classname;
|
||||
|
||||
if (StringUtils.isNotEmpty(classname))
|
||||
{
|
||||
javaPath += classname.replace(".", "/") + "/";
|
||||
}
|
||||
|
||||
if (template.contains("domain.java.vm"))
|
||||
{
|
||||
return javaPath + "domain" + "/" + className + ".java";
|
||||
}
|
||||
|
||||
if (template.contains("Mapper.java.vm"))
|
||||
{
|
||||
return javaPath + "mapper" + "/" + className + "Mapper.java";
|
||||
}
|
||||
|
||||
if (template.contains("Service.java.vm"))
|
||||
{
|
||||
return javaPath + "service" + "/" + "I" + className + "Service.java";
|
||||
}
|
||||
|
||||
if (template.contains("ServiceImpl.java.vm"))
|
||||
{
|
||||
return javaPath + "service" + "/" + className + "ServiceImpl.java";
|
||||
}
|
||||
|
||||
if (template.contains("Controller.java.vm"))
|
||||
{
|
||||
return javaPath + "controller" + "/" + className + "Controller.java";
|
||||
}
|
||||
|
||||
if (template.contains("Mapper.xml.vm"))
|
||||
{
|
||||
return mybatisPath + "Mapper.xml";
|
||||
}
|
||||
|
||||
if (template.contains("list.html.vm"))
|
||||
{
|
||||
return htmlPath + "/" + classname + ".html";
|
||||
}
|
||||
if (template.contains("add.html.vm"))
|
||||
{
|
||||
return htmlPath + "/" + "add.html";
|
||||
}
|
||||
if (template.contains("edit.html.vm"))
|
||||
{
|
||||
return htmlPath + "/" + "edit.html";
|
||||
}
|
||||
if (template.contains("sql.vm"))
|
||||
{
|
||||
return classname + "Menu.sql";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模块名
|
||||
*
|
||||
* @param packageName 包名
|
||||
* @return 模块名
|
||||
*/
|
||||
public static String getModuleName(String packageName)
|
||||
{
|
||||
int lastIndex = packageName.lastIndexOf(".");
|
||||
int nameLength = packageName.length();
|
||||
String moduleName = StringUtils.substring(packageName, lastIndex + 1, nameLength);
|
||||
return moduleName;
|
||||
}
|
||||
|
||||
public static String replaceKeyword(String keyword)
|
||||
{
|
||||
String keyName = keyword.replaceAll("(?:表|信息)", "");
|
||||
return keyName;
|
||||
}
|
||||
|
||||
static
|
||||
{
|
||||
javaTypeMap.put("tinyint", "Integer");
|
||||
javaTypeMap.put("smallint", "Integer");
|
||||
javaTypeMap.put("mediumint", "Integer");
|
||||
javaTypeMap.put("int", "Integer");
|
||||
javaTypeMap.put("integer", "integer");
|
||||
javaTypeMap.put("bigint", "Long");
|
||||
javaTypeMap.put("float", "Float");
|
||||
javaTypeMap.put("double", "Double");
|
||||
javaTypeMap.put("decimal", "BigDecimal");
|
||||
javaTypeMap.put("bit", "Boolean");
|
||||
javaTypeMap.put("char", "String");
|
||||
javaTypeMap.put("varchar", "String");
|
||||
javaTypeMap.put("tinytext", "String");
|
||||
javaTypeMap.put("text", "String");
|
||||
javaTypeMap.put("mediumtext", "String");
|
||||
javaTypeMap.put("longtext", "String");
|
||||
javaTypeMap.put("time", "Date");
|
||||
javaTypeMap.put("date", "Date");
|
||||
javaTypeMap.put("datetime", "Date");
|
||||
javaTypeMap.put("timestamp", "Date");
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
System.out.println(StringUtils.convertToCamelCase("user_name"));
|
||||
System.out.println(replaceKeyword("岗位信息表"));
|
||||
System.out.println(getModuleName("com.ruoyi.project.system"));
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.ruoyi.generator.util;
|
||||
|
||||
import java.util.Properties;
|
||||
import org.apache.velocity.app.Velocity;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
|
||||
/**
|
||||
* VelocityEngine工厂
|
||||
*
|
||||
* @author RuoYi
|
||||
*/
|
||||
public class VelocityInitializer
|
||||
{
|
||||
/**
|
||||
* 初始化vm方法
|
||||
*/
|
||||
public static void initVelocity()
|
||||
{
|
||||
Properties p = new Properties();
|
||||
try
|
||||
{
|
||||
// 加载classpath目录下的vm文件
|
||||
p.setProperty("file.resource.loader.class",
|
||||
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
||||
// 定义字符集
|
||||
p.setProperty(Velocity.ENCODING_DEFAULT, Constants.UTF8);
|
||||
p.setProperty(Velocity.OUTPUT_ENCODING, Constants.UTF8);
|
||||
// 初始化Velocity引擎,指定配置Properties
|
||||
Velocity.init(p);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user