wip: 代码生成临时提交
This commit is contained in:
@@ -1,18 +1,30 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.template.Template;
|
||||
import cn.hutool.extra.template.TemplateConfig;
|
||||
import cn.hutool.extra.template.TemplateEngine;
|
||||
import cn.hutool.extra.template.TemplateUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.youlai.system.config.property.GeneratorProperties;
|
||||
import com.youlai.system.mapper.DatabaseMapper;
|
||||
import com.youlai.system.mapper.GenConfigMapper;
|
||||
import com.youlai.system.mapper.GenFieldConfigMapper;
|
||||
import com.youlai.system.model.entity.GenConfig;
|
||||
import com.youlai.system.model.entity.GenFieldConfig;
|
||||
import com.youlai.system.model.query.TablePageQuery;
|
||||
import com.youlai.system.model.vo.TableColumnVO;
|
||||
import com.youlai.system.model.vo.TableGeneratePreviewVO;
|
||||
import com.youlai.system.model.vo.GeneratorPreviewVO;
|
||||
import com.youlai.system.model.vo.TablePageVO;
|
||||
import com.youlai.system.service.GeneratorService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.logging.log4j.util.Strings;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
import cn.hutool.extra.template.TemplateConfig.ResourceMode;
|
||||
|
||||
@@ -31,6 +43,15 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
|
||||
private final DatabaseMapper databaseMapper;
|
||||
|
||||
private final GeneratorProperties generatorProperties;
|
||||
|
||||
private final GenConfigMapper genConfigMapper;
|
||||
private final GenFieldConfigMapper genFieldConfigMapper;
|
||||
|
||||
// 注入 spring.application.name
|
||||
@Value("${spring.application.name}")
|
||||
private String applicationName;
|
||||
|
||||
/**
|
||||
* 数据表分页列表
|
||||
*
|
||||
@@ -60,49 +81,133 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
* @return 预览数据
|
||||
*/
|
||||
@Override
|
||||
public List<TableGeneratePreviewVO> getTablePreviewData(String tableName) {
|
||||
public List<GeneratorPreviewVO> getTablePreviewData(String tableName) {
|
||||
|
||||
List<TableGeneratePreviewVO> list = new ArrayList<>();
|
||||
List<GeneratorPreviewVO> list = new ArrayList<>();
|
||||
|
||||
TemplateConfig templateConfig = new TemplateConfig("templates" , ResourceMode.CLASSPATH);
|
||||
TemplateEngine templateEngine = TemplateUtil.createEngine(templateConfig);
|
||||
GenConfig genConfig = genConfigMapper.selectOne(new LambdaQueryWrapper<GenConfig>()
|
||||
.eq(GenConfig::getTableName, tableName)
|
||||
);
|
||||
Assert.isTrue(genConfig != null, "未找到表生成配置");
|
||||
|
||||
List<GenFieldConfig> fieldConfigs = genFieldConfigMapper.selectList(new LambdaQueryWrapper<GenFieldConfig>()
|
||||
.eq(GenFieldConfig::getConfigId, genConfig.getId())
|
||||
);
|
||||
Assert.isTrue(CollectionUtil.isNotEmpty(fieldConfigs), "未找到字段生成配置");
|
||||
|
||||
// 遍历模板配置
|
||||
Map<String, GeneratorProperties.TemplateConfig> templateConfigs = generatorProperties.getTemplateConfigs();
|
||||
for (Map.Entry<String, GeneratorProperties.TemplateConfig> templateConfigEntry : templateConfigs.entrySet()) {
|
||||
GeneratorPreviewVO previewVO = new GeneratorPreviewVO();
|
||||
|
||||
GeneratorProperties.TemplateConfig templateConfig = templateConfigEntry.getValue();
|
||||
|
||||
/* 1. 生成文件名 UserController */
|
||||
// User Role Menu Dept
|
||||
String entityName = genConfig.getEntityName();
|
||||
// Controller Service Mapper Entity
|
||||
String templateName = templateConfigEntry.getKey();
|
||||
// .java .ts .vue
|
||||
String extension = templateConfig.getExtension();
|
||||
|
||||
// 文件名 UserController.java
|
||||
String fileName = getFileName(entityName, templateName, extension);
|
||||
previewVO.setFileName(fileName);
|
||||
|
||||
|
||||
Map<String, Object> bindingMap = new HashMap<>();
|
||||
bindingMap.put("tableName", "sys_user");
|
||||
bindingMap.put("author", "Ray");
|
||||
bindingMap.put("date", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm"));
|
||||
bindingMap.put("entityName", "User" );
|
||||
bindingMap.put("lowerFirstEntityName", "user");
|
||||
bindingMap.put("tableComment", "用户");
|
||||
/* 2. 生成文件路径 */
|
||||
// com.youlai.system
|
||||
String packageName = genConfig.getPackageName();
|
||||
// controller
|
||||
String subPackageName = templateConfig.getPackageName();
|
||||
// 文件路径 com.youlai.system.controller
|
||||
String filePath = getFilePath(templateName, packageName, subPackageName);
|
||||
previewVO.setPath(filePath);
|
||||
|
||||
// 包路径
|
||||
bindingMap.put("package", "com.youlai.system");
|
||||
/* 3. 生成文件内容 */
|
||||
|
||||
Template template = templateEngine.getTemplate("generator" + File.separator + "controller.java.vm");
|
||||
String content = template.render(bindingMap);
|
||||
TableGeneratePreviewVO controller = new TableGeneratePreviewVO();
|
||||
controller.setPath("youlai-boot/controller");
|
||||
controller.setContent(content);
|
||||
controller.setFileName("UserController.java");
|
||||
// 生成文件内容
|
||||
String content = getCodeContent(templateConfig, genConfig, fieldConfigs);
|
||||
previewVO.setContent(content);
|
||||
|
||||
list.add(controller);
|
||||
|
||||
TableGeneratePreviewVO vo = new TableGeneratePreviewVO();
|
||||
vo.setPath("youlai-boot/model/vo");
|
||||
vo.setContent(content);
|
||||
vo.setFileName("UserVO.java");
|
||||
|
||||
list.add(vo);
|
||||
|
||||
list.add(previewVO);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
private String generatePath(){
|
||||
|
||||
private String getFileName(String entityName, String templateName, String extension) {
|
||||
if (templateName.equals("Entity")) {
|
||||
return entityName + extension;
|
||||
}
|
||||
if (templateName.equals("MapperXml")) {
|
||||
return entityName + "Mapper" + extension;
|
||||
}
|
||||
if (templateName.equals("API")|| templateName.equals("VIEW")) {
|
||||
return StrUtil.toSymbolCase(entityName,'-') + extension;
|
||||
}
|
||||
return entityName + templateName + extension;
|
||||
}
|
||||
|
||||
private String getFilePath(String templateName, String packageName, String subPackageName) {
|
||||
String path;
|
||||
if (templateName.equals("MapperXml")) {
|
||||
path = (applicationName
|
||||
+ File.separator
|
||||
+ "src" + File.separator + "main" + File.separator + "resources"
|
||||
+ File.separator + subPackageName
|
||||
).replace(".", File.separator);
|
||||
} else if (templateName.equals("API") || templateName.equals("VIEW")) {
|
||||
path = ("vue3-element-admin"
|
||||
+ File.separator
|
||||
+ "src" + File.separator + subPackageName
|
||||
).replace(".", File.separator);
|
||||
} else {
|
||||
path = (applicationName
|
||||
+ File.separator
|
||||
+ "src" + File.separator + "main" + File.separator + "java"
|
||||
+ File.separator + packageName + File.separator + subPackageName
|
||||
).replace(".", File.separator);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码内容
|
||||
*
|
||||
* @param templateConfig 模板配置
|
||||
* @param genConfig 生成配置
|
||||
* @param fieldConfigs 字段配置
|
||||
* @return 代码内容
|
||||
*/
|
||||
private String getCodeContent(GeneratorProperties.TemplateConfig templateConfig, GenConfig genConfig, List<GenFieldConfig> fieldConfigs) {
|
||||
|
||||
Map<String, Object> bindMap = new HashMap<>();
|
||||
|
||||
String entityName = genConfig.getEntityName();
|
||||
|
||||
bindMap.put("package", genConfig.getPackageName());
|
||||
bindMap.put("subPackage", templateConfig.getPackageName());
|
||||
bindMap.put("date", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm"));
|
||||
bindMap.put("entityName", entityName);
|
||||
bindMap.put("tableName", genConfig.getTableName());
|
||||
bindMap.put("author", genConfig.getAuthor());
|
||||
bindMap.put("lowerFirstEntityName", StrUtil.lowerFirst(entityName));
|
||||
bindMap.put("tableComment", StrUtil.replace(genConfig.getTableComment(), "表", Strings.EMPTY));
|
||||
bindMap.put("fieldConfigs", fieldConfigs);
|
||||
|
||||
for (GenFieldConfig fieldConfig : fieldConfigs) {
|
||||
bindMap.put("hasLocalDateTime", "LocalDateTime".equals(fieldConfig.getFieldType()));
|
||||
bindMap.put("hasBigDecimal", "BigDecimal".equals(fieldConfig.getFieldType()));
|
||||
bindMap.put("hasRequiredField", Boolean.TRUE.equals(fieldConfig.getIsRequired()));
|
||||
}
|
||||
|
||||
TemplateEngine templateEngine = TemplateUtil.createEngine(new TemplateConfig("templates", ResourceMode.CLASSPATH));
|
||||
Template template = templateEngine.getTemplate(templateConfig.getTemplatePath());
|
||||
String content = template.render(bindMap);
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -157,7 +157,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
* 删除用户
|
||||
*
|
||||
* @param idsStr 用户ID,多个以英文逗号(,)分割
|
||||
* @return true|false
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean deleteUsers(String idsStr) {
|
||||
@@ -175,7 +175,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param password 用户密码
|
||||
* @return true|false
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean updatePassword(Long userId, String password) {
|
||||
|
||||
Reference in New Issue
Block a user