全项目代码格式化

This commit is contained in:
疯狂的狮子li
2021-12-31 16:31:17 +08:00
parent a53db40b49
commit db9fd081e6
239 changed files with 4509 additions and 9471 deletions

View File

@@ -1,24 +1,22 @@
package com.ruoyi.gen;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.ruoyi.common.security.annotation.EnableCustomConfig;
import com.ruoyi.common.security.annotation.EnableRyFeignClients;
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 代码生成
*
*
* @author ruoyi
*/
@EnableCustomConfig
@EnableCustomSwagger2
@EnableCustomSwagger2
@EnableRyFeignClients
@SpringBootApplication
public class RuoYiGenApplication
{
public static void main(String[] args)
{
public class RuoYiGenApplication {
public static void main(String[] args) {
SpringApplication.run(RuoYiGenApplication.class, args);
System.out.println("(♥◠‿◠)ノ゙ 代码生成模块启动成功 ლ(´ڡ`ლ)゙ ");
}

View File

@@ -5,62 +5,61 @@ import org.springframework.stereotype.Component;
/**
* 代码生成相关配置
*
*
* @author ruoyi
*/
@Component
@ConfigurationProperties(prefix = "gen")
public class GenConfig
{
/** 作者 */
public class GenConfig {
/**
* 作者
*/
public static String author;
/** 生成包路径 */
/**
* 生成包路径
*/
public static String packageName;
/** 自动去除表前缀默认是false */
/**
* 自动去除表前缀默认是false
*/
public static boolean autoRemovePre;
/** 表前缀(类名不会包含表前缀) */
/**
* 表前缀(类名不会包含表前缀)
*/
public static String tablePrefix;
public static String getAuthor()
{
public static String getAuthor() {
return author;
}
public void setAuthor(String author)
{
public void setAuthor(String author) {
GenConfig.author = author;
}
public static String getPackageName()
{
public static String getPackageName() {
return packageName;
}
public void setPackageName(String packageName)
{
public void setPackageName(String packageName) {
GenConfig.packageName = packageName;
}
public static boolean getAutoRemovePre()
{
public static boolean getAutoRemovePre() {
return autoRemovePre;
}
public void setAutoRemovePre(boolean autoRemovePre)
{
public void setAutoRemovePre(boolean autoRemovePre) {
GenConfig.autoRemovePre = autoRemovePre;
}
public static String getTablePrefix()
{
public static String getTablePrefix() {
return tablePrefix;
}
public void setTablePrefix(String tablePrefix)
{
public void setTablePrefix(String tablePrefix) {
GenConfig.tablePrefix = tablePrefix;
}
}

View File

@@ -1,22 +1,6 @@
package com.ruoyi.gen.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.text.Convert;
import cn.hutool.core.convert.Convert;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
@@ -27,29 +11,37 @@ import com.ruoyi.gen.domain.GenTable;
import com.ruoyi.gen.domain.GenTableColumn;
import com.ruoyi.gen.service.IGenTableColumnService;
import com.ruoyi.gen.service.IGenTableService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 代码生成 操作处理
*
*
* @author ruoyi
*/
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RequestMapping("/gen")
@RestController
public class GenController extends BaseController
{
@Autowired
private IGenTableService genTableService;
public class GenController extends BaseController {
@Autowired
private IGenTableColumnService genTableColumnService;
private final IGenTableService genTableService;
private final IGenTableColumnService genTableColumnService;
/**
* 查询代码生成列表
*/
@RequiresPermissions("tool:gen:list")
@GetMapping("/list")
public TableDataInfo genList(GenTable genTable)
{
public TableDataInfo genList(GenTable genTable) {
startPage();
List<GenTable> list = genTableService.selectGenTableList(genTable);
return getDataTable(list);
@@ -60,8 +52,7 @@ public class GenController extends BaseController
*/
@RequiresPermissions("tool:gen:query")
@GetMapping(value = "/{talbleId}")
public AjaxResult getInfo(@PathVariable Long talbleId)
{
public AjaxResult getInfo(@PathVariable Long talbleId) {
GenTable table = genTableService.selectGenTableById(talbleId);
List<GenTable> tables = genTableService.selectGenTableAll();
List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(talbleId);
@@ -77,8 +68,7 @@ public class GenController extends BaseController
*/
@RequiresPermissions("tool:gen:list")
@GetMapping("/db/list")
public TableDataInfo dataList(GenTable genTable)
{
public TableDataInfo dataList(GenTable genTable) {
startPage();
List<GenTable> list = genTableService.selectDbTableList(genTable);
return getDataTable(list);
@@ -88,8 +78,7 @@ public class GenController extends BaseController
* 查询数据表字段列表
*/
@GetMapping(value = "/column/{talbleId}")
public TableDataInfo columnList(Long tableId)
{
public TableDataInfo columnList(Long tableId) {
TableDataInfo dataInfo = new TableDataInfo();
List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(tableId);
dataInfo.setRows(list);
@@ -103,8 +92,7 @@ public class GenController extends BaseController
@RequiresPermissions("tool:gen:import")
@Log(title = "代码生成", businessType = BusinessType.IMPORT)
@PostMapping("/importTable")
public AjaxResult importTableSave(String tables)
{
public AjaxResult importTableSave(String tables) {
String[] tableNames = Convert.toStrArray(tables);
// 查询表信息
List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
@@ -118,8 +106,7 @@ public class GenController extends BaseController
@RequiresPermissions("tool:gen:edit")
@Log(title = "代码生成", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult editSave(@Validated @RequestBody GenTable genTable)
{
public AjaxResult editSave(@Validated @RequestBody GenTable genTable) {
genTableService.validateEdit(genTable);
genTableService.updateGenTable(genTable);
return AjaxResult.success();
@@ -131,8 +118,7 @@ public class GenController extends BaseController
@RequiresPermissions("tool:gen:remove")
@Log(title = "代码生成", businessType = BusinessType.DELETE)
@DeleteMapping("/{tableIds}")
public AjaxResult remove(@PathVariable Long[] tableIds)
{
public AjaxResult remove(@PathVariable Long[] tableIds) {
genTableService.deleteGenTableByIds(tableIds);
return AjaxResult.success();
}
@@ -142,8 +128,7 @@ public class GenController extends BaseController
*/
@RequiresPermissions("tool:gen:preview")
@GetMapping("/preview/{tableId}")
public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException
{
public AjaxResult preview(@PathVariable("tableId") Long tableId) throws IOException {
Map<String, String> dataMap = genTableService.previewCode(tableId);
return AjaxResult.success(dataMap);
}
@@ -154,8 +139,7 @@ public class GenController extends BaseController
@RequiresPermissions("tool:gen:code")
@Log(title = "代码生成", businessType = BusinessType.GENCODE)
@GetMapping("/download/{tableName}")
public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException
{
public void download(HttpServletResponse response, @PathVariable("tableName") String tableName) throws IOException {
byte[] data = genTableService.downloadCode(tableName);
genCode(response, data);
}
@@ -166,8 +150,7 @@ public class GenController extends BaseController
@RequiresPermissions("tool:gen:code")
@Log(title = "代码生成", businessType = BusinessType.GENCODE)
@GetMapping("/genCode/{tableName}")
public AjaxResult genCode(@PathVariable("tableName") String tableName)
{
public AjaxResult genCode(@PathVariable("tableName") String tableName) {
genTableService.generatorCode(tableName);
return AjaxResult.success();
}
@@ -178,8 +161,7 @@ public class GenController extends BaseController
@RequiresPermissions("tool:gen:edit")
@Log(title = "代码生成", businessType = BusinessType.UPDATE)
@GetMapping("/synchDb/{tableName}")
public AjaxResult synchDb(@PathVariable("tableName") String tableName)
{
public AjaxResult synchDb(@PathVariable("tableName") String tableName) {
genTableService.synchDb(tableName);
return AjaxResult.success();
}
@@ -190,8 +172,7 @@ public class GenController extends BaseController
@RequiresPermissions("tool:gen:code")
@Log(title = "代码生成", businessType = BusinessType.GENCODE)
@GetMapping("/batchGenCode")
public void batchGenCode(HttpServletResponse response, String tables) throws IOException
{
public void batchGenCode(HttpServletResponse response, String tables) throws IOException {
String[] tableNames = Convert.toStrArray(tables);
byte[] data = genTableService.downloadCode(tableNames);
genCode(response, data);
@@ -200,8 +181,7 @@ public class GenController extends BaseController
/**
* 生成zip文件
*/
private void genCode(HttpServletResponse response, byte[] data) throws IOException
{
private void genCode(HttpServletResponse response, byte[] data) throws IOException {
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\"ruoyi.zip\"");
response.addHeader("Content-Length", "" + data.length);

View File

@@ -1,15 +1,17 @@
package com.ruoyi.gen.domain;
import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.ArrayUtils;
import com.ruoyi.common.core.constant.GenConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.ArrayUtils;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
/**
* 业务表 gen_table
@@ -21,7 +23,6 @@ import com.ruoyi.common.core.web.domain.BaseEntity;
@NoArgsConstructor
@Accessors(chain = true)
public class GenTable extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* 编号

View File

@@ -1,12 +1,14 @@
package com.ruoyi.gen.domain;
import javax.validation.constraints.NotBlank;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.BaseEntity;
import lombok.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import javax.validation.constraints.NotBlank;
/**
* 代码生成业务字段表 gen_table_column
*
@@ -17,7 +19,6 @@ import lombok.experimental.Accessors;
@NoArgsConstructor
@Accessors(chain = true)
public class GenTableColumn extends BaseEntity {
private static final long serialVersionUID = 1L;
/**
* 编号

View File

@@ -1,60 +1,60 @@
package com.ruoyi.gen.mapper;
import java.util.List;
import com.ruoyi.gen.domain.GenTableColumn;
import java.util.List;
/**
* 业务字段 数据层
*
*
* @author ruoyi
*/
public interface GenTableColumnMapper
{
public interface GenTableColumnMapper {
/**
* 根据表名称查询列信息
*
*
* @param tableName 表名称
* @return 列信息
*/
public List<GenTableColumn> selectDbTableColumnsByName(String tableName);
List<GenTableColumn> selectDbTableColumnsByName(String tableName);
/**
* 查询业务字段列表
*
*
* @param tableId 业务字段编号
* @return 业务字段集合
*/
public List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId);
List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId);
/**
* 新增业务字段
*
*
* @param genTableColumn 业务字段信息
* @return 结果
*/
public int insertGenTableColumn(GenTableColumn genTableColumn);
int insertGenTableColumn(GenTableColumn genTableColumn);
/**
* 修改业务字段
*
*
* @param genTableColumn 业务字段信息
* @return 结果
*/
public int updateGenTableColumn(GenTableColumn genTableColumn);
int updateGenTableColumn(GenTableColumn genTableColumn);
/**
* 删除业务字段
*
*
* @param genTableColumns 列数据
* @return 结果
*/
public int deleteGenTableColumns(List<GenTableColumn> genTableColumns);
int deleteGenTableColumns(List<GenTableColumn> genTableColumns);
/**
* 批量删除业务字段
*
*
* @param ids 需要删除的数据ID
* @return 结果
*/
public int deleteGenTableColumnByIds(Long[] ids);
int deleteGenTableColumnByIds(Long[] ids);
}

View File

@@ -1,83 +1,83 @@
package com.ruoyi.gen.mapper;
import java.util.List;
import com.ruoyi.gen.domain.GenTable;
import java.util.List;
/**
* 业务 数据层
*
*
* @author ruoyi
*/
public interface GenTableMapper
{
public interface GenTableMapper {
/**
* 查询业务列表
*
*
* @param genTable 业务信息
* @return 业务集合
*/
public List<GenTable> selectGenTableList(GenTable genTable);
List<GenTable> selectGenTableList(GenTable genTable);
/**
* 查询据库列表
*
*
* @param genTable 业务信息
* @return 数据库表集合
*/
public List<GenTable> selectDbTableList(GenTable genTable);
List<GenTable> selectDbTableList(GenTable genTable);
/**
* 查询据库列表
*
*
* @param tableNames 表名称组
* @return 数据库表集合
*/
public List<GenTable> selectDbTableListByNames(String[] tableNames);
List<GenTable> selectDbTableListByNames(String[] tableNames);
/**
* 查询所有表信息
*
*
* @return 表信息集合
*/
public List<GenTable> selectGenTableAll();
List<GenTable> selectGenTableAll();
/**
* 查询表ID业务信息
*
*
* @param id 业务ID
* @return 业务信息
*/
public GenTable selectGenTableById(Long id);
GenTable selectGenTableById(Long id);
/**
* 查询表名称业务信息
*
*
* @param tableName 表名称
* @return 业务信息
*/
public GenTable selectGenTableByName(String tableName);
GenTable selectGenTableByName(String tableName);
/**
* 新增业务
*
*
* @param genTable 业务信息
* @return 结果
*/
public int insertGenTable(GenTable genTable);
int insertGenTable(GenTable genTable);
/**
* 修改业务
*
*
* @param genTable 业务信息
* @return 结果
*/
public int updateGenTable(GenTable genTable);
int updateGenTable(GenTable genTable);
/**
* 批量删除业务
*
*
* @param ids 需要删除的数据ID
* @return 结果
*/
public int deleteGenTableByIds(Long[] ids);
int deleteGenTableByIds(Long[] ids);
}

View File

@@ -1,68 +1,66 @@
package com.ruoyi.gen.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;
import cn.hutool.core.convert.Convert;
import com.ruoyi.gen.domain.GenTableColumn;
import com.ruoyi.gen.mapper.GenTableColumnMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 业务字段 服务层实现
*
*
* @author ruoyi
*/
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Service
public class GenTableColumnServiceImpl implements IGenTableColumnService
{
@Autowired
private GenTableColumnMapper genTableColumnMapper;
public class GenTableColumnServiceImpl implements IGenTableColumnService {
/**
private final GenTableColumnMapper genTableColumnMapper;
/**
* 查询业务字段列表
*
*
* @param tableId 业务字段编号
* @return 业务字段集合
*/
@Override
public List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId)
{
return genTableColumnMapper.selectGenTableColumnListByTableId(tableId);
}
@Override
public List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId) {
return genTableColumnMapper.selectGenTableColumnListByTableId(tableId);
}
/**
* 新增业务字段
*
*
* @param genTableColumn 业务字段信息
* @return 结果
*/
@Override
public int insertGenTableColumn(GenTableColumn genTableColumn)
{
return genTableColumnMapper.insertGenTableColumn(genTableColumn);
}
/**
* 修改业务字段
*
* @param genTableColumn 业务字段信息
* @return 结果
*/
@Override
public int updateGenTableColumn(GenTableColumn genTableColumn)
{
return genTableColumnMapper.updateGenTableColumn(genTableColumn);
}
@Override
public int insertGenTableColumn(GenTableColumn genTableColumn) {
return genTableColumnMapper.insertGenTableColumn(genTableColumn);
}
/**
/**
* 修改业务字段
*
* @param genTableColumn 业务字段信息
* @return 结果
*/
@Override
public int updateGenTableColumn(GenTableColumn genTableColumn) {
return genTableColumnMapper.updateGenTableColumn(genTableColumn);
}
/**
* 删除业务字段对象
*
*
* @param ids 需要删除的数据ID
* @return 结果
*/
@Override
public int deleteGenTableColumnByIds(String ids)
{
return genTableColumnMapper.deleteGenTableColumnByIds(Convert.toLongArray(ids));
}
@Override
public int deleteGenTableColumnByIds(String ids) {
return genTableColumnMapper.deleteGenTableColumnByIds(Convert.toLongArray(ids));
}
}

View File

@@ -1,31 +1,10 @@
package com.ruoyi.gen.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.FileUtils;
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 org.springframework.transaction.annotation.Transactional;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.GenConstants;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.text.CharsetKit;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.gen.domain.GenTable;
@@ -35,32 +14,50 @@ import com.ruoyi.gen.mapper.GenTableMapper;
import com.ruoyi.gen.util.GenUtils;
import com.ruoyi.gen.util.VelocityInitializer;
import com.ruoyi.gen.util.VelocityUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 业务 服务层实现
*
*
* @author ruoyi
*/
@Slf4j
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Service
public class GenTableServiceImpl implements IGenTableService
{
private static final Logger log = LoggerFactory.getLogger(GenTableServiceImpl.class);
public class GenTableServiceImpl implements IGenTableService {
@Autowired
private GenTableMapper genTableMapper;
@Autowired
private GenTableColumnMapper genTableColumnMapper;
private final GenTableMapper genTableMapper;
private final GenTableColumnMapper genTableColumnMapper;
/**
* 查询业务信息
*
*
* @param id 业务ID
* @return 业务信息
*/
@Override
public GenTable selectGenTableById(Long id)
{
public GenTable selectGenTableById(Long id) {
GenTable genTable = genTableMapper.selectGenTableById(id);
setTableFromOptions(genTable);
return genTable;
@@ -68,68 +65,61 @@ public class GenTableServiceImpl implements IGenTableService
/**
* 查询业务列表
*
*
* @param genTable 业务信息
* @return 业务集合
*/
@Override
public List<GenTable> selectGenTableList(GenTable genTable)
{
public List<GenTable> selectGenTableList(GenTable genTable) {
return genTableMapper.selectGenTableList(genTable);
}
/**
* 查询据库列表
*
*
* @param genTable 业务信息
* @return 数据库表集合
*/
@Override
public List<GenTable> selectDbTableList(GenTable genTable)
{
public List<GenTable> selectDbTableList(GenTable genTable) {
return genTableMapper.selectDbTableList(genTable);
}
/**
* 查询据库列表
*
*
* @param tableNames 表名称组
* @return 数据库表集合
*/
@Override
public List<GenTable> selectDbTableListByNames(String[] tableNames)
{
public List<GenTable> selectDbTableListByNames(String[] tableNames) {
return genTableMapper.selectDbTableListByNames(tableNames);
}
/**
* 查询所有表信息
*
*
* @return 表信息集合
*/
@Override
public List<GenTable> selectGenTableAll()
{
public List<GenTable> selectGenTableAll() {
return genTableMapper.selectGenTableAll();
}
/**
* 修改业务
*
*
* @param genTable 业务信息
* @return 结果
*/
@Override
@Transactional
public void updateGenTable(GenTable genTable)
{
public void updateGenTable(GenTable genTable) {
String options = JSON.toJSONString(genTable.getParams());
genTable.setOptions(options);
int row = genTableMapper.updateGenTable(genTable);
if (row > 0)
{
for (GenTableColumn cenTableColumn : genTable.getColumns())
{
if (row > 0) {
for (GenTableColumn cenTableColumn : genTable.getColumns()) {
genTableColumnMapper.updateGenTableColumn(cenTableColumn);
}
}
@@ -137,62 +127,53 @@ public class GenTableServiceImpl implements IGenTableService
/**
* 删除业务对象
*
*
* @param tableIds 需要删除的数据ID
* @return 结果
*/
@Override
@Transactional
public void deleteGenTableByIds(Long[] tableIds)
{
public void deleteGenTableByIds(Long[] tableIds) {
genTableMapper.deleteGenTableByIds(tableIds);
genTableColumnMapper.deleteGenTableColumnByIds(tableIds);
}
/**
* 导入表结构
*
*
* @param tableList 导入表列表
*/
@Override
@Transactional
public void importGenTable(List<GenTable> tableList)
{
public void importGenTable(List<GenTable> tableList) {
String operName = SecurityUtils.getUsername();
try
{
for (GenTable table : tableList)
{
try {
for (GenTable table : tableList) {
String tableName = table.getTableName();
GenUtils.initTable(table, operName);
int row = genTableMapper.insertGenTable(table);
if (row > 0)
{
if (row > 0) {
// 保存列信息
List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
for (GenTableColumn column : genTableColumns)
{
for (GenTableColumn column : genTableColumns) {
GenUtils.initColumnField(column, table);
genTableColumnMapper.insertGenTableColumn(column);
}
}
}
}
catch (Exception e)
{
} catch (Exception e) {
throw new ServiceException("导入失败:" + e.getMessage());
}
}
/**
* 预览代码
*
*
* @param tableId 表编号
* @return 预览数据列表
*/
@Override
public Map<String, String> previewCode(Long tableId)
{
public Map<String, String> previewCode(Long tableId) {
Map<String, String> dataMap = new LinkedHashMap<>();
// 查询表信息
GenTable table = genTableMapper.selectGenTableById(tableId);
@@ -206,8 +187,7 @@ public class GenTableServiceImpl implements IGenTableService
// 获取模板列表
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
for (String template : templates)
{
for (String template : templates) {
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
@@ -219,13 +199,12 @@ public class GenTableServiceImpl implements IGenTableService
/**
* 生成代码(下载方式)
*
*
* @param tableName 表名称
* @return 数据
*/
@Override
public byte[] downloadCode(String tableName)
{
public byte[] downloadCode(String tableName) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
generatorCode(tableName, zip);
@@ -235,12 +214,11 @@ public class GenTableServiceImpl implements IGenTableService
/**
* 生成代码(自定义路径)
*
*
* @param tableName 表名称
*/
@Override
public void generatorCode(String tableName)
{
public void generatorCode(String tableName) {
// 查询表信息
GenTable table = genTableMapper.selectGenTableByName(tableName);
// 设置主子表信息
@@ -254,21 +232,16 @@ public class GenTableServiceImpl implements IGenTableService
// 获取模板列表
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
for (String template : templates)
{
if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm"))
{
for (String template : templates) {
if (!StringUtils.containsAny(template, "sql.vm", "api.js.vm", "index.vue.vm", "index-tree.vue.vm")) {
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
tpl.merge(context, sw);
try
{
try {
String path = getGenPath(table, template);
FileUtils.writeStringToFile(new File(path), sw.toString(), CharsetKit.UTF_8);
}
catch (IOException e)
{
FileUtils.writeStringToFile(new File(path), sw.toString(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new ServiceException("渲染模板失败,表名:" + table.getTableName());
}
}
@@ -277,52 +250,46 @@ public class GenTableServiceImpl implements IGenTableService
/**
* 同步数据库
*
*
* @param tableName 表名称
*/
@Override
@Transactional
public void synchDb(String tableName)
{
public void synchDb(String tableName) {
GenTable table = genTableMapper.selectGenTableByName(tableName);
List<GenTableColumn> tableColumns = table.getColumns();
List<String> tableColumnNames = tableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
List<GenTableColumn> dbTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
if (StringUtils.isEmpty(dbTableColumns))
{
if (StringUtils.isEmpty(dbTableColumns)) {
throw new ServiceException("同步数据失败,原表结构不存在");
}
List<String> dbTableColumnNames = dbTableColumns.stream().map(GenTableColumn::getColumnName).collect(Collectors.toList());
dbTableColumns.forEach(column -> {
if (!tableColumnNames.contains(column.getColumnName()))
{
if (!tableColumnNames.contains(column.getColumnName())) {
GenUtils.initColumnField(column, table);
genTableColumnMapper.insertGenTableColumn(column);
}
});
List<GenTableColumn> delColumns = tableColumns.stream().filter(column -> !dbTableColumnNames.contains(column.getColumnName())).collect(Collectors.toList());
if (StringUtils.isNotEmpty(delColumns))
{
if (StringUtils.isNotEmpty(delColumns)) {
genTableColumnMapper.deleteGenTableColumns(delColumns);
}
}
/**
* 批量生成代码(下载方式)
*
*
* @param tableNames 表数组
* @return 数据
*/
@Override
public byte[] downloadCode(String[] tableNames)
{
public byte[] downloadCode(String[] tableNames) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zip = new ZipOutputStream(outputStream);
for (String tableName : tableNames)
{
for (String tableName : tableNames) {
generatorCode(tableName, zip);
}
IOUtils.closeQuietly(zip);
@@ -332,8 +299,7 @@ public class GenTableServiceImpl implements IGenTableService
/**
* 查询表信息并生成代码
*/
private void generatorCode(String tableName, ZipOutputStream zip)
{
private void generatorCode(String tableName, ZipOutputStream zip) {
// 查询表信息
GenTable table = genTableMapper.selectGenTableByName(tableName);
// 设置主子表信息
@@ -347,23 +313,19 @@ public class GenTableServiceImpl implements IGenTableService
// 获取模板列表
List<String> templates = VelocityUtils.getTemplateList(table.getTplCategory());
for (String template : templates)
{
for (String template : templates) {
// 渲染模板
StringWriter sw = new StringWriter();
Template tpl = Velocity.getTemplate(template, Constants.UTF8);
tpl.merge(context, sw);
try
{
try {
// 添加到zip
zip.putNextEntry(new ZipEntry(VelocityUtils.getFileName(template, table)));
IOUtils.write(sw.toString(), zip, Constants.UTF8);
IOUtils.closeQuietly(sw);
zip.flush();
zip.flush();
zip.closeEntry();
}
catch (IOException e)
{
} catch (IOException e) {
log.error("渲染模板失败,表名:" + table.getTableName(), e);
}
}
@@ -371,36 +333,24 @@ public class GenTableServiceImpl implements IGenTableService
/**
* 修改保存参数校验
*
*
* @param genTable 业务信息
*/
@Override
public void validateEdit(GenTable genTable)
{
if (GenConstants.TPL_TREE.equals(genTable.getTplCategory()))
{
public void validateEdit(GenTable genTable) {
if (GenConstants.TPL_TREE.equals(genTable.getTplCategory())) {
String options = JSON.toJSONString(genTable.getParams());
JSONObject paramsObj = JSONObject.parseObject(options);
if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE)))
{
if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_CODE))) {
throw new ServiceException("树编码字段不能为空");
}
else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE)))
{
} else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_PARENT_CODE))) {
throw new ServiceException("树父编码字段不能为空");
}
else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME)))
{
} else if (StringUtils.isEmpty(paramsObj.getString(GenConstants.TREE_NAME))) {
throw new ServiceException("树名称字段不能为空");
}
else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory()))
{
if (StringUtils.isEmpty(genTable.getSubTableName()))
{
} else if (GenConstants.TPL_SUB.equals(genTable.getTplCategory())) {
if (StringUtils.isEmpty(genTable.getSubTableName())) {
throw new ServiceException("关联子表的表名不能为空");
}
else if (StringUtils.isEmpty(genTable.getSubTableFkName()))
{
} else if (StringUtils.isEmpty(genTable.getSubTableFkName())) {
throw new ServiceException("子表关联的外键名不能为空");
}
}
@@ -409,69 +359,58 @@ public class GenTableServiceImpl implements IGenTableService
/**
* 设置主键列信息
*
*
* @param table 业务表信息
*/
public void setPkColumn(GenTable table)
{
for (GenTableColumn column : table.getColumns())
{
if (column.isPk())
{
public void setPkColumn(GenTable table) {
for (GenTableColumn column : table.getColumns()) {
if (column.isPk()) {
table.setPkColumn(column);
break;
}
}
if (StringUtils.isNull(table.getPkColumn()))
{
if (StringUtils.isNull(table.getPkColumn())) {
table.setPkColumn(table.getColumns().get(0));
}
if (GenConstants.TPL_SUB.equals(table.getTplCategory()))
{
for (GenTableColumn column : table.getSubTable().getColumns())
{
if (column.isPk())
{
if (GenConstants.TPL_SUB.equals(table.getTplCategory())) {
for (GenTableColumn column : table.getSubTable().getColumns()) {
if (column.isPk()) {
table.getSubTable().setPkColumn(column);
break;
}
}
if (StringUtils.isNull(table.getSubTable().getPkColumn()))
{
if (StringUtils.isNull(table.getSubTable().getPkColumn())) {
table.getSubTable().setPkColumn(table.getSubTable().getColumns().get(0));
}
}
}
/**
* 设置主子表信息
*
*
* @param table 业务表信息
*/
public void setSubTable(GenTable table)
{
public void setSubTable(GenTable table) {
String subTableName = table.getSubTableName();
if (StringUtils.isNotEmpty(subTableName))
{
if (StringUtils.isNotEmpty(subTableName)) {
table.setSubTable(genTableMapper.selectGenTableByName(subTableName));
}
}
/**
* 设置代码生成其他选项值
*
*
* @param genTable 设置后的生成对象
*/
public void setTableFromOptions(GenTable genTable)
{
public void setTableFromOptions(GenTable genTable) {
JSONObject paramsObj = JSONObject.parseObject(genTable.getOptions());
if (StringUtils.isNotNull(paramsObj))
{
if (StringUtils.isNotNull(paramsObj)) {
String treeCode = paramsObj.getString(GenConstants.TREE_CODE);
String treeParentCode = paramsObj.getString(GenConstants.TREE_PARENT_CODE);
String treeName = paramsObj.getString(GenConstants.TREE_NAME);
String parentMenuId = paramsObj.getString(GenConstants.PARENT_MENU_ID);
String parentMenuName = paramsObj.getString(GenConstants.PARENT_MENU_NAME);
genTable.setTreeCode(treeCode);
genTable.setTreeParentCode(treeParentCode);
genTable.setTreeName(treeName);
@@ -482,16 +421,14 @@ public class GenTableServiceImpl implements IGenTableService
/**
* 获取代码生成地址
*
* @param table 业务表信息
*
* @param table 业务表信息
* @param template 模板文件路径
* @return 生成地址
*/
public static String getGenPath(GenTable table, String template)
{
public static String getGenPath(GenTable table, String template) {
String genPath = table.getGenPath();
if (StringUtils.equals(genPath, "/"))
{
if (StringUtils.equals(genPath, "/")) {
return System.getProperty("user.dir") + File.separator + "src" + File.separator + VelocityUtils.getFileName(template, table);
}
return genPath + File.separator + VelocityUtils.getFileName(template, table);

View File

@@ -1,44 +1,44 @@
package com.ruoyi.gen.service;
import java.util.List;
import com.ruoyi.gen.domain.GenTableColumn;
import java.util.List;
/**
* 业务字段 服务层
*
*
* @author ruoyi
*/
public interface IGenTableColumnService
{
public interface IGenTableColumnService {
/**
* 查询业务字段列表
*
*
* @param tableId 业务字段编号
* @return 业务字段集合
*/
public List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId);
List<GenTableColumn> selectGenTableColumnListByTableId(Long tableId);
/**
* 新增业务字段
*
*
* @param genTableColumn 业务字段信息
* @return 结果
*/
public int insertGenTableColumn(GenTableColumn genTableColumn);
int insertGenTableColumn(GenTableColumn genTableColumn);
/**
* 修改业务字段
*
*
* @param genTableColumn 业务字段信息
* @return 结果
*/
public int updateGenTableColumn(GenTableColumn genTableColumn);
int updateGenTableColumn(GenTableColumn genTableColumn);
/**
* 删除业务字段信息
*
*
* @param ids 需要删除的数据ID
* @return 结果
*/
public int deleteGenTableColumnByIds(String ids);
int deleteGenTableColumnByIds(String ids);
}

View File

@@ -1,121 +1,121 @@
package com.ruoyi.gen.service;
import com.ruoyi.gen.domain.GenTable;
import java.util.List;
import java.util.Map;
import com.ruoyi.gen.domain.GenTable;
/**
* 业务 服务层
*
*
* @author ruoyi
*/
public interface IGenTableService
{
public interface IGenTableService {
/**
* 查询业务列表
*
*
* @param genTable 业务信息
* @return 业务集合
*/
public List<GenTable> selectGenTableList(GenTable genTable);
List<GenTable> selectGenTableList(GenTable genTable);
/**
* 查询据库列表
*
*
* @param genTable 业务信息
* @return 数据库表集合
*/
public List<GenTable> selectDbTableList(GenTable genTable);
List<GenTable> selectDbTableList(GenTable genTable);
/**
* 查询据库列表
*
*
* @param tableNames 表名称组
* @return 数据库表集合
*/
public List<GenTable> selectDbTableListByNames(String[] tableNames);
List<GenTable> selectDbTableListByNames(String[] tableNames);
/**
* 查询所有表信息
*
*
* @return 表信息集合
*/
public List<GenTable> selectGenTableAll();
List<GenTable> selectGenTableAll();
/**
* 查询业务信息
*
*
* @param id 业务ID
* @return 业务信息
*/
public GenTable selectGenTableById(Long id);
GenTable selectGenTableById(Long id);
/**
* 修改业务
*
*
* @param genTable 业务信息
* @return 结果
*/
public void updateGenTable(GenTable genTable);
void updateGenTable(GenTable genTable);
/**
* 删除业务信息
*
*
* @param tableIds 需要删除的表数据ID
* @return 结果
*/
public void deleteGenTableByIds(Long[] tableIds);
void deleteGenTableByIds(Long[] tableIds);
/**
* 导入表结构
*
*
* @param tableList 导入表列表
*/
public void importGenTable(List<GenTable> tableList);
void importGenTable(List<GenTable> tableList);
/**
* 预览代码
*
*
* @param tableId 表编号
* @return 预览数据列表
*/
public Map<String, String> previewCode(Long tableId);
Map<String, String> previewCode(Long tableId);
/**
* 生成代码(下载方式)
*
*
* @param tableName 表名称
* @return 数据
*/
public byte[] downloadCode(String tableName);
byte[] downloadCode(String tableName);
/**
* 生成代码(自定义路径)
*
*
* @param tableName 表名称
* @return 数据
*/
public void generatorCode(String tableName);
void generatorCode(String tableName);
/**
* 同步数据库
*
*
* @param tableName 表名称
*/
public void synchDb(String tableName);
void synchDb(String tableName);
/**
* 批量生成代码(下载方式)
*
*
* @param tableNames 表数组
* @return 数据
*/
public byte[] downloadCode(String[] tableNames);
byte[] downloadCode(String[] tableNames);
/**
* 修改保存参数校验
*
*
* @param genTable 业务信息
*/
public void validateEdit(GenTable genTable);
void validateEdit(GenTable genTable);
}

View File

@@ -1,25 +1,24 @@
package com.ruoyi.gen.util;
import java.util.Arrays;
import org.apache.commons.lang3.RegExUtils;
import com.ruoyi.common.core.constant.GenConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.gen.config.GenConfig;
import com.ruoyi.gen.domain.GenTable;
import com.ruoyi.gen.domain.GenTableColumn;
import org.apache.commons.lang3.RegExUtils;
import java.util.Arrays;
/**
* 代码生成器 工具类
*
*
* @author ruoyi
*/
public class GenUtils
{
public class GenUtils {
/**
* 初始化表信息
*/
public static void initTable(GenTable genTable, String operName)
{
public static void initTable(GenTable genTable, String operName) {
genTable.setClassName(convertClassName(genTable.getTableName()));
genTable.setPackageName(GenConfig.getPackageName());
genTable.setModuleName(getModuleName(GenConfig.getPackageName()));
@@ -32,8 +31,7 @@ public class GenUtils
/**
* 初始化列属性字段
*/
public static void initColumnField(GenTableColumn column, GenTable table)
{
public static void initColumnField(GenTableColumn column, GenTable table) {
String dataType = getDbType(column.getColumnType());
String columnName = column.getColumnName();
column.setTableId(table.getTableId());
@@ -43,36 +41,28 @@ public class GenUtils
// 设置默认类型
column.setJavaType(GenConstants.TYPE_STRING);
if (arraysContains(GenConstants.COLUMNTYPE_STR, dataType) || arraysContains(GenConstants.COLUMNTYPE_TEXT, dataType))
{
if (arraysContains(GenConstants.COLUMNTYPE_STR, dataType) || arraysContains(GenConstants.COLUMNTYPE_TEXT, dataType)) {
// 字符串长度超过500设置为文本域
Integer columnLength = getColumnLength(column.getColumnType());
String htmlType = columnLength >= 500 || arraysContains(GenConstants.COLUMNTYPE_TEXT, dataType) ? GenConstants.HTML_TEXTAREA : GenConstants.HTML_INPUT;
column.setHtmlType(htmlType);
}
else if (arraysContains(GenConstants.COLUMNTYPE_TIME, dataType))
{
} else if (arraysContains(GenConstants.COLUMNTYPE_TIME, dataType)) {
column.setJavaType(GenConstants.TYPE_DATE);
column.setHtmlType(GenConstants.HTML_DATETIME);
}
else if (arraysContains(GenConstants.COLUMNTYPE_NUMBER, dataType))
{
} else if (arraysContains(GenConstants.COLUMNTYPE_NUMBER, dataType)) {
column.setHtmlType(GenConstants.HTML_INPUT);
// 如果是浮点型 统一用BigDecimal
String[] str = StringUtils.split(StringUtils.substringBetween(column.getColumnType(), "(", ")"), ",");
if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0)
{
if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0) {
column.setJavaType(GenConstants.TYPE_BIGDECIMAL);
}
// 如果是整形
else if (str != null && str.length == 1 && Integer.parseInt(str[0]) <= 10)
{
else if (str != null && str.length == 1 && Integer.parseInt(str[0]) <= 10) {
column.setJavaType(GenConstants.TYPE_INTEGER);
}
// 长整形
else
{
else {
column.setJavaType(GenConstants.TYPE_LONG);
}
}
@@ -81,74 +71,63 @@ public class GenUtils
column.setIsInsert(GenConstants.REQUIRE);
// 编辑字段
if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName) && !column.isPk())
{
if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName) && !column.isPk()) {
column.setIsEdit(GenConstants.REQUIRE);
}
// 列表字段
if (!arraysContains(GenConstants.COLUMNNAME_NOT_LIST, columnName) && !column.isPk())
{
if (!arraysContains(GenConstants.COLUMNNAME_NOT_LIST, columnName) && !column.isPk()) {
column.setIsList(GenConstants.REQUIRE);
}
// 查询字段
if (!arraysContains(GenConstants.COLUMNNAME_NOT_QUERY, columnName) && !column.isPk())
{
if (!arraysContains(GenConstants.COLUMNNAME_NOT_QUERY, columnName) && !column.isPk()) {
column.setIsQuery(GenConstants.REQUIRE);
}
// 查询字段类型
if (StringUtils.endsWithIgnoreCase(columnName, "name"))
{
if (StringUtils.endsWithIgnoreCase(columnName, "name")) {
column.setQueryType(GenConstants.QUERY_LIKE);
}
// 状态字段设置单选框
if (StringUtils.endsWithIgnoreCase(columnName, "status"))
{
if (StringUtils.endsWithIgnoreCase(columnName, "status")) {
column.setHtmlType(GenConstants.HTML_RADIO);
}
// 类型&性别字段设置下拉框
else if (StringUtils.endsWithIgnoreCase(columnName, "type")
|| StringUtils.endsWithIgnoreCase(columnName, "sex"))
{
|| StringUtils.endsWithIgnoreCase(columnName, "sex")) {
column.setHtmlType(GenConstants.HTML_SELECT);
}
// 图片字段设置图片上传控件
else if (StringUtils.endsWithIgnoreCase(columnName, "image"))
{
else if (StringUtils.endsWithIgnoreCase(columnName, "image")) {
column.setHtmlType(GenConstants.HTML_IMAGE_UPLOAD);
}
// 文件字段设置文件上传控件
else if (StringUtils.endsWithIgnoreCase(columnName, "file"))
{
else if (StringUtils.endsWithIgnoreCase(columnName, "file")) {
column.setHtmlType(GenConstants.HTML_FILE_UPLOAD);
}
// 内容字段设置富文本控件
else if (StringUtils.endsWithIgnoreCase(columnName, "content"))
{
else if (StringUtils.endsWithIgnoreCase(columnName, "content")) {
column.setHtmlType(GenConstants.HTML_EDITOR);
}
}
/**
* 校验数组是否包含指定值
*
* @param arr 数组
*
* @param arr 数组
* @param targetValue 值
* @return 是否包含
*/
public static boolean arraysContains(String[] arr, String targetValue)
{
public static boolean arraysContains(String[] arr, String targetValue) {
return Arrays.asList(arr).contains(targetValue);
}
/**
* 获取模块名
*
*
* @param packageName 包名
* @return 模块名
*/
public static String getModuleName(String packageName)
{
public static String getModuleName(String packageName) {
int lastIndex = packageName.lastIndexOf(".");
int nameLength = packageName.length();
String moduleName = StringUtils.substring(packageName, lastIndex + 1, nameLength);
@@ -157,12 +136,11 @@ public class GenUtils
/**
* 获取业务名
*
*
* @param tableName 表名
* @return 业务名
*/
public static String getBusinessName(String tableName)
{
public static String getBusinessName(String tableName) {
int lastIndex = tableName.lastIndexOf("_");
int nameLength = tableName.length();
String businessName = StringUtils.substring(tableName, lastIndex + 1, nameLength);
@@ -171,16 +149,14 @@ public class GenUtils
/**
* 表名转换成Java类名
*
*
* @param tableName 表名称
* @return 类名
*/
public static String convertClassName(String tableName)
{
public static String convertClassName(String tableName) {
boolean autoRemovePre = GenConfig.getAutoRemovePre();
String tablePrefix = GenConfig.getTablePrefix();
if (autoRemovePre && StringUtils.isNotEmpty(tablePrefix))
{
if (autoRemovePre && StringUtils.isNotEmpty(tablePrefix)) {
String[] searchList = StringUtils.split(tablePrefix, ",");
tableName = replaceFirst(tableName, searchList);
}
@@ -189,18 +165,15 @@ public class GenUtils
/**
* 批量替换前缀
*
*
* @param replacementm 替换值
* @param searchList 替换列表
* @param searchList 替换列表
* @return
*/
public static String replaceFirst(String replacementm, String[] searchList)
{
public static String replaceFirst(String replacementm, String[] searchList) {
String text = replacementm;
for (String searchString : searchList)
{
if (replacementm.startsWith(searchString))
{
for (String searchString : searchList) {
if (replacementm.startsWith(searchString)) {
text = replacementm.replaceFirst(searchString, "");
break;
}
@@ -210,48 +183,39 @@ public class GenUtils
/**
* 关键字替换
*
*
* @param text 需要被替换的名字
* @return 替换后的名字
*/
public static String replaceText(String text)
{
public static String replaceText(String text) {
return RegExUtils.replaceAll(text, "(?:表|若依)", "");
}
/**
* 获取数据库类型字段
*
*
* @param columnType 列类型
* @return 截取后的列类型
*/
public static String getDbType(String columnType)
{
if (StringUtils.indexOf(columnType, "(") > 0)
{
public static String getDbType(String columnType) {
if (StringUtils.indexOf(columnType, "(") > 0) {
return StringUtils.substringBefore(columnType, "(");
}
else
{
} else {
return columnType;
}
}
/**
* 获取字段长度
*
*
* @param columnType 列类型
* @return 截取后的列类型
*/
public static Integer getColumnLength(String columnType)
{
if (StringUtils.indexOf(columnType, "(") > 0)
{
public static Integer getColumnLength(String columnType) {
if (StringUtils.indexOf(columnType, "(") > 0) {
String length = StringUtils.substringBetween(columnType, "(", ")");
return Integer.valueOf(length);
}
else
{
} else {
return 0;
}
}

View File

@@ -1,33 +1,29 @@
package com.ruoyi.gen.util;
import java.util.Properties;
import org.apache.velocity.app.Velocity;
import com.ruoyi.common.core.constant.Constants;
import org.apache.velocity.app.Velocity;
import java.util.Properties;
/**
* VelocityEngine工厂
*
*
* @author ruoyi
*/
public class VelocityInitializer
{
public class VelocityInitializer {
/**
* 初始化vm方法
*/
public static void initVelocity()
{
public static void initVelocity() {
Properties p = new Properties();
try
{
try {
// 加载classpath目录下的vm文件
p.setProperty("resource.loader.file.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
// 定义字符集
p.setProperty(Velocity.INPUT_ENCODING, Constants.UTF8);
// 初始化Velocity引擎指定配置Properties
Velocity.init(p);
}
catch (Exception e)
{
} catch (Exception e) {
throw new RuntimeException(e);
}
}

View File

@@ -1,31 +1,37 @@
package com.ruoyi.gen.util;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.velocity.VelocityContext;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.constant.GenConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.gen.domain.GenTable;
import com.ruoyi.gen.domain.GenTableColumn;
import org.apache.velocity.VelocityContext;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* 模板工具类
*
*
* @author ruoyi
*/
public class VelocityUtils
{
/** 项目空间路径 */
public class VelocityUtils {
/**
* 项目空间路径
*/
private static final String PROJECT_PATH = "main/java";
/** mybatis空间路径 */
/**
* mybatis空间路径
*/
private static final String MYBATIS_PATH = "main/resources/mapper";
/** 默认上级菜单,系统工具 */
/**
* 默认上级菜单,系统工具
*/
private static final String DEFAULT_PARENT_MENU_ID = "3";
/**
@@ -33,8 +39,7 @@ public class VelocityUtils
*
* @return 模板列表
*/
public static VelocityContext prepareContext(GenTable genTable)
{
public static VelocityContext prepareContext(GenTable genTable) {
String moduleName = genTable.getModuleName();
String businessName = genTable.getBusinessName();
String packageName = genTable.getPackageName();
@@ -61,27 +66,23 @@ public class VelocityUtils
velocityContext.put("table", genTable);
velocityContext.put("dicts", getDicts(genTable));
setMenuVelocityContext(velocityContext, genTable);
if (GenConstants.TPL_TREE.equals(tplCategory))
{
if (GenConstants.TPL_TREE.equals(tplCategory)) {
setTreeVelocityContext(velocityContext, genTable);
}
if (GenConstants.TPL_SUB.equals(tplCategory))
{
if (GenConstants.TPL_SUB.equals(tplCategory)) {
setSubVelocityContext(velocityContext, genTable);
}
return velocityContext;
}
public static void setMenuVelocityContext(VelocityContext context, GenTable genTable)
{
public static void setMenuVelocityContext(VelocityContext context, GenTable genTable) {
String options = genTable.getOptions();
JSONObject paramsObj = JSONObject.parseObject(options);
String parentMenuId = getParentMenuId(paramsObj);
context.put("parentMenuId", parentMenuId);
}
public static void setTreeVelocityContext(VelocityContext context, GenTable genTable)
{
public static void setTreeVelocityContext(VelocityContext context, GenTable genTable) {
String options = genTable.getOptions();
JSONObject paramsObj = JSONObject.parseObject(options);
String treeCode = getTreecode(paramsObj);
@@ -92,18 +93,15 @@ public class VelocityUtils
context.put("treeParentCode", treeParentCode);
context.put("treeName", treeName);
context.put("expandColumn", getExpandColumn(genTable));
if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE))
{
if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) {
context.put("tree_parent_code", paramsObj.getString(GenConstants.TREE_PARENT_CODE));
}
if (paramsObj.containsKey(GenConstants.TREE_NAME))
{
if (paramsObj.containsKey(GenConstants.TREE_NAME)) {
context.put("tree_name", paramsObj.getString(GenConstants.TREE_NAME));
}
}
public static void setSubVelocityContext(VelocityContext context, GenTable genTable)
{
public static void setSubVelocityContext(VelocityContext context, GenTable genTable) {
GenTable subTable = genTable.getSubTable();
String subTableName = genTable.getSubTableName();
String subTableFkName = genTable.getSubTableFkName();
@@ -125,8 +123,7 @@ public class VelocityUtils
*
* @return 模板列表
*/
public static List<String> getTemplateList(String tplCategory)
{
public static List<String> getTemplateList(String tplCategory) {
List<String> templates = new ArrayList<String>();
templates.add("vm/java/domain.java.vm");
templates.add("vm/java/mapper.java.vm");
@@ -136,16 +133,11 @@ public class VelocityUtils
templates.add("vm/xml/mapper.xml.vm");
templates.add("vm/sql/sql.vm");
templates.add("vm/js/api.js.vm");
if (GenConstants.TPL_CRUD.equals(tplCategory))
{
if (GenConstants.TPL_CRUD.equals(tplCategory)) {
templates.add("vm/vue/index.vue.vm");
}
else if (GenConstants.TPL_TREE.equals(tplCategory))
{
} else if (GenConstants.TPL_TREE.equals(tplCategory)) {
templates.add("vm/vue/index-tree.vue.vm");
}
else if (GenConstants.TPL_SUB.equals(tplCategory))
{
} else if (GenConstants.TPL_SUB.equals(tplCategory)) {
templates.add("vm/vue/index.vue.vm");
templates.add("vm/java/sub-domain.java.vm");
}
@@ -155,8 +147,7 @@ public class VelocityUtils
/**
* 获取文件名
*/
public static String getFileName(String template, GenTable genTable)
{
public static String getFileName(String template, GenTable genTable) {
// 文件名称
String fileName = "";
// 包路径
@@ -172,48 +163,28 @@ public class VelocityUtils
String mybatisPath = MYBATIS_PATH + "/" + moduleName;
String vuePath = "vue";
if (template.contains("domain.java.vm"))
{
if (template.contains("domain.java.vm")) {
fileName = StringUtils.format("{}/domain/{}.java", javaPath, className);
}
if (template.contains("sub-domain.java.vm") && StringUtils.equals(GenConstants.TPL_SUB, genTable.getTplCategory()))
{
if (template.contains("sub-domain.java.vm") && StringUtils.equals(GenConstants.TPL_SUB, genTable.getTplCategory())) {
fileName = StringUtils.format("{}/domain/{}.java", javaPath, genTable.getSubTable().getClassName());
}
else if (template.contains("mapper.java.vm"))
{
} else if (template.contains("mapper.java.vm")) {
fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className);
}
else if (template.contains("service.java.vm"))
{
} else if (template.contains("service.java.vm")) {
fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className);
}
else if (template.contains("serviceImpl.java.vm"))
{
} else if (template.contains("serviceImpl.java.vm")) {
fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, className);
}
else if (template.contains("controller.java.vm"))
{
} else if (template.contains("controller.java.vm")) {
fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, className);
}
else if (template.contains("mapper.xml.vm"))
{
} else if (template.contains("mapper.xml.vm")) {
fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, className);
}
else if (template.contains("sql.vm"))
{
} else if (template.contains("sql.vm")) {
fileName = businessName + "Menu.sql";
}
else if (template.contains("api.js.vm"))
{
} else if (template.contains("api.js.vm")) {
fileName = StringUtils.format("{}/api/{}/{}.js", vuePath, moduleName, businessName);
}
else if (template.contains("index.vue.vm"))
{
} else if (template.contains("index.vue.vm")) {
fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
}
else if (template.contains("index-tree.vue.vm"))
{
} else if (template.contains("index-tree.vue.vm")) {
fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
}
return fileName;
@@ -225,8 +196,7 @@ public class VelocityUtils
* @param packageName 包名称
* @return 包前缀名称
*/
public static String getPackagePrefix(String packageName)
{
public static String getPackagePrefix(String packageName) {
int lastIndex = packageName.lastIndexOf(".");
String basePackage = StringUtils.substring(packageName, 0, lastIndex);
return basePackage;
@@ -234,27 +204,21 @@ public class VelocityUtils
/**
* 根据列类型获取导入包
*
*
* @param genTable 业务表对象
* @return 返回需要导入的包列表
*/
public static HashSet<String> getImportList(GenTable genTable)
{
public static HashSet<String> getImportList(GenTable genTable) {
List<GenTableColumn> columns = genTable.getColumns();
GenTable subGenTable = genTable.getSubTable();
HashSet<String> importList = new HashSet<String>();
if (StringUtils.isNotNull(subGenTable))
{
if (StringUtils.isNotNull(subGenTable)) {
importList.add("java.util.List");
}
for (GenTableColumn column : columns)
{
if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType()))
{
for (GenTableColumn column : columns) {
if (!column.isSuperColumn() && GenConstants.TYPE_DATE.equals(column.getJavaType())) {
importList.add("java.util.Date");
}
else if (!column.isSuperColumn() && GenConstants.TYPE_BIGDECIMAL.equals(column.getJavaType()))
{
} else if (!column.isSuperColumn() && GenConstants.TYPE_BIGDECIMAL.equals(column.getJavaType())) {
importList.add("java.math.BigDecimal");
}
}
@@ -263,20 +227,17 @@ public class VelocityUtils
/**
* 根据列类型获取字典组
*
*
* @param genTable 业务表对象
* @return 返回字典组
*/
public static String getDicts(GenTable genTable)
{
public static String getDicts(GenTable genTable) {
List<GenTableColumn> columns = genTable.getColumns();
Set<String> dicts = new HashSet<String>();
for (GenTableColumn column : columns)
{
for (GenTableColumn column : columns) {
if (!column.isSuperColumn() && StringUtils.isNotEmpty(column.getDictType()) && StringUtils.equalsAny(
column.getHtmlType(),
new String[] { GenConstants.HTML_SELECT, GenConstants.HTML_RADIO, GenConstants.HTML_CHECKBOX }))
{
new String[]{GenConstants.HTML_SELECT, GenConstants.HTML_RADIO, GenConstants.HTML_CHECKBOX})) {
dicts.add("'" + column.getDictType() + "'");
}
}
@@ -286,12 +247,11 @@ public class VelocityUtils
/**
* 获取权限前缀
*
* @param moduleName 模块名称
* @param moduleName 模块名称
* @param businessName 业务名称
* @return 返回权限前缀
*/
public static String getPermissionPrefix(String moduleName, String businessName)
{
public static String getPermissionPrefix(String moduleName, String businessName) {
return StringUtils.format("{}:{}", moduleName, businessName);
}
@@ -301,11 +261,9 @@ public class VelocityUtils
* @param paramsObj 生成其他选项
* @return 上级菜单ID字段
*/
public static String getParentMenuId(JSONObject paramsObj)
{
public static String getParentMenuId(JSONObject paramsObj) {
if (StringUtils.isNotEmpty(paramsObj) && paramsObj.containsKey(GenConstants.PARENT_MENU_ID)
&& StringUtils.isNotEmpty(paramsObj.getString(GenConstants.PARENT_MENU_ID)))
{
&& StringUtils.isNotEmpty(paramsObj.getString(GenConstants.PARENT_MENU_ID))) {
return paramsObj.getString(GenConstants.PARENT_MENU_ID);
}
return DEFAULT_PARENT_MENU_ID;
@@ -317,10 +275,8 @@ public class VelocityUtils
* @param paramsObj 生成其他选项
* @return 树编码
*/
public static String getTreecode(JSONObject paramsObj)
{
if (paramsObj.containsKey(GenConstants.TREE_CODE))
{
public static String getTreecode(JSONObject paramsObj) {
if (paramsObj.containsKey(GenConstants.TREE_CODE)) {
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_CODE));
}
return StringUtils.EMPTY;
@@ -332,10 +288,8 @@ public class VelocityUtils
* @param paramsObj 生成其他选项
* @return 树父编码
*/
public static String getTreeParentCode(JSONObject paramsObj)
{
if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE))
{
public static String getTreeParentCode(JSONObject paramsObj) {
if (paramsObj.containsKey(GenConstants.TREE_PARENT_CODE)) {
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_PARENT_CODE));
}
return StringUtils.EMPTY;
@@ -347,10 +301,8 @@ public class VelocityUtils
* @param paramsObj 生成其他选项
* @return 树名称
*/
public static String getTreeName(JSONObject paramsObj)
{
if (paramsObj.containsKey(GenConstants.TREE_NAME))
{
public static String getTreeName(JSONObject paramsObj) {
if (paramsObj.containsKey(GenConstants.TREE_NAME)) {
return StringUtils.toCamelCase(paramsObj.getString(GenConstants.TREE_NAME));
}
return StringUtils.EMPTY;
@@ -362,20 +314,16 @@ public class VelocityUtils
* @param genTable 业务表对象
* @return 展开按钮列序号
*/
public static int getExpandColumn(GenTable genTable)
{
public static int getExpandColumn(GenTable genTable) {
String options = genTable.getOptions();
JSONObject paramsObj = JSONObject.parseObject(options);
String treeName = paramsObj.getString(GenConstants.TREE_NAME);
int num = 0;
for (GenTableColumn column : genTable.getColumns())
{
if (column.isList())
{
for (GenTableColumn column : genTable.getColumns()) {
if (column.isList()) {
num++;
String columnName = column.getColumnName();
if (columnName.equals(treeName))
{
if (columnName.equals(treeName)) {
break;
}
}