抽取重复代码到一个方法并修改方法为private级别

master
wangjianlong 5 years ago committed by Limy
parent 240d14d350
commit c0063765ce

@ -1,140 +1,135 @@
package com.ruoyi.generator.service.impl; package com.ruoyi.generator.service.impl;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.List; import java.util.List;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template; import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext; import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity; import org.apache.velocity.app.Velocity;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.ruoyi.common.constant.Constants; import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils; import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.generator.config.GenConfig; import com.ruoyi.generator.config.GenConfig;
import com.ruoyi.generator.domain.ColumnInfo; import com.ruoyi.generator.domain.ColumnInfo;
import com.ruoyi.generator.domain.TableInfo; import com.ruoyi.generator.domain.TableInfo;
import com.ruoyi.generator.mapper.GenMapper; import com.ruoyi.generator.mapper.GenMapper;
import com.ruoyi.generator.service.IGenService; import com.ruoyi.generator.service.IGenService;
import com.ruoyi.generator.util.GenUtils; import com.ruoyi.generator.util.GenUtils;
import com.ruoyi.generator.util.VelocityInitializer; import com.ruoyi.generator.util.VelocityInitializer;
/** /**
* *
* *
* @author ruoyi * @author ruoyi
*/ */
@Service @Service
public class GenServiceImpl implements IGenService public class GenServiceImpl implements IGenService
{ {
private static final Logger log = LoggerFactory.getLogger(GenServiceImpl.class); private static final Logger log = LoggerFactory.getLogger(GenServiceImpl.class);
@Autowired @Autowired
private GenMapper genMapper; private GenMapper genMapper;
/** /**
* ry * ry
* *
* @param tableInfo * @param tableInfo
* @return * @return
*/ */
@Override @Override
public List<TableInfo> selectTableList(TableInfo tableInfo) public List<TableInfo> selectTableList(TableInfo tableInfo)
{ {
return genMapper.selectTableList(tableInfo); return genMapper.selectTableList(tableInfo);
} }
/** /**
* *
* *
* @param tableName * @param tableName
* @return * @return
*/ */
@Override @Override
public byte[] generatorCode(String tableName) public byte[] generatorCode(String tableName)
{ {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream); ZipOutputStream zip = new ZipOutputStream(outputStream);
// 查询表信息 generatorCode(tableName, zip);
TableInfo table = genMapper.selectTableByName(tableName); IOUtils.closeQuietly(zip);
// 查询列信息 return outputStream.toByteArray();
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)
* @param tableNames {
* @return ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
*/ ZipOutputStream zip = new ZipOutputStream(outputStream);
@Override for (String tableName : tableNames)
public byte[] generatorCode(String[] tableNames) {
{ generatorCode(tableName, zip);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); }
ZipOutputStream zip = new ZipOutputStream(outputStream); IOUtils.closeQuietly(zip);
for (String tableName : tableNames) return outputStream.toByteArray();
{ }
// 查询表信息
TableInfo table = genMapper.selectTableByName(tableName); /**
// 查询列信息 *
List<ColumnInfo> columns = genMapper.selectTableColumnsByName(tableName); */
// 生成代码 private void generatorCode(String tableName, ZipOutputStream zip)
generatorCode(table, columns, zip); {
} // 查询表信息
IOUtils.closeQuietly(zip); TableInfo table = genMapper.selectTableByName(tableName);
return outputStream.toByteArray(); // 查询列信息
} List<ColumnInfo> columns = genMapper.selectTableColumnsByName(tableName);
/** // 表名转换成Java属性名
* String className = GenUtils.tableToJava(table.getTableName());
*/ table.setClassName(className);
public void generatorCode(TableInfo table, List<ColumnInfo> columns, ZipOutputStream zip) table.setClassname(StringUtils.uncapitalize(className));
{ // 列信息
// 表名转换成Java属性名 table.setColumns(GenUtils.transColums(columns));
String className = GenUtils.tableToJava(table.getTableName()); // 设置主键
table.setClassName(className); table.setPrimaryKey(table.getColumnsLast());
table.setClassname(StringUtils.uncapitalize(className));
// 列信息 VelocityInitializer.initVelocity();
table.setColumns(GenUtils.transColums(columns));
// 设置主键 String packageName = GenConfig.getPackageName();
table.setPrimaryKey(table.getColumnsLast()); String moduleName = GenUtils.getModuleName(packageName);
VelocityInitializer.initVelocity(); VelocityContext context = GenUtils.getVelocityContext(table);
String packageName = GenConfig.getPackageName(); // 获取模板列表
String moduleName = GenUtils.getModuleName(packageName); List<String> templates = GenUtils.getTemplates();
for (String template : templates)
VelocityContext context = GenUtils.getVelocityContext(table); {
// 渲染模板
// 获取模板列表 StringWriter sw = new StringWriter();
List<String> templates = GenUtils.getTemplates(); Template tpl = Velocity.getTemplate(template, Constants.UTF8);
for (String template : templates) tpl.merge(context, sw);
{ try
// 渲染模板 {
StringWriter sw = new StringWriter(); // 添加到zip
Template tpl = Velocity.getTemplate(template, Constants.UTF8); zip.putNextEntry(new ZipEntry(GenUtils.getFileName(template, table, moduleName)));
tpl.merge(context, sw); IOUtils.write(sw.toString(), zip, Constants.UTF8);
try IOUtils.closeQuietly(sw);
{ zip.closeEntry();
// 添加到zip }
zip.putNextEntry(new ZipEntry(GenUtils.getFileName(template, table, moduleName))); catch (IOException e)
IOUtils.write(sw.toString(), zip, Constants.UTF8); {
IOUtils.closeQuietly(sw); log.error("渲染模板失败,表名:" + table.getTableName(), e);
zip.closeEntry(); }
} }
catch (IOException e) }
{ }
log.error("渲染模板失败,表名:" + table.getTableName(), e);
}
}
}
}

Loading…
Cancel
Save