/**
* 生成代码(单表)
*
* @param tableName
*/
@RequestMapping("/code/{tableName}")
public R code(@PathVariable("tableName") String tableName) {
String[] tableNames = new String[]{tableName};
byte[] data = generatorService.generatorCode(tableNames);
FileUtil.byte2File(data, codePath);
return success();
}
service
byte[] generatorCode(String[] tableNames);
impl
@Override
public byte[] generatorCode(String[] tableNames) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
for(String tableName : tableNames){
//查询表信息
Map<String, String> table = generatorMapper.get(tableName);
//查询列信息
List<Map<String, String>> columns = generatorMapper.listColumns(tableName);
//生成代码
GenUtils.generatorCode(table, columns, zip);
}
IOUtils.closeQuietly(zip);
return outputStream.toByteArray();
}
mapper
@Select("select table_name tableName, engine, table_comment tableComment, create_time createTime from information_schema.tables \r\n"
+ " where table_schema = (select database()) and table_name = #{tableName}")
Map<String, String> get(String tableName);
mapper
@Select("select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columns\r\n"
+ " where table_name = #{tableName} and table_schema = (select database()) order by ordinal_position")
List<Map<String, String>> listColumns(String tableName);
方法
/**
* 生成代码
*/
public static void generatorCode(Map<String, String> table,
List<Map<String, String>> columns, ZipOutputStream zip) {
//配置信息
Configuration config = getConfig();
//表信息
TableDO tableDO = new TableDO();
tableDO.setTableName(table.get("tableName"));
tableDO.setComments(table.get("tableComment"));
//表名转换成Java类名
String className = tableToJava(tableDO.getTableName(), config.getString("tablePrefix"), config.getString("autoRemovePre"));
tableDO.setClassName(className);
tableDO.setClassname(StringUtils.uncapitalize(className));
//列信息
List<ColumnDO> columsList = new ArrayList<>();
for (Map<String, String> column : columns) {
ColumnDO columnDO = new ColumnDO();
columnDO.setColumnName(column.get("columnName"));
columnDO.setDataType(column.get("dataType"));
columnDO.setComments(column.get("columnComment"));
columnDO.setExtra(column.get("extra"));
//列名转换成Java属性名
String attrName = columnToJava(columnDO.getColumnName());
columnDO.setAttrName(attrName);
columnDO.setAttrname(StringUtils.uncapitalize(attrName));
//列的数据类型,转换成Java类型
String attrType = config.getString(columnDO.getDataType(), "unknowType");
columnDO.setAttrType(attrType);
//是否主键
if ("PRI".equalsIgnoreCase(column.get("columnKey")) && tableDO.getPk() == null) {
tableDO.setPk(columnDO);
}
columsList.add(columnDO);
}
tableDO.setColumns(columsList);
//没主键,则第一个字段为主键
if (tableDO.getPk() == null) {
tableDO.setPk(tableDO.getColumns().get(0));
}
//设置velocity资源加载器
Properties prop = new Properties();
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(prop);
//封装模板数据
Map<String, Object> map = new HashMap<>(16);
//表名
map.put("tableName", tableDO.getTableName());
//注释
map.put("comments", tableDO.getComments());
map.put("pk", tableDO.getPk());
//首字母大写
map.put("className", tableDO.getClassName());
//首字母小写
map.put("classname", tableDO.getClassname());
map.put("pathName", config.getString("package").substring(config.getString("package").lastIndexOf(".") + 1));
map.put("columns", tableDO.getColumns());
map.put("package", config.getString("package"));
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, tableDO.getClassname(), tableDO.getClassName(), config.getString("package").substring(config.getString("package").lastIndexOf(".") + 1))));
IOUtils.write(sw.toString(), zip, "UTF-8");
IOUtils.closeQuietly(sw);
zip.closeEntry();
} catch (IOException e) {
throw new BDException("渲染模板失败,表名:" + tableDO.getTableName(), e);
}
}
}