From 74834fd24ee4398f7c54e8859ff394cd4730ddfc Mon Sep 17 00:00:00 2001 From: wangjianlong Date: Mon, 27 May 2019 14:41:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E5=8F=96=E9=87=8D=E5=A4=8D=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=88=B0=E4=B8=80=E4=B8=AA=E6=96=B9=E6=B3=95=E5=B9=B6?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=B9=E6=B3=95=E4=B8=BAprivate=E7=BA=A7?= =?UTF-8?q?=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/GenServiceImpl.java | 275 +++++++++--------- 1 file changed, 135 insertions(+), 140 deletions(-) diff --git a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenServiceImpl.java b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenServiceImpl.java index e352ad98f..f960ea38d 100644 --- a/ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenServiceImpl.java +++ b/ruoyi-generator/src/main/java/com/ruoyi/generator/service/impl/GenServiceImpl.java @@ -1,140 +1,135 @@ -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.constant.Constants; -import com.ruoyi.common.utils.StringUtils; -import com.ruoyi.generator.config.GenConfig; -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 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 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 columns = genMapper.selectTableColumnsByName(tableName); - // 生成代码 - generatorCode(table, columns, zip); - } - IOUtils.closeQuietly(zip); - return outputStream.toByteArray(); - } - - /** - * 生成代码 - */ - public void generatorCode(TableInfo table, List 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 = GenConfig.getPackageName(); - String moduleName = GenUtils.getModuleName(packageName); - - VelocityContext context = GenUtils.getVelocityContext(table); - - // 获取模板列表 - List 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); - } - } - } -} +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.constant.Constants; +import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.generator.config.GenConfig; +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 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); + generatorCode(tableName, 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) + { + generatorCode(tableName, zip); + } + IOUtils.closeQuietly(zip); + return outputStream.toByteArray(); + } + + /** + * 查询表信息并生成代码 + */ + private void generatorCode(String tableName, ZipOutputStream zip) + { + // 查询表信息 + TableInfo table = genMapper.selectTableByName(tableName); + // 查询列信息 + List columns = genMapper.selectTableColumnsByName(tableName); + + // 表名转换成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 = GenConfig.getPackageName(); + String moduleName = GenUtils.getModuleName(packageName); + + VelocityContext context = GenUtils.getVelocityContext(table); + + // 获取模板列表 + List 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); + } + } + } +}