wip: 代码生成临时提交
This commit is contained in:
17
src/main/java/com/youlai/system/config/JacksonConfig.java
Normal file
17
src/main/java/com/youlai/system/config/JacksonConfig.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.youlai.system.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class JacksonConfig {
|
||||
|
||||
@Bean
|
||||
public ObjectMapper objectMapper() {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
|
||||
return objectMapper;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import com.youlai.system.model.vo.GeneratorPreviewVO;
|
||||
import com.youlai.system.model.vo.TablePageVO;
|
||||
import com.youlai.system.service.GeneratorService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -32,6 +33,21 @@ public class GeneratorController {
|
||||
return PageResult.success(result);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取代码生成配置")
|
||||
@GetMapping("/{tableName}/config")
|
||||
public Result<GenConfigForm> getGenConfig(
|
||||
@Parameter(description = "表名", example = "sys_user") @PathVariable String tableName) {
|
||||
GenConfigForm formData = generatorService.getGenConfig(tableName);
|
||||
return Result.success(formData);
|
||||
}
|
||||
|
||||
@Operation(summary = "保存代码生成配置")
|
||||
@PostMapping("/{tableName}/config")
|
||||
public Result saveGenConfig(@RequestBody GenConfigForm formData) {
|
||||
generatorService.saveGenConfig(formData);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "获取预览生成代码")
|
||||
@GetMapping("/{tableName}/preview")
|
||||
public Result<List<GeneratorPreviewVO>> getTablePreviewData(@PathVariable String tableName) {
|
||||
@@ -39,19 +55,4 @@ public class GeneratorController {
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "获取代码生成配置")
|
||||
@GetMapping("/{tableName}/config")
|
||||
public Result<GenConfigForm> getGenConfig(@PathVariable String tableName) {
|
||||
GenConfigForm formData = generatorService.getGenConfig(tableName);
|
||||
return Result.success(formData);
|
||||
}
|
||||
|
||||
@Operation(summary = "保存代码生成配置")
|
||||
@PostMapping("/{tableName}/config")
|
||||
public Result saveGenCodeConfig(@RequestBody GenConfigForm formData) {
|
||||
boolean result = generatorService.saveGenCodeConfig(formData);
|
||||
return Result.judge(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,18 +28,15 @@ public interface GenConfigConverter {
|
||||
|
||||
List<GenConfigForm.FieldConfig> toFieldConfigList(List<GenFieldConfig> fieldConfigs);
|
||||
|
||||
@Mapping(source = "configId", target = "configId")
|
||||
@Mapping(source = "columnName", target = "columnName")
|
||||
@Mapping(source = "columnType", target = "columnType")
|
||||
@Mapping(source = "fieldName", target = "fieldName")
|
||||
@Mapping(source = "fieldType", target = "fieldType")
|
||||
@Mapping(source = "comment", target = "comment")
|
||||
@Mapping(source = "formType", target = "formType")
|
||||
@Mapping(source = "queryType", target = "queryType")
|
||||
@Mapping(source = "isShowInList", target = "isShowInList")
|
||||
@Mapping(source = "isShowInForm", target = "isShowInForm")
|
||||
@Mapping(source = "isShowInQuery", target = "isShowInQuery")
|
||||
@Mapping(source = "isRequired", target = "isRequired")
|
||||
GenConfigForm.FieldConfig toFieldConfig(GenFieldConfig genFieldConfig);
|
||||
|
||||
|
||||
|
||||
@Mapping(source = "formData", target = "genConfig")
|
||||
@Mapping(source = "formData.fieldConfigs", target = "fieldConfigs")
|
||||
GenConfig toGenConfig(GenConfigForm formData);
|
||||
|
||||
@Mapping(source = "formData.fieldConfigs", target = "fieldConfigs")
|
||||
List<GenFieldConfig> toGenFieldConfigList(List<GenConfigForm.FieldConfig> fieldConfigs);
|
||||
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.youlai.system.enums;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.EnumValue;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
import com.youlai.system.common.base.IBaseEnum;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -48,11 +50,23 @@ public enum QueryTypeEnum implements IBaseEnum<Integer> {
|
||||
IS_NOT_NULL(14, "IS NOT NULL")
|
||||
;
|
||||
|
||||
// Mybatis-Plus 提供注解表示插入数据库时插入该值
|
||||
// 存储在数据库中的枚举属性值
|
||||
@EnumValue
|
||||
@JsonValue
|
||||
private final Integer value;
|
||||
|
||||
// @JsonValue // 表示对枚举序列化时返回此字段
|
||||
// 序列化成 JSON 时的属性值
|
||||
private final String label;
|
||||
|
||||
|
||||
@JsonCreator
|
||||
public static QueryTypeEnum fromValue(Integer value) {
|
||||
for (QueryTypeEnum type : QueryTypeEnum.values()) {
|
||||
if (type.getValue().equals(value)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("No enum constant with value " + value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,6 +11,9 @@ import java.util.List;
|
||||
@Data
|
||||
public class GenConfigForm {
|
||||
|
||||
@Schema(description = "主键")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "表名")
|
||||
private String tableName;
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.youlai.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.youlai.system.model.entity.GenConfig;
|
||||
import com.youlai.system.model.form.GenConfigForm;
|
||||
import com.youlai.system.model.query.TablePageQuery;
|
||||
import com.youlai.system.model.vo.GeneratorPreviewVO;
|
||||
import com.youlai.system.model.vo.TableColumnVO;
|
||||
import com.youlai.system.model.vo.TablePageVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 代码生成配置接口
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
public interface GenConfigService extends IService<GenConfig> {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.youlai.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.youlai.system.model.entity.GenConfig;
|
||||
import com.youlai.system.model.entity.GenFieldConfig;
|
||||
|
||||
/**
|
||||
* 代码生成配置接口
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
public interface GenFieldConfigService extends IService<GenFieldConfig> {
|
||||
|
||||
}
|
||||
@@ -3,21 +3,20 @@ package com.youlai.system.service;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.youlai.system.model.form.GenConfigForm;
|
||||
import com.youlai.system.model.query.TablePageQuery;
|
||||
import com.youlai.system.model.vo.TableColumnVO;
|
||||
import com.youlai.system.model.vo.GeneratorPreviewVO;
|
||||
import com.youlai.system.model.vo.TableColumnVO;
|
||||
import com.youlai.system.model.vo.TablePageVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 代码生成业务接口
|
||||
* 代码生成配置接口
|
||||
*
|
||||
* @author haoxr
|
||||
* @since 2.11.0
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
public interface GeneratorService {
|
||||
|
||||
|
||||
/**
|
||||
* 获取数据表分页列表
|
||||
*
|
||||
@@ -26,14 +25,6 @@ public interface GeneratorService {
|
||||
*/
|
||||
Page<TablePageVO> getTablePage(TablePageQuery queryParams);
|
||||
|
||||
/**
|
||||
* 获取数据表字段列表
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return
|
||||
*/
|
||||
List<TableColumnVO> getTableColumns(String tableName);
|
||||
|
||||
/**
|
||||
* 获取预览生成代码
|
||||
*
|
||||
@@ -56,5 +47,7 @@ public interface GeneratorService {
|
||||
* @param formData 表单数据
|
||||
* @return
|
||||
*/
|
||||
boolean saveGenCodeConfig(GenConfigForm formData);
|
||||
void saveGenConfig(GenConfigForm formData);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.mapper.GenConfigMapper;
|
||||
import com.youlai.system.model.entity.GenConfig;
|
||||
import com.youlai.system.service.GenConfigService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 数据库服务实现类
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GenConfigServiceImpl extends ServiceImpl<GenConfigMapper, GenConfig> implements GenConfigService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.youlai.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.youlai.system.mapper.GenFieldConfigMapper;
|
||||
import com.youlai.system.model.entity.GenFieldConfig;
|
||||
import com.youlai.system.service.GenFieldConfigService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 代码生成字段配置服务实现类
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.10.0
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GenFieldConfigServiceImpl extends ServiceImpl<GenFieldConfigMapper, GenFieldConfig> implements GenFieldConfigService {
|
||||
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.youlai.system.service.impl;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.template.Template;
|
||||
import cn.hutool.extra.template.TemplateConfig;
|
||||
@@ -11,22 +12,23 @@ 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.converter.GenConfigConverter;
|
||||
import com.youlai.system.exception.BusinessException;
|
||||
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.form.GenConfigForm;
|
||||
import com.youlai.system.model.query.TablePageQuery;
|
||||
import com.youlai.system.model.vo.TableColumnVO;
|
||||
import com.youlai.system.model.vo.GeneratorPreviewVO;
|
||||
import com.youlai.system.model.vo.TableColumnVO;
|
||||
import com.youlai.system.model.vo.TablePageVO;
|
||||
import com.youlai.system.service.GeneratorService;
|
||||
import com.youlai.system.service.GenConfigService;
|
||||
import com.youlai.system.service.GenFieldConfigService;
|
||||
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;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
@@ -35,23 +37,23 @@ import java.util.*;
|
||||
* 数据库服务实现类
|
||||
*
|
||||
* @author Ray
|
||||
* @since 2.11.0
|
||||
* @since 2.10.0
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GeneratorServiceImpl implements GeneratorService {
|
||||
|
||||
private final DatabaseMapper databaseMapper;
|
||||
|
||||
private final GeneratorProperties generatorProperties;
|
||||
|
||||
private final GenConfigMapper genConfigMapper;
|
||||
private final GenFieldConfigMapper genFieldConfigMapper;
|
||||
private final GenConfigService genConfigService;
|
||||
private final GenFieldConfigService genFieldConfigService;
|
||||
|
||||
// 注入 spring.application.name
|
||||
@Value("${spring.application.name}")
|
||||
private String applicationName;
|
||||
|
||||
private final GenConfigConverter genConfigConverter;
|
||||
|
||||
/**
|
||||
* 数据表分页列表
|
||||
*
|
||||
@@ -63,17 +65,6 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
return databaseMapper.getTablePage(page, queryParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据表字段列表
|
||||
*
|
||||
* @param tableName 表名
|
||||
* @return 字段列表
|
||||
*/
|
||||
@Override
|
||||
public List<TableColumnVO> getTableColumns(String tableName) {
|
||||
return databaseMapper.getTableColumns(tableName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取代码生成配置
|
||||
*
|
||||
@@ -83,27 +74,36 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
@Override
|
||||
public GenConfigForm getGenConfig(String tableName) {
|
||||
// 查询表生成配置
|
||||
GenConfig genConfig = genConfigMapper.selectOne(
|
||||
GenConfig genConfig = genConfigService.getOne(
|
||||
new LambdaQueryWrapper<>(GenConfig.class)
|
||||
.eq(GenConfig::getTableName, tableName)
|
||||
.last("LIMIT 1")
|
||||
);
|
||||
|
||||
// 查询字段生成配置
|
||||
List<GenFieldConfig> fieldConfigs = genFieldConfigMapper.selectList(
|
||||
List<GenFieldConfig> fieldConfigs = genFieldConfigService.list(
|
||||
new LambdaQueryWrapper<>(GenFieldConfig.class)
|
||||
.eq(GenFieldConfig::getConfigId, genConfig.getId())
|
||||
);
|
||||
|
||||
GenConfigForm genConfigForm = new GenConfigForm();
|
||||
|
||||
|
||||
return null;
|
||||
GenConfigForm configFormData = genConfigConverter.toGenConfigForm(genConfig, fieldConfigs);
|
||||
return configFormData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean saveGenCodeConfig(GenConfigForm formData) {
|
||||
return false;
|
||||
public void saveGenConfig(GenConfigForm formData) {
|
||||
GenConfig genConfig = genConfigConverter.toGenConfig(formData);
|
||||
genConfigService.saveOrUpdate(genConfig);
|
||||
|
||||
List<GenFieldConfig> genFieldConfigs = genConfigConverter.toGenFieldConfigList(formData.getFieldConfigs());
|
||||
|
||||
if (CollectionUtil.isEmpty(genFieldConfigs)) {
|
||||
throw new BusinessException("字段配置不能为空");
|
||||
}
|
||||
genFieldConfigs.forEach(genFieldConfig -> {
|
||||
genFieldConfig.setConfigId(genConfig.getId());
|
||||
});
|
||||
genFieldConfigService.saveOrUpdateBatch(genFieldConfigs);
|
||||
}
|
||||
|
||||
|
||||
@@ -118,12 +118,12 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
|
||||
List<GeneratorPreviewVO> list = new ArrayList<>();
|
||||
|
||||
GenConfig genConfig = genConfigMapper.selectOne(new LambdaQueryWrapper<GenConfig>()
|
||||
GenConfig genConfig = genConfigService.getOne(new LambdaQueryWrapper<GenConfig>()
|
||||
.eq(GenConfig::getTableName, tableName)
|
||||
);
|
||||
Assert.isTrue(genConfig != null, "未找到表生成配置");
|
||||
|
||||
List<GenFieldConfig> fieldConfigs = genFieldConfigMapper.selectList(new LambdaQueryWrapper<GenFieldConfig>()
|
||||
List<GenFieldConfig> fieldConfigs = genFieldConfigService.list(new LambdaQueryWrapper<GenFieldConfig>()
|
||||
.eq(GenFieldConfig::getConfigId, genConfig.getId())
|
||||
);
|
||||
Assert.isTrue(CollectionUtil.isNotEmpty(fieldConfigs), "未找到字段生成配置");
|
||||
@@ -233,14 +233,15 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
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()));
|
||||
bindMap.put("hasRequiredField", ObjectUtil.equals(fieldConfig.getIsRequired(), 1));
|
||||
}
|
||||
|
||||
TemplateEngine templateEngine = TemplateUtil.createEngine(new TemplateConfig("templates", ResourceMode.CLASSPATH));
|
||||
TemplateEngine templateEngine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
|
||||
Template template = templateEngine.getTemplate(templateConfig.getTemplatePath());
|
||||
String content = template.render(bindMap);
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user