✨ 代码生成,属性占位符支持前端传参
This commit is contained in:
@@ -3,11 +3,11 @@ package com.hccake.ballcat.codegen.controller;
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.hccake.ballcat.codegen.model.dto.GeneratorOptionDTO;
|
||||
import com.hccake.ballcat.codegen.model.qo.TableInfoQO;
|
||||
import com.hccake.ballcat.codegen.model.vo.TableInfo;
|
||||
import com.hccake.ballcat.codegen.service.GeneratorService;
|
||||
import com.hccake.ballcat.codegen.service.TableInfoService;
|
||||
import com.hccake.ballcat.codegen.model.dto.GeneratorOptionDTO;
|
||||
import com.hccake.ballcat.common.core.result.R;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -16,8 +16,6 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 代码生成器
|
||||
@@ -52,15 +50,7 @@ public class GenerateController {
|
||||
*/
|
||||
@SneakyThrows
|
||||
@PostMapping("/generate")
|
||||
public void generatorCode(@RequestBody GeneratorOptionDTO generatorOptionDTO, HttpServletResponse response) {
|
||||
// TODO 待前端写好,暂时先写死
|
||||
Map<String, String> map = new HashMap<>(4);
|
||||
map.put("moduleName", "gen");
|
||||
map.put("packageName", "com.hccake.ballcat");
|
||||
map.put("author", "hccake");
|
||||
generatorOptionDTO.setGenProperties(map);
|
||||
generatorOptionDTO.setTemplateGroupId(1);
|
||||
|
||||
public void generateCode(@RequestBody GeneratorOptionDTO generatorOptionDTO, HttpServletResponse response) {
|
||||
byte[] data = generatorService.generatorCode(generatorOptionDTO);
|
||||
response.reset();
|
||||
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"ballcat.zip\"");
|
||||
|
||||
@@ -2,6 +2,8 @@ package com.hccake.ballcat.codegen.service;
|
||||
|
||||
import com.hccake.ballcat.codegen.model.dto.GeneratorOptionDTO;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author hccake
|
||||
* @date 2018/7/29
|
||||
@@ -13,6 +15,6 @@ public interface GeneratorService {
|
||||
* @param generatorOptionDTO 代码生成的一些参数
|
||||
* @return 已生成的代码数据
|
||||
*/
|
||||
byte[] generatorCode(GeneratorOptionDTO generatorOptionDTO);
|
||||
byte[] generatorCode(GeneratorOptionDTO generatorOptionDTO) throws IOException;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.hccake.ballcat.codegen.service.impl;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import com.hccake.ballcat.codegen.model.bo.TemplateFile;
|
||||
import com.hccake.ballcat.codegen.model.dto.GeneratorOptionDTO;
|
||||
@@ -11,15 +11,18 @@ import com.hccake.ballcat.codegen.service.TableInfoService;
|
||||
import com.hccake.ballcat.codegen.service.TemplateGroupService;
|
||||
import com.hccake.ballcat.codegen.util.GenUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@DS("#header.dsName")
|
||||
@@ -35,26 +38,26 @@ public class GeneratorServiceImpl implements GeneratorService {
|
||||
* @return 已生成的代码数据
|
||||
*/
|
||||
@Override
|
||||
public byte[] generatorCode(GeneratorOptionDTO generatorOptionDTO) {
|
||||
public byte[] generatorCode(GeneratorOptionDTO generatorOptionDTO) throws IOException {
|
||||
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
ZipOutputStream zip = new ZipOutputStream(outputStream)) {
|
||||
|
||||
// 根据tableName 查询最新的表单配置
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
ZipOutputStream zip = new ZipOutputStream(outputStream);
|
||||
// 根据tableName 查询最新的表单配置
|
||||
List<TemplateFile> templateFiles = templateGroupService
|
||||
.findTemplateFiles(generatorOptionDTO.getTemplateGroupId());
|
||||
Assert.notEmpty(templateFiles, "模板组中模板文件为空!");
|
||||
|
||||
List<TemplateFile> templateFiles = templateGroupService
|
||||
.findTemplateFiles(generatorOptionDTO.getTemplateGroupId());
|
||||
|
||||
for (String tableName : generatorOptionDTO.getTableNames()) {
|
||||
// 查询表信息
|
||||
TableInfo tableInfo = tableInfoService.queryTableInfo(tableName);
|
||||
// 查询列信息
|
||||
List<ColumnInfo> columnInfoList = tableInfoService.queryColumnInfo(tableName);
|
||||
// 生成代码
|
||||
GenUtils.generatorCode(generatorOptionDTO.getTablePrefix(), generatorOptionDTO.getGenProperties(),
|
||||
tableInfo, columnInfoList, zip, templateFiles);
|
||||
for (String tableName : generatorOptionDTO.getTableNames()) {
|
||||
// 查询表信息
|
||||
TableInfo tableInfo = tableInfoService.queryTableInfo(tableName);
|
||||
// 查询列信息
|
||||
List<ColumnInfo> columnInfoList = tableInfoService.queryColumnInfo(tableName);
|
||||
// 生成代码
|
||||
GenUtils.generatorCode(generatorOptionDTO.getTablePrefix(), generatorOptionDTO.getGenProperties(),
|
||||
tableInfo, columnInfoList, zip, templateFiles);
|
||||
}
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
IoUtil.close(zip);
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user