refactor: 代码生成器
This commit is contained in:
@@ -6,6 +6,14 @@ import org.apache.commons.configuration.PropertiesConfiguration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 工具类
|
||||
* </p>
|
||||
*
|
||||
* @author lyd
|
||||
* @since 2023-05-03
|
||||
*/
|
||||
public class ColUtil {
|
||||
private static final Logger log = LoggerFactory.getLogger(ColUtil.class);
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.util.Map;
|
||||
|
||||
import static org.nl.modules.common.utils.FileUtil.SYS_TEM_DIR;
|
||||
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description: 代码生成
|
||||
@@ -101,7 +100,7 @@ public class GenUtil {
|
||||
* 定义后端文件路径以及名称
|
||||
*/
|
||||
private static String getAdminFilePath(String templateName, CodeGenConfig genConfig, String className, String rootPath) {
|
||||
String projectPath = rootPath + File.separator;
|
||||
String projectPath = rootPath + File.separator + genConfig.getModule_name() + File.separator;
|
||||
String packagePath = projectPath + "src" + File.separator + "main" + File.separator + "java" + File.separator;
|
||||
if (!ObjectUtils.isEmpty(genConfig.getPack())) {
|
||||
packagePath += genConfig.getPack().replace(".", File.separator) + File.separator;
|
||||
@@ -323,7 +322,6 @@ public class GenUtil {
|
||||
|
||||
/**
|
||||
* 打包下载
|
||||
*
|
||||
* @param columns
|
||||
* @param genConfig
|
||||
* @return
|
||||
@@ -371,7 +369,6 @@ public class GenUtil {
|
||||
|
||||
/**
|
||||
* 生成文件
|
||||
*
|
||||
* @param file
|
||||
* @param template
|
||||
* @param map
|
||||
@@ -394,7 +391,6 @@ public class GenUtil {
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
*
|
||||
* @param columnInfos
|
||||
* @param genConfig
|
||||
* @throws IOException
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/*
|
||||
package org.nl.modules.common.utils;/*
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -13,7 +13,6 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.nl.modules.common.utils;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
@@ -96,7 +95,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
|
||||
File file = null;
|
||||
try {
|
||||
// 用uuid作为文件名,防止生成的临时文件重复
|
||||
//file = File.createTempFile(IdUtil.simpleUUID(), prefix);
|
||||
file = new File(SYS_TEM_DIR + IdUtil.simpleUUID() + prefix);
|
||||
// MultipartFile to File
|
||||
multipartFile.transferTo(file);
|
||||
@@ -210,7 +208,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
|
||||
BigExcelWriter writer = ExcelUtil.getBigWriter(file);
|
||||
// 一次性写出内容,使用默认样式,强制输出标题
|
||||
writer.write(list, true);
|
||||
SXSSFSheet sheet = (SXSSFSheet) writer.getSheet();
|
||||
SXSSFSheet sheet = (SXSSFSheet)writer.getSheet();
|
||||
//上面需要强转SXSSFSheet 不然没有trackAllColumnsForAutoSizing方法
|
||||
sheet.trackAllColumnsForAutoSizing();
|
||||
//列宽自适应
|
||||
@@ -249,7 +247,7 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
|
||||
// 1M
|
||||
int len = 1024 * 1024;
|
||||
if (size > (maxSize * len)) {
|
||||
throw new BadRequestException("文件超出规定大小");
|
||||
throw new BadRequestException("文件超出规定大小!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@ package org.nl.system.controller.generator;
|
||||
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.nl.system.service.generator.ICodeGenConfigService;
|
||||
import org.nl.system.service.generator.dao.CodeGenConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -20,7 +23,6 @@ import org.springframework.web.bind.annotation.*;
|
||||
*/
|
||||
@SaIgnore
|
||||
@RestController
|
||||
|
||||
@RequestMapping("api/genConfig")
|
||||
public class CodeGenConfigController {
|
||||
|
||||
@@ -29,14 +31,14 @@ public class CodeGenConfigController {
|
||||
|
||||
|
||||
@GetMapping(value = "/{tableName}")
|
||||
public ResponseEntity<Object> query(@PathVariable String tableName) {
|
||||
public ResponseEntity<Object> query(@PathVariable String tableName){
|
||||
return new ResponseEntity<>(genConfigService.findByTableName(tableName), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@PutMapping
|
||||
public ResponseEntity<Object> update(@Validated @RequestBody CodeGenConfig genConfig) {
|
||||
return new ResponseEntity<>(genConfigService.update(genConfig.getTable_name(), genConfig), HttpStatus.OK);
|
||||
public ResponseEntity<Object> update(@Validated @RequestBody CodeGenConfig genConfig){
|
||||
return new ResponseEntity<>(genConfigService.update(genConfig.getTable_name(), genConfig),HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,6 @@ import java.util.List;
|
||||
@SaIgnore
|
||||
@RestController
|
||||
@RequestMapping("/api/generator")
|
||||
|
||||
public class CodeGeneratorController {
|
||||
@Autowired
|
||||
private ICodeGeneratorService generatorService;
|
||||
@@ -40,26 +39,26 @@ public class CodeGeneratorController {
|
||||
|
||||
|
||||
@GetMapping(value = "/tables")
|
||||
public ResponseEntity<Object> queryTables(@RequestParam(defaultValue = "") String name, PageQuery pageable) {
|
||||
public ResponseEntity<Object> queryTables(@RequestParam(defaultValue = "") String name, PageQuery pageable){
|
||||
return new ResponseEntity<>(TableDataInfo.build(generatorService.getTables(name, pageable)), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@GetMapping(value = "/columns")
|
||||
public ResponseEntity<Object> queryColumns(@RequestParam String tableName) {
|
||||
public ResponseEntity<Object> queryColumns(@RequestParam String tableName){
|
||||
return new ResponseEntity<>(TableDataInfo.build(generatorService.getColumns(tableName)), HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@PutMapping
|
||||
public ResponseEntity<HttpStatus> save(@RequestBody List<CodeColumnConfig> columnInfos) {
|
||||
public ResponseEntity<HttpStatus> save(@RequestBody List<CodeColumnConfig> columnInfos){
|
||||
generatorService.updateBatchById(columnInfos);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping(value = "sync")
|
||||
public ResponseEntity<HttpStatus> sync(@RequestBody List<String> tables) {
|
||||
public ResponseEntity<HttpStatus> sync(@RequestBody List<String> tables){
|
||||
for (String table : tables) {
|
||||
generatorService.sync(generatorService.getColumns(table), generatorService.query(table));
|
||||
}
|
||||
@@ -68,24 +67,20 @@ public class CodeGeneratorController {
|
||||
|
||||
|
||||
@PostMapping(value = "/{tableName}/{type}")
|
||||
public ResponseEntity<Object> generator(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response) {
|
||||
if (!generatorEnabled && type == 0) {
|
||||
throw new BadRequestException("此环境不允许生成代码,请选择预览或者下载查看!");
|
||||
public ResponseEntity<Object> generator(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response){
|
||||
if(!generatorEnabled && type == 0){
|
||||
throw new BadRequestException("此环境不允许生成代码,请选择预览或者下载查看");
|
||||
}
|
||||
switch (type) {
|
||||
switch (type){
|
||||
// 生成代码
|
||||
case 0:
|
||||
generatorService.generator(genConfigService.findByTableName(tableName), generatorService.getColumns(tableName));
|
||||
case 0: generatorService.generator(genConfigService.findByTableName(tableName), generatorService.getColumns(tableName));
|
||||
break;
|
||||
// 预览
|
||||
case 1:
|
||||
return generatorService.preview(genConfigService.findByTableName(tableName), generatorService.getColumns(tableName));
|
||||
case 1: return generatorService.preview(genConfigService.findByTableName(tableName), generatorService.getColumns(tableName));
|
||||
// 打包
|
||||
case 2:
|
||||
generatorService.download(genConfigService.findByTableName(tableName), generatorService.getColumns(tableName), request, response);
|
||||
case 2: generatorService.download(genConfigService.findByTableName(tableName), generatorService.getColumns(tableName), request, response);
|
||||
break;
|
||||
default:
|
||||
throw new BadRequestException("没有这个选项");
|
||||
default: throw new BadRequestException("信息为空");
|
||||
}
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.nl.system.service.generator;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.nl.system.service.generator.dao.CodeGenConfig;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -15,7 +15,6 @@ public interface ICodeGenConfigService extends IService<CodeGenConfig> {
|
||||
|
||||
/**
|
||||
* 根据表名查找
|
||||
*
|
||||
* @param tableName
|
||||
* @return
|
||||
*/
|
||||
@@ -23,7 +22,6 @@ public interface ICodeGenConfigService extends IService<CodeGenConfig> {
|
||||
|
||||
/**
|
||||
* 根据表名更新
|
||||
*
|
||||
* @param tableName
|
||||
* @param genConfig
|
||||
* @return
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package org.nl.system.service.generator;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.nl.common.domain.query.PageQuery;
|
||||
import org.nl.system.service.generator.dao.CodeColumnConfig;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.nl.system.service.generator.dao.CodeGenConfig;
|
||||
import org.nl.system.service.generator.dto.TablesInfo;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
|
||||
@@ -25,16 +26,14 @@ public interface ICodeGeneratorService extends IService<CodeColumnConfig> {
|
||||
|
||||
/**
|
||||
* 获得所有的表格信息
|
||||
*
|
||||
* @param name
|
||||
* @param pageQuery
|
||||
* @return
|
||||
* @return IPage<TablesInfo>
|
||||
*/
|
||||
IPage<TablesInfo> getTables(String name, PageQuery pageQuery);
|
||||
|
||||
/**
|
||||
* 得到数据表的元数据
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return /
|
||||
*/
|
||||
@@ -42,7 +41,6 @@ public interface ICodeGeneratorService extends IService<CodeColumnConfig> {
|
||||
|
||||
/**
|
||||
* 根据表名查询表字段
|
||||
*
|
||||
* @param tableName
|
||||
* @return
|
||||
*/
|
||||
@@ -50,36 +48,32 @@ public interface ICodeGeneratorService extends IService<CodeColumnConfig> {
|
||||
|
||||
/**
|
||||
* 同步表数据
|
||||
*
|
||||
* @param columnInfos /
|
||||
* @param columnInfos /
|
||||
* @param columnInfoList /
|
||||
*/
|
||||
@Async
|
||||
void sync(IPage<CodeColumnConfig> columnInfos, List<CodeColumnConfig> columnInfoList);
|
||||
|
||||
/**
|
||||
* 预览代码
|
||||
*
|
||||
* 视图
|
||||
* @param byTableName
|
||||
* @param columns
|
||||
* @return
|
||||
* @return ResponseEntity<Object>
|
||||
*/
|
||||
ResponseEntity<Object> preview(CodeGenConfig byTableName, IPage<CodeColumnConfig> columns);
|
||||
|
||||
/**
|
||||
* 打包下载
|
||||
*
|
||||
* @param genConfig 配置信息
|
||||
* @param genConfig 配置信息
|
||||
* @param columnsPage 字段信息分页数据
|
||||
* @param request /
|
||||
* @param response /
|
||||
* @param request /
|
||||
* @param response /
|
||||
*/
|
||||
void download(CodeGenConfig genConfig, IPage<CodeColumnConfig> columnsPage, HttpServletRequest request, HttpServletResponse response);
|
||||
|
||||
/**
|
||||
* 代码生成
|
||||
*
|
||||
* @param genConfig 配置信息
|
||||
* @param genConfig 配置信息
|
||||
* @param columnsPage 字段信息分页数据
|
||||
*/
|
||||
void generator(CodeGenConfig genConfig, IPage<CodeColumnConfig> columnsPage);
|
||||
|
||||
@@ -5,12 +5,15 @@ import cn.hutool.core.util.ObjectUtil;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.nl.common.utils.GenUtil;
|
||||
import org.nl.system.service.generator.dto.ColumnInfo;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 列的数据信息表
|
||||
@@ -72,25 +75,24 @@ public class CodeColumnConfig implements Serializable {
|
||||
|
||||
/**
|
||||
* 创建默认的实体
|
||||
*
|
||||
* @param tableName
|
||||
* @param config
|
||||
* @return
|
||||
* @param tableName /
|
||||
* @param config /
|
||||
* @return CodeColumnConfig
|
||||
*/
|
||||
public static CodeColumnConfig createDefault(String tableName, ColumnInfo config) {
|
||||
CodeColumnConfig columnConfig = new CodeColumnConfig();
|
||||
columnConfig.setColumn_id(IdUtil.getSnowflake(1, 1).nextIdStr());
|
||||
columnConfig.setColumn_id(IdUtil.getSnowflake(1,1).nextIdStr());
|
||||
columnConfig.setTable_name(tableName);
|
||||
columnConfig.setColumn_name(config.getColumn_name());
|
||||
columnConfig.setColumn_type(config.getColumn_type());
|
||||
columnConfig.setKey_type(config.getKey_type());
|
||||
columnConfig.setExtra(config.getExtra());
|
||||
columnConfig.setNot_null((ObjectUtil.isNotEmpty(config.getKey_type())
|
||||
&& ObjectUtil.isNotEmpty(config.getExtra())
|
||||
&&ObjectUtil.isNotEmpty(config.getExtra())
|
||||
&& GenUtil.PK.equalsIgnoreCase(config.getKey_type())
|
||||
&& GenUtil.EXTRA.equalsIgnoreCase(config.getExtra()))
|
||||
? false : ObjectUtil.isNotEmpty(config.getNot_null()) ? config.getNot_null() : false);
|
||||
columnConfig.setRemark(ObjectUtil.isNotEmpty(config.getRemark()) ? config.getRemark() : null);
|
||||
&&GenUtil.EXTRA.equalsIgnoreCase(config.getExtra()))
|
||||
?false:ObjectUtil.isNotEmpty(config.getNot_null())?config.getNot_null():false);
|
||||
columnConfig.setRemark(ObjectUtil.isNotEmpty(config.getRemark())?config.getRemark():null);
|
||||
columnConfig.setList_show(true);
|
||||
columnConfig.setForm_show(true);
|
||||
return columnConfig;
|
||||
|
||||
@@ -2,12 +2,11 @@ package org.nl.system.service.generator.dao;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 代码生成配置表
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package org.nl.system.service.generator.dao.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.nl.system.service.generator.dao.CodeColumnConfig;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.nl.system.service.generator.dto.ColumnInfo;
|
||||
import org.nl.system.service.generator.dto.TablesInfo;
|
||||
|
||||
@@ -19,25 +19,24 @@ public interface CodeColumnConfigMapper extends BaseMapper<CodeColumnConfig> {
|
||||
|
||||
/**
|
||||
* 分页查找
|
||||
*
|
||||
* @param name 表名
|
||||
* @return 表信息
|
||||
* @param name /
|
||||
* @param pageSize /
|
||||
* @param offset /
|
||||
* @return List<TablesInfo>
|
||||
*/
|
||||
List<TablesInfo> getTables(String name, int pageSize, int offset);
|
||||
|
||||
/**
|
||||
* 分页查询的总数
|
||||
*
|
||||
* @param name 表名
|
||||
* @return 表信息
|
||||
* @param name
|
||||
* @return long
|
||||
*/
|
||||
long getTablesTotal(String name);
|
||||
|
||||
/**
|
||||
* 获取
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return 列数据
|
||||
* 获取字段名称
|
||||
* @param tableName
|
||||
* @return List<ColumnInfo>
|
||||
*/
|
||||
List<ColumnInfo> getTablesByTableName(String tableName);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.nl.system.service.generator.dto;
|
||||
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,7 +40,7 @@ public class CodeGenConfigServiceImpl extends ServiceImpl<CodeGenConfigMapper, C
|
||||
@Override
|
||||
public CodeGenConfig update(String tableName, CodeGenConfig genConfig) {
|
||||
// 如果 api 路径为空,则自动生成路径
|
||||
if (StrUtil.isEmpty(genConfig.getApi_path())) {
|
||||
if(StrUtil.isEmpty(genConfig.getApi_path())){
|
||||
String separator = File.separator;
|
||||
String[] paths;
|
||||
String symbol = "\\";
|
||||
@@ -63,7 +63,7 @@ public class CodeGenConfigServiceImpl extends ServiceImpl<CodeGenConfigMapper, C
|
||||
if (ObjectUtil.isNotEmpty(genConfig.getConfig_id())) {
|
||||
codeGenConfigMapper.updateById(genConfig);
|
||||
} else {
|
||||
genConfig.setConfig_id(IdUtil.getSnowflake(1, 1).nextIdStr());
|
||||
genConfig.setConfig_id(IdUtil.getSnowflake(1,1).nextIdStr());
|
||||
codeGenConfigMapper.insert(genConfig);
|
||||
}
|
||||
return genConfig;
|
||||
|
||||
@@ -9,8 +9,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.nl.common.domain.query.PageQuery;
|
||||
import org.nl.modules.common.exception.BadRequestException;
|
||||
import org.nl.common.utils.GenUtil;
|
||||
import org.nl.modules.common.exception.BadRequestException;
|
||||
import org.nl.modules.common.utils.FileUtil;
|
||||
import org.nl.system.service.generator.ICodeGeneratorService;
|
||||
import org.nl.system.service.generator.dao.CodeColumnConfig;
|
||||
@@ -142,7 +142,7 @@ public class CodeGeneratorServiceImpl extends ServiceImpl<CodeColumnConfigMapper
|
||||
ZipUtil.zip(file.getPath(), zipPath);
|
||||
FileUtil.downloadFile(request, response, new File(zipPath), true);
|
||||
} catch (IOException e) {
|
||||
throw new BadRequestException("打包失败");
|
||||
throw new BadRequestException("系统繁忙,稍后在试");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,7 +156,7 @@ public class CodeGeneratorServiceImpl extends ServiceImpl<CodeColumnConfigMapper
|
||||
GenUtil.generatorCode(columns, genConfig);
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new BadRequestException("生成失败,请手动处理已生成的文件");
|
||||
throw new BadRequestException("系统繁忙,稍后在试");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user