feat: 初始化
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
FROM openjdk:8u212-jre
|
||||
MAINTAINER MrBird 852252810@qq.com
|
||||
|
||||
COPY ./target/febs-server-generator-2.2-RELEASE.jar /febs/febs-server-generator-2.2-RELEASE.jar
|
||||
ADD agent/ /agent
|
||||
|
||||
ENTRYPOINT ["java", "-javaagent:/agent/skywalking-agent.jar", "-Dskywalking.agent.service_name=febs-generator", "-Dskywalking.collector.backend_service=skywalkingIp:11800", "-jar", "/febs/febs-server-generator-2.2-RELEASE.jar"]
|
||||
@@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.yida.data</groupId>
|
||||
<artifactId>febs-server</artifactId>
|
||||
<version>2.2-RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>febs-server-generator</artifactId>
|
||||
<name>FEBS-Server-Generator</name>
|
||||
<description>FEBS-Server-Generator代码生成器</description>
|
||||
|
||||
<dependencies>
|
||||
<!--freemarker-->
|
||||
<dependency>
|
||||
<groupId>org.freemarker</groupId>
|
||||
<artifactId>freemarker</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<outputDirectory>../../febs-jar</outputDirectory>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/usr/bin/env bash
|
||||
mvn package
|
||||
|
||||
docker build -t febs-server-generator .
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package cc.mrbird.febs.server.generator;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
|
||||
import cc.mrbird.febs.common.security.starter.annotation.EnableFebsCloudResourceServer;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
@SpringBootApplication
|
||||
@EnableFebsCloudResourceServer
|
||||
@MapperScan("cc.mrbird.febs.server.generator.mapper")
|
||||
public class FebsServerGeneratorApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder(FebsServerGeneratorApplication.class)
|
||||
.web(WebApplicationType.SERVLET)
|
||||
.run(args);
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package cc.mrbird.febs.server.generator.controller;
|
||||
|
||||
import com.yida.data.common.core.entity.FebsResponse;
|
||||
import com.yida.data.common.core.entity.system.GeneratorConfig;
|
||||
import com.yida.data.common.core.exception.FebsException;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import cc.mrbird.febs.server.generator.service.GeneratorConfigService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("config")
|
||||
public class GeneratorConfigController {
|
||||
|
||||
private final GeneratorConfigService generatorConfigService;
|
||||
|
||||
@GetMapping
|
||||
@PreAuthorize("hasAuthority('gen:config')")
|
||||
public FebsResponse getGeneratorConfig() {
|
||||
return new FebsResponse().data(generatorConfigService.findGeneratorConfig());
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@PreAuthorize("hasAuthority('gen:config:update')")
|
||||
public void updateGeneratorConfig(@Valid GeneratorConfig generatorConfig) throws FebsException {
|
||||
if (StringUtils.isBlank(generatorConfig.getId())) {
|
||||
throw new FebsException("配置id不能为空");
|
||||
}
|
||||
this.generatorConfigService.updateGeneratorConfig(generatorConfig);
|
||||
}
|
||||
}
|
||||
+111
@@ -0,0 +1,111 @@
|
||||
package cc.mrbird.febs.server.generator.controller;
|
||||
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
||||
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
||||
import com.yida.data.common.core.entity.FebsResponse;
|
||||
import com.yida.data.common.core.entity.QueryRequest;
|
||||
import com.yida.data.common.core.entity.constant.GeneratorConstant;
|
||||
import com.yida.data.common.core.entity.system.Column;
|
||||
import com.yida.data.common.core.entity.system.GeneratorConfig;
|
||||
import com.yida.data.common.core.entity.system.Table;
|
||||
import com.yida.data.common.core.exception.FebsException;
|
||||
import com.yida.data.common.core.utils.FebsUtil;
|
||||
import com.yida.data.common.core.utils.FileUtil;
|
||||
|
||||
import org.apache.commons.lang3.RegExUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.util.FileSystemUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
import cc.mrbird.febs.server.generator.helper.GeneratorHelper;
|
||||
import cc.mrbird.febs.server.generator.service.GeneratorConfigService;
|
||||
import cc.mrbird.febs.server.generator.service.GeneratorService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
@Slf4j
|
||||
@Validated
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
public class GeneratorController {
|
||||
|
||||
private static final String SUFFIX = "_code.zip";
|
||||
|
||||
private final GeneratorService generatorService;
|
||||
private final GeneratorConfigService generatorConfigService;
|
||||
private final GeneratorHelper generatorHelper;
|
||||
private final DynamicDataSourceProperties properties;
|
||||
|
||||
@GetMapping("datasources")
|
||||
@PreAuthorize("hasAuthority('gen:generate')")
|
||||
public FebsResponse datasources() {
|
||||
Map<String, DataSourceProperty> datasources = properties.getDatasource();
|
||||
List<String> datasourcesName = new ArrayList<>();
|
||||
datasources.forEach((k, v) -> {
|
||||
String datasourceName = StringUtils.substringBefore(StringUtils.substringAfterLast(v.getUrl(), "/"), "?");
|
||||
datasourcesName.add(datasourceName);
|
||||
});
|
||||
return new FebsResponse().data(datasourcesName);
|
||||
}
|
||||
|
||||
@GetMapping("tables")
|
||||
@PreAuthorize("hasAuthority('gen:generate')")
|
||||
public FebsResponse tablesInfo(String tableName, String datasource, QueryRequest request) {
|
||||
Map<String, Object> dataTable = FebsUtil.getDataTable(generatorService.getTables(tableName, request, GeneratorConstant.DATABASE_TYPE, datasource));
|
||||
return new FebsResponse().data(dataTable);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
//@PreAuthorize("hasAuthority('gen:generate:gen')")
|
||||
public void generate(String[] name,
|
||||
@NotBlank(message = "{required}") String datasource,
|
||||
String remark, HttpServletResponse response) throws Exception {
|
||||
GeneratorConfig generatorConfig = generatorConfigService.findGeneratorConfig();
|
||||
if (generatorConfig == null) {
|
||||
throw new FebsException("代码生成配置为空");
|
||||
}
|
||||
|
||||
for (String name1 : name) {
|
||||
String className = name1;
|
||||
if (GeneratorConfig.TRIM_YES.equals(generatorConfig.getIsTrim())) {
|
||||
className = RegExUtils.replaceFirst(name1, generatorConfig.getTrimValue(), StringUtils.EMPTY);
|
||||
}
|
||||
|
||||
generatorConfig.setTableName(name1);
|
||||
generatorConfig.setClassName(FebsUtil.underscoreToCamel(className));
|
||||
Table table = generatorService.getTable(GeneratorConstant.DATABASE_TYPE, datasource, name1);
|
||||
generatorConfig.setTableComment(Objects.nonNull(table) ? table.getRemark() : remark);
|
||||
// 生成代码到临时目录
|
||||
List<Column> columns = generatorService.getColumns(GeneratorConstant.DATABASE_TYPE, datasource, name1);
|
||||
generatorHelper.generateEntityFile(columns, generatorConfig);
|
||||
generatorHelper.generateMapperFile(columns, generatorConfig);
|
||||
generatorHelper.generateMapperXmlFile(columns, generatorConfig);
|
||||
generatorHelper.generateServiceFile(columns, generatorConfig);
|
||||
generatorHelper.generateServiceImplFile(columns, generatorConfig);
|
||||
generatorHelper.generateControllerFile(columns, generatorConfig);
|
||||
}
|
||||
// 打包
|
||||
String zipFile = System.currentTimeMillis() + SUFFIX;
|
||||
FileUtil.compress(GeneratorConstant.TEMP_PATH + "src", zipFile);
|
||||
// 下载
|
||||
FileUtil.download(zipFile, "generate" + SUFFIX, true, response);
|
||||
// 删除临时目录
|
||||
FileSystemUtils.deleteRecursively(new File(GeneratorConstant.TEMP_PATH));
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package cc.mrbird.febs.server.generator.entity;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
public class FieldType {
|
||||
|
||||
public static final String DATE = "date";
|
||||
public static final String DATETIME = "datetime";
|
||||
public static final String TIMESTAMP = "timestamp";
|
||||
public static final String DECIMAL = "decimal";
|
||||
public static final String NUMERIC = "numeric";
|
||||
}
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
package cc.mrbird.febs.server.generator.helper;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.yida.data.common.core.annotation.Helper;
|
||||
import com.yida.data.common.core.entity.constant.FebsConstant;
|
||||
import com.yida.data.common.core.entity.constant.GeneratorConstant;
|
||||
import com.yida.data.common.core.entity.system.Column;
|
||||
import com.yida.data.common.core.entity.system.GeneratorConfig;
|
||||
import com.yida.data.common.core.utils.FebsUtil;
|
||||
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateExceptionHandler;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import cc.mrbird.febs.server.generator.entity.FieldType;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 代码生成器工具类
|
||||
*
|
||||
* @author MrBird
|
||||
*/
|
||||
@Slf4j
|
||||
@Helper
|
||||
public class GeneratorHelper {
|
||||
|
||||
private static String getFilePath(GeneratorConfig configure, String packagePath, String suffix, boolean serviceInterface) {
|
||||
String filePath = GeneratorConstant.TEMP_PATH + configure.getJavaPath() +
|
||||
packageConvertPath(configure.getBasePackage() + "." + packagePath);
|
||||
filePath += configure.getClassName() + suffix;
|
||||
return filePath;
|
||||
}
|
||||
|
||||
private static String packageConvertPath(String packageName) {
|
||||
return String.format("/%s/", packageName.contains(".") ? packageName.replaceAll("\\.", "/") : packageName);
|
||||
}
|
||||
|
||||
public void generateEntityFile(List<Column> columns, GeneratorConfig configure) throws Exception {
|
||||
String suffix = GeneratorConstant.JAVA_FILE_SUFFIX;
|
||||
String path = getFilePath(configure, configure.getEntityPackage(), suffix, false);
|
||||
String templateName = GeneratorConstant.ENTITY_TEMPLATE;
|
||||
File entityFile = new File(path);
|
||||
JSONObject data = toJsonObject(configure);
|
||||
data.put("hasDate", false);
|
||||
data.put("hasBigDecimal", false);
|
||||
columns.forEach(c -> {
|
||||
c.setField(FebsUtil.underscoreToCamel(StringUtils.lowerCase(c.getName())));
|
||||
if (StringUtils.containsAny(c.getType(), FieldType.DATE, FieldType.DATETIME, FieldType.TIMESTAMP)) {
|
||||
data.put("hasDate", true);
|
||||
}
|
||||
if (StringUtils.containsAny(c.getType(), FieldType.DECIMAL, FieldType.NUMERIC)) {
|
||||
data.put("hasBigDecimal", true);
|
||||
}
|
||||
});
|
||||
data.put("columns", columns);
|
||||
this.generateFileByTemplate(templateName, entityFile, data);
|
||||
}
|
||||
|
||||
public void generateMapperFile(List<Column> columns, GeneratorConfig configure) throws Exception {
|
||||
String suffix = GeneratorConstant.MAPPER_FILE_SUFFIX;
|
||||
String path = getFilePath(configure, configure.getMapperPackage(), suffix, false);
|
||||
String templateName = GeneratorConstant.MAPPER_TEMPLATE;
|
||||
File mapperFile = new File(path);
|
||||
generateFileByTemplate(templateName, mapperFile, toJsonObject(configure));
|
||||
}
|
||||
|
||||
public void generateServiceFile(List<Column> columns, GeneratorConfig configure) throws Exception {
|
||||
String suffix = GeneratorConstant.SERVICE_FILE_SUFFIX;
|
||||
String path = getFilePath(configure, configure.getServicePackage(), suffix, true);
|
||||
String templateName = GeneratorConstant.SERVICE_TEMPLATE;
|
||||
File serviceFile = new File(path);
|
||||
generateFileByTemplate(templateName, serviceFile, toJsonObject(configure));
|
||||
}
|
||||
|
||||
public void generateServiceImplFile(List<Column> columns, GeneratorConfig configure) throws Exception {
|
||||
String suffix = GeneratorConstant.SERVICEIMPL_FILE_SUFFIX;
|
||||
String path = getFilePath(configure, configure.getServiceImplPackage(), suffix, false);
|
||||
String templateName = GeneratorConstant.SERVICEIMPL_TEMPLATE;
|
||||
File serviceImplFile = new File(path);
|
||||
generateFileByTemplate(templateName, serviceImplFile, toJsonObject(configure));
|
||||
}
|
||||
|
||||
public void generateControllerFile(List<Column> columns, GeneratorConfig configure) throws Exception {
|
||||
String suffix = GeneratorConstant.CONTROLLER_FILE_SUFFIX;
|
||||
String path = getFilePath(configure, configure.getControllerPackage(), suffix, false);
|
||||
String templateName = GeneratorConstant.CONTROLLER_TEMPLATE;
|
||||
File controllerFile = new File(path);
|
||||
generateFileByTemplate(templateName, controllerFile, toJsonObject(configure));
|
||||
}
|
||||
|
||||
public void generateMapperXmlFile(List<Column> columns, GeneratorConfig configure) throws Exception {
|
||||
String suffix = GeneratorConstant.MAPPERXML_FILE_SUFFIX;
|
||||
String path = getFilePath(configure, configure.getMapperXmlPackage(), suffix, false);
|
||||
String templateName = GeneratorConstant.MAPPERXML_TEMPLATE;
|
||||
File mapperXmlFile = new File(path);
|
||||
JSONObject data = toJsonObject(configure);
|
||||
columns.forEach(c -> c.setField(FebsUtil.underscoreToCamel(StringUtils.lowerCase(c.getName()))));
|
||||
data.put("columns", columns);
|
||||
generateFileByTemplate(templateName, mapperXmlFile, data);
|
||||
}
|
||||
|
||||
@SuppressWarnings("all")
|
||||
private void generateFileByTemplate(String templateName, File file, Object data) throws Exception {
|
||||
Template template = getTemplate(templateName);
|
||||
Files.createParentDirs(file);
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(file);
|
||||
try (Writer out = new BufferedWriter(new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8), 10240)) {
|
||||
template.process(data, out);
|
||||
} catch (Exception e) {
|
||||
String message = "代码生成异常";
|
||||
log.error(message, e);
|
||||
throw new Exception(message);
|
||||
}
|
||||
}
|
||||
|
||||
private JSONObject toJsonObject(Object o) {
|
||||
return JSONObject.parseObject(JSONObject.toJSON(o).toString());
|
||||
}
|
||||
|
||||
private Template getTemplate(String templateName) throws Exception {
|
||||
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
|
||||
String templatePath = GeneratorHelper.class.getResource("/generator/templates/").getPath();
|
||||
File file = new File(templatePath);
|
||||
if (!file.exists()) {
|
||||
templatePath = System.getProperties().getProperty(FebsConstant.JAVA_TEMP_DIR);
|
||||
file = new File(templatePath + File.separator + templateName);
|
||||
FileUtils.copyInputStreamToFile(Objects.requireNonNull(GeneratorHelper.class.getClassLoader().getResourceAsStream("classpath:generator/templates/" + templateName)), file);
|
||||
}
|
||||
configuration.setDirectoryForTemplateLoading(new File(templatePath));
|
||||
configuration.setDefaultEncoding(FebsConstant.UTF8);
|
||||
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
|
||||
return configuration.getTemplate(templateName);
|
||||
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package cc.mrbird.febs.server.generator.mapper;
|
||||
|
||||
import com.yida.data.common.core.entity.system.GeneratorConfig;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
public interface GeneratorConfigMapper extends BaseMapper<GeneratorConfig> {
|
||||
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package cc.mrbird.febs.server.generator.mapper;
|
||||
|
||||
|
||||
import com.yida.data.common.core.entity.system.Column;
|
||||
import com.yida.data.common.core.entity.system.Table;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
public interface GeneratorMapper {
|
||||
|
||||
/**
|
||||
* 获取数据列表
|
||||
*
|
||||
* @param databaseType databaseType
|
||||
* @return 数据库列表
|
||||
*/
|
||||
List<String> getDatabases(@Param("databaseType") String databaseType);
|
||||
|
||||
/**
|
||||
* 获取数据表
|
||||
*
|
||||
* @param page page
|
||||
* @param tableName tableName
|
||||
* @param databaseType databaseType
|
||||
* @param schemaName schemaName
|
||||
* @param <T> Type
|
||||
* @return 数据表分页数据
|
||||
*/
|
||||
<T> IPage<Table> getTables(Page<T> page, @Param("tableName") String tableName, @Param("databaseType") String databaseType, @Param("schemaName") String schemaName);
|
||||
|
||||
/**
|
||||
* 获取数据表列信息
|
||||
*
|
||||
* @param databaseType databaseType
|
||||
* @param schemaName schemaName
|
||||
* @param tableName tableName
|
||||
* @return 数据表列信息
|
||||
*/
|
||||
List<Column> getColumns(@Param("databaseType") String databaseType, @Param("schemaName") String schemaName, @Param("tableName") String tableName);
|
||||
|
||||
/**
|
||||
* 获取单个数据表
|
||||
*
|
||||
* @param databaseType databaseType
|
||||
* @param schemaName schemaName
|
||||
* @param tableName tableName
|
||||
* @return com.yida.data.common.core.entity.system.Table
|
||||
* @author ZYJ
|
||||
* @date 2023/5/19 11:12
|
||||
*/
|
||||
Table getTable(@Param("databaseType") String databaseType,
|
||||
@Param("schemaName") String schemaName,
|
||||
@Param("tableName") String tableName);
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package cc.mrbird.febs.server.generator.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.yida.data.common.core.entity.system.GeneratorConfig;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
public interface GeneratorConfigService extends IService<GeneratorConfig> {
|
||||
|
||||
/**
|
||||
* 查询
|
||||
*
|
||||
* @return GeneratorConfig
|
||||
*/
|
||||
GeneratorConfig findGeneratorConfig();
|
||||
|
||||
/**
|
||||
* 修改
|
||||
*
|
||||
* @param generatorConfig generatorConfig
|
||||
*/
|
||||
void updateGeneratorConfig(GeneratorConfig generatorConfig);
|
||||
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package cc.mrbird.febs.server.generator.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.yida.data.common.core.entity.QueryRequest;
|
||||
import com.yida.data.common.core.entity.system.Column;
|
||||
import com.yida.data.common.core.entity.system.Table;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
public interface GeneratorService {
|
||||
|
||||
/**
|
||||
* 获取数据库列表
|
||||
*
|
||||
* @param databaseType databaseType
|
||||
* @return 数据库列表
|
||||
*/
|
||||
List<String> getDatabases(String databaseType);
|
||||
|
||||
/**
|
||||
* 获取数据表
|
||||
*
|
||||
* @param tableName tableName
|
||||
* @param request request
|
||||
* @param databaseType databaseType
|
||||
* @param schemaName schemaName
|
||||
* @return 数据表分页数据
|
||||
*/
|
||||
IPage<Table> getTables(String tableName, QueryRequest request, String databaseType, String schemaName);
|
||||
|
||||
/**
|
||||
* 获取数据表列信息
|
||||
*
|
||||
* @param databaseType databaseType
|
||||
* @param schemaName schemaName
|
||||
* @param tableName tableName
|
||||
* @return 数据表列信息
|
||||
*/
|
||||
List<Column> getColumns(String databaseType, String schemaName, String tableName);
|
||||
|
||||
/**
|
||||
* 获取单个数据表
|
||||
*
|
||||
* @param databaseType databaseType
|
||||
* @param schemaName schemaName
|
||||
* @param tableName tableName
|
||||
* @return com.yida.data.common.core.entity.system.Table
|
||||
* @author ZYJ
|
||||
* @date 2023/5/19 11:18
|
||||
*/
|
||||
Table getTable(String databaseType, String schemaName, String tableName);
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package cc.mrbird.febs.server.generator.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.yida.data.common.core.entity.system.GeneratorConfig;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import cc.mrbird.febs.server.generator.mapper.GeneratorConfigMapper;
|
||||
import cc.mrbird.febs.server.generator.service.GeneratorConfigService;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
@Service
|
||||
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
|
||||
public class GeneratorConfigServiceImpl extends ServiceImpl<GeneratorConfigMapper, GeneratorConfig> implements GeneratorConfigService {
|
||||
|
||||
@Override
|
||||
public GeneratorConfig findGeneratorConfig() {
|
||||
List<GeneratorConfig> generatorConfigs = this.baseMapper.selectList(null);
|
||||
return CollectionUtils.isNotEmpty(generatorConfigs) ? generatorConfigs.get(0) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateGeneratorConfig(GeneratorConfig generatorConfig) {
|
||||
this.saveOrUpdate(generatorConfig);
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package cc.mrbird.febs.server.generator.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.yida.data.common.core.entity.QueryRequest;
|
||||
import com.yida.data.common.core.entity.constant.FebsConstant;
|
||||
import com.yida.data.common.core.entity.system.Column;
|
||||
import com.yida.data.common.core.entity.system.Table;
|
||||
import com.yida.data.common.core.utils.SortUtil;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import cc.mrbird.febs.server.generator.mapper.GeneratorMapper;
|
||||
import cc.mrbird.febs.server.generator.service.GeneratorService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author MrBird
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class GeneratorServiceImpl implements GeneratorService {
|
||||
|
||||
private final GeneratorMapper generatorMapper;
|
||||
|
||||
@Override
|
||||
public List<String> getDatabases(String databaseType) {
|
||||
return generatorMapper.getDatabases(databaseType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IPage<Table> getTables(String tableName, QueryRequest request, String databaseType, String schemaName) {
|
||||
Page<Table> page = new Page<>(request.getPageNum(), request.getPageSize());
|
||||
SortUtil.handlePageSort(request, page, "createTime", FebsConstant.ORDER_ASC, false);
|
||||
return generatorMapper.getTables(page, tableName, databaseType, schemaName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Column> getColumns(String databaseType, String schemaName, String tableName) {
|
||||
return generatorMapper.getColumns(databaseType, schemaName, tableName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Table getTable(String databaseType, String schemaName, String tableName) {
|
||||
return generatorMapper.getTable(databaseType, schemaName, tableName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
required=\u4E0D\u80FD\u4E3A\u7A7A
|
||||
range=\u6709\u6548\u957f\u5ea6{min}\u5230{max}\u4e2a\u5b57\u7b26
|
||||
email=\u90ae\u7bb1\u683c\u5f0f\u4e0d\u5408\u6cd5
|
||||
mobile=\u624b\u673a\u53f7\u4e0d\u5408\u6cd5
|
||||
noMoreThan=\u957f\u5ea6\u4e0d\u80fd\u8d85\u8fc7{max}\u4e2a\u5b57\u7b26
|
||||
invalid=\u503c\u4e0d\u5408\u6cd5
|
||||
@@ -0,0 +1,9 @@
|
||||
████████ ██████ ████████ ████████ ███████ ██ ██
|
||||
░░░░░░██ ░█░░░░██ ░░░░░░██ ░██░░░░░ ░██░░░░██ ░██ ░██
|
||||
██ ░█ ░██ ██ ░██ ░██ ░██░██ ░██
|
||||
██ ░██████ ██ █████░███████ ░██ ░██░██ ░██
|
||||
██ ░█░░░░ ██ ██ ░░░░░ ░██░░░░ ░██ ░██░██ ░██
|
||||
██ ░█ ░██ ██ ░██ ░██ ██ ░██ ░██
|
||||
████████░███████ ████████ ░████████░███████ ░░███████
|
||||
░░░░░░░░ ░░░░░░░ ░░░░░░░░ ░░░░░░░░ ░░░░░░░ ░░░░░░░
|
||||
${spring.application.name}
|
||||
@@ -0,0 +1,27 @@
|
||||
spring:
|
||||
application:
|
||||
name: FEBS-Server-Generator
|
||||
cloud:
|
||||
nacos:
|
||||
config:
|
||||
server-addr: ${nacos.url}
|
||||
group: DEFAULT_GROUP
|
||||
prefix: febs-server-generator
|
||||
file-extension: yaml
|
||||
discovery:
|
||||
server-addr: ${nacos.url}
|
||||
|
||||
logging:
|
||||
level:
|
||||
org:
|
||||
springframework:
|
||||
boot:
|
||||
actuate:
|
||||
endpoint:
|
||||
EndpointId: error
|
||||
com:
|
||||
alibaba:
|
||||
cloud:
|
||||
nacos:
|
||||
client:
|
||||
NacosPropertySourceBuilder: error
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package ${basePackage}.${controllerPackage};
|
||||
|
||||
import ${basePackage}.${entityPackage}.${className};
|
||||
import ${basePackage}.${servicePackage}.I${className}Service;
|
||||
import com.yida.data.common.core.entity.FebsResponse;
|
||||
import com.yida.data.common.core.entity.QueryRequest;
|
||||
import com.yida.data.common.core.exception.FebsException;
|
||||
import com.yida.data.common.core.utils.FebsUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* ${tableComment} Controller
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
*/
|
||||
@Slf4j
|
||||
@Validated
|
||||
@RestController
|
||||
@RequestMapping("${className?uncap_first}")
|
||||
@RequiredArgsConstructor
|
||||
public class ${className}Controller {
|
||||
|
||||
private final ${className}Service ${className?uncap_first}Service;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package ${basePackage}.${entityPackage};
|
||||
|
||||
<#if hasDate = true>
|
||||
import java.util.Date;
|
||||
</#if>
|
||||
<#if hasBigDecimal = true>
|
||||
import java.math.BigDecimal;
|
||||
</#if>
|
||||
import lombok.Data;
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
|
||||
/**
|
||||
* ${tableComment} Entity
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
*/
|
||||
@Data
|
||||
@TableName("${tableName}")
|
||||
public class ${className} {
|
||||
|
||||
<#if columns??>
|
||||
<#list columns as column>
|
||||
/**
|
||||
* ${column.remark}
|
||||
*/
|
||||
@ApiModelProperty(value = "${column.remark}")
|
||||
<#if column.isKey = true>
|
||||
@TableId(value = "${column.name}", type = IdType.AUTO)
|
||||
<#else>
|
||||
<#if (column.name='create_date' || column.name='create_id') || column.name='create_name'>
|
||||
@TableField(value ="${column.name}", fill = FieldFill.INSERT)
|
||||
<#elseif (column.name='update_date' || column.name='update_id') || column.name='update_name'>
|
||||
@TableField(value ="${column.name}", fill = FieldFill.UPDATE)
|
||||
<#else>
|
||||
@TableField("${column.name}")
|
||||
</#if>
|
||||
</#if>
|
||||
<#if column.name='del_flag'>
|
||||
@TableLogic(value = "0", delval = "1")
|
||||
</#if>
|
||||
<#if (column.type = 'varchar' || column.type = 'text' || column.type = 'uniqueidentifier'
|
||||
|| column.type = 'varchar2' || column.type = 'nvarchar' || column.type = 'VARCHAR2'
|
||||
|| column.type = 'VARCHAR'|| column.type = 'CLOB' || column.type = 'char')>
|
||||
private String ${column.field?uncap_first};
|
||||
|
||||
</#if>
|
||||
<#if column.type = 'timestamp' || column.type = 'datetime'||column.type = 'TIMESTAMP' || column.type = 'DATETIME'>
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime ${column.field?uncap_first};
|
||||
|
||||
</#if>
|
||||
<#if column.type = 'date' || column.type = 'DATE'>
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd")
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
|
||||
private LocalDate ${column.field?uncap_first};
|
||||
|
||||
</#if>
|
||||
<#if column.type = 'TIME' || column.type = 'time'>
|
||||
@DateTimeFormat(pattern = "HH:mm:ss")
|
||||
@JsonFormat(timezone = "GMT+8", pattern = "HH:mm:ss")
|
||||
private LocalTime ${column.field?uncap_first};
|
||||
|
||||
</#if>
|
||||
<#if column.type = 'int' || column.type = 'smallint'>
|
||||
private Integer ${column.field?uncap_first};
|
||||
|
||||
</#if>
|
||||
<#if column.type = 'double'>
|
||||
private Double ${column.field?uncap_first};
|
||||
|
||||
</#if>
|
||||
<#if column.type = 'bigint'>
|
||||
private Long ${column.field?uncap_first};
|
||||
|
||||
</#if>
|
||||
<#if column.type = 'tinyint'>
|
||||
private Integer ${column.field?uncap_first};
|
||||
|
||||
</#if>
|
||||
<#if column.type = 'decimal' || column.type = 'numeric'>
|
||||
private BigDecimal ${column.field?uncap_first};
|
||||
</#if>
|
||||
</#list>
|
||||
</#if>
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package ${basePackage}.${mapperPackage};
|
||||
|
||||
import ${basePackage}.${entityPackage}.${className};
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* ${tableComment} Mapper
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
*/
|
||||
public interface ${className}Mapper extends BaseMapper<${className}> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="${basePackage}.${mapperPackage}.${className}Mapper">
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,18 @@
|
||||
package ${basePackage}.${servicePackage};
|
||||
|
||||
import ${basePackage}.${entityPackage}.${className};
|
||||
|
||||
import com.yida.data.common.core.entity.QueryRequest;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ${tableComment} Service接口
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
*/
|
||||
public interface ${className}Service extends IService<${className}> {
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package ${basePackage}.${serviceImplPackage};
|
||||
|
||||
import ${basePackage}.${entityPackage}.${className};
|
||||
import ${basePackage}.${mapperPackage}.${className}Mapper;
|
||||
import ${basePackage}.${servicePackage}.${className}Service;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.yida.data.common.core.entity.QueryRequest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* ${tableComment} Service实现
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
|
||||
public class ${className}ServiceImpl extends ServiceImpl
|
||||
<${className}Mapper, ${className}> implements ${className}Service {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cc.mrbird.febs.server.generator.mapper.GeneratorMapper">
|
||||
<!--查询可用数据库-->
|
||||
<select id="getDatabases" resultType="java.lang.String">
|
||||
SELECT DISTINCT TABLE_SCHEMA
|
||||
FROM information_schema.TABLES
|
||||
</select>
|
||||
|
||||
<!--查询数据库下面的表-->
|
||||
<select id="getTables" resultType="com.yida.data.common.core.entity.system.Table" parameterType="string">
|
||||
SELECT
|
||||
CREATE_TIME createTime,
|
||||
UPDATE_TIME updateTime,
|
||||
TABLE_ROWS dataRows,
|
||||
TABLE_NAME name,
|
||||
TABLE_COMMENT remark
|
||||
FROM
|
||||
information_schema.TABLES
|
||||
WHERE
|
||||
TABLE_SCHEMA = #{schemaName}
|
||||
<if test="tableName != null and tableName != ''">
|
||||
AND TABLE_NAME = #{tableName}
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<!--查询数据库表下面的列属性-->
|
||||
<select id="getColumns" resultType="com.yida.data.common.core.entity.system.Column">
|
||||
SELECT
|
||||
COLUMN_NAME name,
|
||||
CASE
|
||||
COLUMN_key
|
||||
WHEN 'PRI' THEN
|
||||
1 ELSE 0
|
||||
END isKey,
|
||||
DATA_TYPE type,
|
||||
COLUMN_COMMENT remark
|
||||
FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = #{schemaName} AND TABLE_NAME = #{tableName}
|
||||
</select>
|
||||
|
||||
<!-- 获取单个数据表 -->
|
||||
<select id="getTable" resultType="com.yida.data.common.core.entity.system.Table">
|
||||
SELECT
|
||||
CREATE_TIME createTime,
|
||||
UPDATE_TIME updateTime,
|
||||
TABLE_ROWS dataRows,
|
||||
TABLE_NAME name,
|
||||
TABLE_COMMENT remark
|
||||
FROM
|
||||
information_schema.TABLES
|
||||
WHERE
|
||||
TABLE_SCHEMA = #{schemaName}
|
||||
<if test="tableName != null and tableName != ''">
|
||||
AND TABLE_NAME = #{tableName}
|
||||
</if>
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user