feat: add system framework
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.schisandra</groupId>
|
||||
<artifactId>schisandra-cloud-storage-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>easy-gen-code-spring-boot-starter</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>easy-gen-code-spring-boot-starter</name>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity-engine-core</artifactId>
|
||||
<version>2.2</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-to-slf4j</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-all</artifactId>
|
||||
<version>5.1.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.16</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.76</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.dataformat</groupId>
|
||||
<artifactId>jackson-dataformat-yaml</artifactId>
|
||||
<version>2.12.3</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-slf4j-impl</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-to-slf4j</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>5.3.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.22</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>rdc-releases</id>
|
||||
<name>dw-repo-releases</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2012812-release-SiITng/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>rdc-snapshots</id>
|
||||
<name>dw-repo-snapshots</name>
|
||||
<url>https://packages.aliyun.com/maven/repository/2012812-snapshot-LHGmjL/</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
</project>
|
@@ -0,0 +1,26 @@
|
||||
package com.schisandra.core.anno;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Documented
|
||||
@Target({ElementType.TYPE, ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface Table {
|
||||
|
||||
String value() default "";
|
||||
|
||||
String keyType() default "";
|
||||
|
||||
String col() default "";
|
||||
|
||||
String comment() default "";
|
||||
|
||||
String myBatisType() default "";
|
||||
|
||||
String jdbcType() default "";
|
||||
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
package com.schisandra.core.config;
|
||||
|
||||
import com.schisandra.core.entity.JdbcConfig;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 代码生成器配置
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
@Data
|
||||
public class GenConfig {
|
||||
|
||||
/**
|
||||
* 从自定义的类中获取数据
|
||||
*/
|
||||
private String handler;
|
||||
|
||||
/**
|
||||
* 从classPath下的json文件中获取数据
|
||||
*/
|
||||
private String json;
|
||||
|
||||
/**
|
||||
* 使用jdbc从表结构中获取数据
|
||||
*/
|
||||
private JdbcConfig jdbc;
|
||||
|
||||
/**
|
||||
* 模板与生成文件的映射配置文件
|
||||
*/
|
||||
private String mapperInfos;
|
||||
|
||||
/**
|
||||
* 针对某一个模板将数据写入该模板上下文
|
||||
*/
|
||||
private String filePutHandler;
|
||||
|
||||
/**
|
||||
* 自定义写入全局的参数
|
||||
*/
|
||||
private Map<String, Object> params;
|
||||
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package com.schisandra.core.config;
|
||||
|
||||
import com.schisandra.core.entity.Mapper;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 模板与生成文件的映射关系
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
@Data
|
||||
public class MapperConfig {
|
||||
|
||||
/**
|
||||
* 文件生成到该项目的哪个模块
|
||||
*/
|
||||
private String module;
|
||||
|
||||
/**
|
||||
* 映射关系
|
||||
*/
|
||||
private List<Mapper> mappers;
|
||||
|
||||
}
|
@@ -0,0 +1,241 @@
|
||||
package com.schisandra.core.core;
|
||||
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.schisandra.core.config.GenConfig;
|
||||
import com.schisandra.core.config.MapperConfig;
|
||||
import com.schisandra.core.core.impl.JdbcPutContextHandler;
|
||||
import com.schisandra.core.core.sdk.FilePutContextHandler;
|
||||
import com.schisandra.core.core.sdk.PostCurFiledContextAware;
|
||||
import com.schisandra.core.entity.Context;
|
||||
import com.schisandra.core.entity.MapperInfo;
|
||||
import com.schisandra.core.utils.*;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CodeGenerationCode {
|
||||
|
||||
public static void doGenCode() {
|
||||
doGenCode(null);
|
||||
}
|
||||
|
||||
public static void doGenCode(PostCurFiledContextAware aware) {
|
||||
|
||||
try {
|
||||
// 01 获取入口配置文件
|
||||
GenConfig genConfig = YamlUtils.loadYaml("genCode/gen.yml", GenConfig.class);
|
||||
|
||||
// 02 获取全局的配置上下文
|
||||
assert genConfig != null;
|
||||
VelocityContext context = getGlobalContext(genConfig);
|
||||
Map<String, Object> contexts = new HashMap<>();
|
||||
for (String key : context.getKeys()) {
|
||||
Object value = context.get(key);
|
||||
if (value instanceof String && FunctionUtils.isFunction(value)) {
|
||||
value = FunctionUtils.doFunction(value);
|
||||
context.put(key, value);
|
||||
}
|
||||
contexts.put(key, context.get(key));
|
||||
}
|
||||
System.out.println("==========> getGlobalContext:" + System.lineSeparator() + JSONObject.toJSONString(contexts));
|
||||
System.out.println();
|
||||
|
||||
// 03 获取 模板与生成文件的映射关系
|
||||
List<MapperInfo> infos = getMapperInfos(genConfig, context, aware);
|
||||
|
||||
// 04 通过映射关系生成代码文件
|
||||
genCode(infos, genConfig.getParams().getOrDefault("templateBasePath", "").toString());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void genCode(List<MapperInfo> infos, String templateBasePath) {
|
||||
|
||||
AtomicInteger count = new AtomicInteger(1);
|
||||
infos.forEach(info -> {
|
||||
CodeGeneratorUtils codeGenerator = new CodeGeneratorUtils(templateBasePath);
|
||||
codeGenerator.generateCode(
|
||||
info.getContext(),
|
||||
info.getTemplatePath(),
|
||||
info.getOutPutPath(),
|
||||
count);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
private static List<MapperInfo> getMapperInfos(GenConfig genConfig, VelocityContext context, PostCurFiledContextAware aware) {
|
||||
|
||||
MapperConfig mapperConfig = YamlUtils.loadYaml(genConfig.getMapperInfos(), MapperConfig.class);
|
||||
assert mapperConfig != null;
|
||||
// 允许在生成文件前对上下文中的数据进行修改 处理一些导包之类的不好控制业务
|
||||
if (Objects.nonNull(aware)) {
|
||||
aware.doAware(genConfig, mapperConfig, context);
|
||||
Map<String, Object> contexts = new HashMap<>();
|
||||
for (String key : context.getKeys()) {
|
||||
Object value = context.get(key);
|
||||
contexts.put(key, value);
|
||||
}
|
||||
System.out.println("==========> modify file context :" + System.lineSeparator() + JSONObject.toJSONString(contexts));
|
||||
}
|
||||
|
||||
AtomicInteger count = new AtomicInteger(1);
|
||||
return mapperConfig.getMappers().stream().filter(Objects::nonNull).map(item -> {
|
||||
MapperInfo info = new MapperInfo();
|
||||
info.setTemplatePath(getResPath(item.getTemplate()));
|
||||
String cName = item.getName();
|
||||
|
||||
if (cName.contains("$")) {
|
||||
cName = replace$key(context, cName);
|
||||
}
|
||||
String pkg = item.getPackageName();
|
||||
if (StrUtil.isNotEmpty(pkg) && pkg.contains("$")) {
|
||||
pkg = replace$key(context, pkg);
|
||||
}
|
||||
String filePath = item.getFilePath();
|
||||
if (StrUtil.isNotEmpty(filePath) && filePath.contains("$")) {
|
||||
filePath = replace$key(context, filePath);
|
||||
}
|
||||
String outPutPath = getTargetPath(mapperConfig.getModule(), pkg, filePath);
|
||||
File file = new File(outPutPath);
|
||||
if (!file.exists()) {
|
||||
boolean mkdirs = file.mkdirs();
|
||||
System.out.println("==========> mkdir " + mkdirs + " " + file.getAbsolutePath());
|
||||
System.out.println();
|
||||
}
|
||||
outPutPath += ("/" + cName + "." + item.getExt());
|
||||
VelocityContext cloneCtx = (VelocityContext) context.clone();
|
||||
info.setOutPutPath(outPutPath);
|
||||
if (StrUtil.isNotBlank(genConfig.getFilePutHandler())) {
|
||||
try {
|
||||
FilePutContextHandler handler = (FilePutContextHandler) Class.forName(genConfig.getFilePutHandler()).newInstance();
|
||||
handler.put(cloneCtx, item.getFileId());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
cloneCtx.put("className", cName);
|
||||
cloneCtx.put("packageName", pkg);
|
||||
System.out.println("==========> " + count.get() + " MapperInfo : " + System.lineSeparator() + info);
|
||||
System.out.println();
|
||||
Map<String, Object> contexts = new HashMap<>();
|
||||
for (String key : cloneCtx.getKeys()) {
|
||||
contexts.put(key, cloneCtx.get(key));
|
||||
}
|
||||
System.out.println("==========> MapperInfo context : " + System.lineSeparator() + JSONObject.toJSONString(contexts));
|
||||
System.out.println();
|
||||
System.out.println("=============================================================================================================================================");
|
||||
System.out.println();
|
||||
info.setContext(cloneCtx);
|
||||
return info;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static String replace$key(VelocityContext velocityContext, String conextkey) {
|
||||
|
||||
String key = conextkey.substring(conextkey.indexOf("${") + 2, conextkey.indexOf("}"));
|
||||
Object value = velocityContext.get(key);
|
||||
conextkey = conextkey.replace("${" + key + "}", value.toString());
|
||||
if (conextkey.contains("$")) {
|
||||
conextkey = replace$key(velocityContext, conextkey);
|
||||
}
|
||||
return conextkey;
|
||||
|
||||
}
|
||||
|
||||
private static VelocityContext getGlobalContext(GenConfig genConfig) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
|
||||
|
||||
Context context = ConfigUtils.getContext();
|
||||
boolean putData = false;
|
||||
|
||||
// 01 处理优先级第一的处理器
|
||||
if (StrUtil.isNotBlank(genConfig.getHandler())) {
|
||||
JdbcPutContextHandler jdbcPutContextHandler = (JdbcPutContextHandler) Class.forName(genConfig.getHandler()).newInstance();
|
||||
putData = jdbcPutContextHandler.put();
|
||||
}
|
||||
|
||||
// 02 处理优先级第二的json导入
|
||||
if (!putData && StrUtil.isNotBlank(genConfig.getJson())) {
|
||||
List<String> lines = FileUtil.readLines(getRealResPath(genConfig.getJson()), "UTF-8");
|
||||
StringBuilder json = new StringBuilder();
|
||||
for (String line : lines) {
|
||||
json.append(line);
|
||||
}
|
||||
context = ContextUtils.buildContext(json.toString());
|
||||
ConfigUtils.reSetContext(context);
|
||||
putData = true;
|
||||
}
|
||||
|
||||
// 03 处理优先级第三的mysql导入
|
||||
if (!putData) {
|
||||
JdbcPutContextHandler jdbcPutContextHandler = new JdbcPutContextHandler();
|
||||
jdbcPutContextHandler.put();
|
||||
}
|
||||
|
||||
// 04 添加配置中的全局参数
|
||||
Map<String, Object> params = genConfig.getParams();
|
||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
context.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return context.get();
|
||||
|
||||
}
|
||||
|
||||
private static String getTargetPath(String module, String packageName, String filePath) {
|
||||
|
||||
if (StrUtil.isEmpty(module)) {
|
||||
module = "/";
|
||||
}
|
||||
if (SystemUtils.isMacOs()) {
|
||||
if (StrUtil.isEmpty(filePath)) {
|
||||
return getCurPath().substring(0, getCurPath().length() - 2) + module + "src/main/java/" + packageName.replaceAll("\\.", "/");
|
||||
} else {
|
||||
return getCurPath().substring(0, getCurPath().length() - 2) + module + filePath;
|
||||
}
|
||||
} else {
|
||||
if (StrUtil.isEmpty(filePath)) {
|
||||
return getCurPath() + module + "src/main/java/" + packageName.replaceAll("\\.", "\\");
|
||||
} else {
|
||||
return getCurPath() + module + filePath;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static URL getRealResPath(String res) {
|
||||
ClassLoader classLoader = CodeGenerationCode.class.getClassLoader();
|
||||
return classLoader.getResource(res);
|
||||
}
|
||||
|
||||
private static String getResPath(String res) {
|
||||
|
||||
if (SystemUtils.isMacOs()) {
|
||||
ClassLoader classLoader = CodeGenerationCode.class.getClassLoader();
|
||||
return classLoader.getResource(res).getPath()
|
||||
.replaceAll(getCurPath().substring(0, getCurPath().length() - 1), "");
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static String getCurPath() {
|
||||
if (SystemUtils.isMacOs()) {
|
||||
return new File("./").getAbsolutePath();
|
||||
} else {
|
||||
return System.getProperty("user.dir");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,134 @@
|
||||
package com.schisandra.core.core;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import com.schisandra.core.entity.JdbcConfig;
|
||||
import com.schisandra.core.entity.TableInfo;
|
||||
import com.schisandra.core.utils.Lists;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Driver;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
/**
|
||||
* 简易mysql处理器 查询表备注及表字段
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
public class MysqlDataHandler {
|
||||
|
||||
private String tableName;
|
||||
private String dbUrl;
|
||||
private String dbUser;
|
||||
private String dbPw;
|
||||
private String dbName;
|
||||
private String driver;
|
||||
|
||||
public String getTableComment() {
|
||||
JdbcConfig config = new JdbcConfig(
|
||||
dbUrl + dbName + "?useUnicode=true",
|
||||
dbUser,
|
||||
dbPw,
|
||||
dbName,
|
||||
driver,
|
||||
tableName
|
||||
);
|
||||
JdbcTemplate jdbcTemplate = initJdbcTemplate(config);
|
||||
String sql = "SELECT table_comment tableComment FROM information_schema.TABLES WHERE table_schema = ? and table_name = ? ";
|
||||
List<Object> params = Lists.newArrayList();
|
||||
params.add(config.getDbName());
|
||||
params.add(tableName);
|
||||
BeanPropertyRowMapper<TableInfo> rowMapper = new BeanPropertyRowMapper<>(TableInfo.class);
|
||||
if (CollectionUtil.isEmpty(params)) {
|
||||
return jdbcTemplate.query(sql, rowMapper).get(0).getTableComment();
|
||||
}
|
||||
List<TableInfo> query = jdbcTemplate.query(sql, rowMapper, params.toArray());
|
||||
if (CollectionUtil.isEmpty(query)) {
|
||||
throw new RuntimeException("表不存在:" + tableName);
|
||||
}
|
||||
return query.get(0).getTableComment();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 使用表名 查询表结构信息
|
||||
*/
|
||||
public List<TableInfo> searchByDb() {
|
||||
|
||||
JdbcConfig config = new JdbcConfig(
|
||||
dbUrl + dbName + "?useUnicode=true",
|
||||
dbUser,
|
||||
dbPw,
|
||||
dbName,
|
||||
driver,
|
||||
tableName
|
||||
);
|
||||
JdbcTemplate jdbcTemplate = initJdbcTemplate(config);
|
||||
String sql = "select column_name as name, column_comment as comment,DATA_TYPE as dataType,COLUMN_KEY as keyType " +
|
||||
"from information_schema.columns where table_schema = ? and table_name = ? ORDER BY ORDINAL_POSITION";
|
||||
List<Object> params = Lists.newArrayList();
|
||||
|
||||
params.add(config.getDbName());
|
||||
params.add(tableName);
|
||||
BeanPropertyRowMapper<TableInfo> rowMapper = new BeanPropertyRowMapper<>(TableInfo.class);
|
||||
if (CollectionUtil.isEmpty(params)) {
|
||||
return jdbcTemplate.query(sql, rowMapper);
|
||||
}
|
||||
List<TableInfo> list = jdbcTemplate.query(sql, rowMapper, params.toArray());
|
||||
return list.stream().peek(item -> {
|
||||
item.setCol(item.getName());
|
||||
item.setName(underlineToHump(item.getName()));
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化一个jdbc
|
||||
*/
|
||||
private static JdbcTemplate initJdbcTemplate(JdbcConfig config) {
|
||||
DataSource dataSource;
|
||||
try {
|
||||
dataSource = new SimpleDriverDataSource(
|
||||
BeanUtils.instantiateClass((Class<Driver>) Class.forName(config.getDriver())),
|
||||
config.getUrl(),
|
||||
config.getUsername(),
|
||||
config.getPassword());
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
private static Pattern UNDERLINE_PATTERN = Pattern.compile("_([a-z])");
|
||||
|
||||
public static String underlineToHump(String str) {
|
||||
//正则匹配下划线及后一个字符,删除下划线并将匹配的字符转成大写
|
||||
Matcher matcher = UNDERLINE_PATTERN.matcher(str);
|
||||
StringBuffer sb = new StringBuffer(str);
|
||||
if (matcher.find()) {
|
||||
sb = new StringBuffer();
|
||||
//将当前匹配的子串替换成指定字符串,并且将替换后的子串及之前到上次匹配的子串之后的字符串添加到StringBuffer对象中
|
||||
//正则之前的字符和被替换的字符
|
||||
matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
|
||||
//把之后的字符串也添加到StringBuffer对象中
|
||||
matcher.appendTail(sb);
|
||||
} else {
|
||||
//去除除字母之外的前面带的下划线
|
||||
return sb.toString().replaceAll("_", "");
|
||||
}
|
||||
return underlineToHump(sb.toString());
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
package com.schisandra.core.core.impl;
|
||||
|
||||
import com.schisandra.core.config.GenConfig;
|
||||
import com.schisandra.core.config.MapperConfig;
|
||||
import com.schisandra.core.core.sdk.PostCurFiledContextAware;
|
||||
import com.schisandra.core.entity.TableInfo;
|
||||
import com.schisandra.core.utils.TableUtils;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 从字段注解中获取数据
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public class ClassPutContextHandler implements PostCurFiledContextAware {
|
||||
|
||||
private Class<?> clazz;
|
||||
|
||||
public ClassPutContextHandler(Class<?> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doAware(GenConfig genConfig, MapperConfig mapperConfig, VelocityContext context) {
|
||||
|
||||
String tableComment = TableUtils.getComment(clazz);
|
||||
List<TableInfo> fields = TableUtils.build(clazz);
|
||||
context.put("tableComment", tableComment);
|
||||
context.put("fields", fields);
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package com.schisandra.core.core.impl;
|
||||
|
||||
import com.schisandra.core.core.sdk.FilePutContextHandler;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class DemoFilePutContextHandler implements FilePutContextHandler {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> putData(String fileId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
package com.schisandra.core.core.impl;
|
||||
|
||||
import com.schisandra.core.config.GenConfig;
|
||||
import com.schisandra.core.core.sdk.PutContextHandler;
|
||||
import com.schisandra.core.entity.TableInfo;
|
||||
import com.schisandra.core.utils.JdbcUtil;
|
||||
import com.schisandra.core.utils.YamlUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 从配置的mysql数据源中获取表字段及表备注写入到全局上下文
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public class JdbcPutContextHandler implements PutContextHandler {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> putData() {
|
||||
|
||||
GenConfig genConfig = YamlUtils.loadYaml("genCode/gen.yml", GenConfig.class);
|
||||
String tableComment = JdbcUtil.getComment(genConfig.getJdbc(), genConfig.getJdbc().getTableName());
|
||||
List<TableInfo> fields = JdbcUtil.queryTableInfo(genConfig.getJdbc(), genConfig.getJdbc().getTableName());
|
||||
Map<String, Object> res = new HashMap<>();
|
||||
res.put("tableComment", tableComment);
|
||||
res.put("fields", fields);
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
package com.schisandra.core.core.sdk;
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 针对某个生成文件写入独有上下文
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public interface FilePutContextHandler {
|
||||
|
||||
/**
|
||||
* 将map中的键值对写入当前文件的上下文
|
||||
*
|
||||
* @param fileId 文件id
|
||||
* @return 写入的数据
|
||||
*/
|
||||
Map<String, Object> putData(String fileId);
|
||||
|
||||
default void put(VelocityContext context, String fileId) {
|
||||
|
||||
Map<String, Object> map = putData(fileId);
|
||||
if (MapUtil.isNotEmpty(map)) {
|
||||
for (Map.Entry<String, Object> entity : map.entrySet()) {
|
||||
context.put(entity.getKey(), entity.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package com.schisandra.core.core.sdk;
|
||||
|
||||
/**
|
||||
* 内置函数
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public interface Function {
|
||||
|
||||
/**
|
||||
* 函数key
|
||||
*/
|
||||
String funcKey();
|
||||
|
||||
/**
|
||||
* 执行函授后的返回值
|
||||
*/
|
||||
String doFunc();
|
||||
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package com.schisandra.core.core.sdk;
|
||||
|
||||
import com.schisandra.core.config.GenConfig;
|
||||
import com.schisandra.core.config.MapperConfig;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
|
||||
/**
|
||||
* 针对当前文件的上下文进行操作
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface PostCurFiledContextAware {
|
||||
|
||||
void doAware(GenConfig genConfig, MapperConfig mapperConfig, VelocityContext context);
|
||||
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package com.schisandra.core.core.sdk;
|
||||
|
||||
import com.schisandra.core.entity.Context;
|
||||
import com.schisandra.core.utils.ConfigUtils;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 从类中写入数据到全局上下文
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public interface PutContextHandler {
|
||||
|
||||
Map<String, Object> putData();
|
||||
|
||||
default boolean put() {
|
||||
|
||||
boolean res = false;
|
||||
Map<String, Object> map = putData();
|
||||
Context context = ConfigUtils.getContext();
|
||||
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
||||
context.put(entry.getKey(), entry.getValue());
|
||||
res = true;
|
||||
}
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,83 @@
|
||||
package com.schisandra.core.entity;
|
||||
|
||||
import com.schisandra.core.utils.PrimitiveTypeUtils;
|
||||
import lombok.Data;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 自定义上下文
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
@Data
|
||||
public class Context implements Cloneable {
|
||||
|
||||
private VelocityContext context = new VelocityContext();
|
||||
|
||||
public VelocityContext get() {
|
||||
for (Map.Entry<String, Object> entry : contexts.entrySet()) {
|
||||
context.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
private Map<String, Object> contexts = new ConcurrentHashMap<>();
|
||||
|
||||
public void put(String key, Object obj) {
|
||||
contexts.put(key, obj);
|
||||
}
|
||||
|
||||
public void remove(String key) {
|
||||
contexts.remove(key);
|
||||
}
|
||||
|
||||
public void put(Object obj) {
|
||||
|
||||
if (PrimitiveTypeUtils.isPrimitive(obj)) {
|
||||
return;
|
||||
}
|
||||
Class<?> aClass = obj.getClass();
|
||||
Field[] fields = aClass.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
Object value = field.get(obj);
|
||||
put(field.getName(), value);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void removeObj(Object obj) {
|
||||
|
||||
if (PrimitiveTypeUtils.isPrimitive(obj)) {
|
||||
return;
|
||||
}
|
||||
Class<?> aClass = obj.getClass();
|
||||
Field[] fields = aClass.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
String name = field.getName();
|
||||
remove(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Context clone() {
|
||||
try {
|
||||
Context clone = (Context) super.clone();
|
||||
// TODO: copy mutable state here, so the clone can't change the internals of the original
|
||||
return clone;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new AssertionError();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,25 @@
|
||||
package com.schisandra.core.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* 数据库配置
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class JdbcConfig {
|
||||
|
||||
private String url;
|
||||
private String username;
|
||||
private String password;
|
||||
private String dbName;
|
||||
private String driver;
|
||||
private String tableName;
|
||||
|
||||
}
|
@@ -0,0 +1,43 @@
|
||||
package com.schisandra.core.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 映射关系
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
@Data
|
||||
public class Mapper {
|
||||
|
||||
/**
|
||||
* 生成文件的id
|
||||
*/
|
||||
private String fileId;
|
||||
|
||||
/**
|
||||
* 模板地址
|
||||
*/
|
||||
private String template;
|
||||
|
||||
/**
|
||||
* 生成类包名称 可使用占位符 ${}
|
||||
*/
|
||||
private String packageName;
|
||||
|
||||
/**
|
||||
* 生成文件的路径 没有指定则用 packageName 进行替换处理 可使用占位符 ${}
|
||||
*/
|
||||
private String filePath;
|
||||
|
||||
/**
|
||||
* 生成文件的名称 可使用占位符 ${}
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 生成文件的扩展名
|
||||
*/
|
||||
private String ext;
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
package com.schisandra.core.entity;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.Data;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 映射关系包裹对象
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
@Data
|
||||
public class MapperInfo {
|
||||
|
||||
/**
|
||||
* 文件id用于自定义忘 context 中存入自己特有的信息
|
||||
*/
|
||||
private String fileId;
|
||||
|
||||
/**
|
||||
* 模板路径
|
||||
*/
|
||||
private String templatePath;
|
||||
|
||||
/**
|
||||
* 生成文件路径
|
||||
*/
|
||||
private String outPutPath;
|
||||
|
||||
/**
|
||||
* 每个文件自个持有的上下文 包含全局的上下文数据
|
||||
*/
|
||||
private VelocityContext context;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
Map<String, Object> res = new HashMap<>();
|
||||
res.put("fileId", fileId);
|
||||
res.put("templatePath", templatePath);
|
||||
res.put("outPutPath", outPutPath);
|
||||
return JSONObject.toJSONString(res);
|
||||
}
|
||||
}
|
@@ -0,0 +1,65 @@
|
||||
package com.schisandra.core.entity;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.schisandra.core.utils.MySQLToJavaTypeConverter;
|
||||
import com.schisandra.core.utils.MySQLToMyBatisTypeConverter;
|
||||
import lombok.Data;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* 表信息
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
@Data
|
||||
public class TableInfo {
|
||||
|
||||
// 字段索引类型
|
||||
private String keyType;
|
||||
private String col;
|
||||
private String name;
|
||||
private String upName;
|
||||
private String comment;
|
||||
private String dataType;
|
||||
private String tableComment;
|
||||
// 转化为java中的数据类型
|
||||
private String type;
|
||||
// 转为mybatis中的数据类型
|
||||
private String myBatisType;
|
||||
|
||||
public String getMyBatisType() {
|
||||
if (StrUtil.isNotEmpty(myBatisType)) {
|
||||
return myBatisType;
|
||||
}
|
||||
return MySQLToMyBatisTypeConverter.convert(dataType);
|
||||
|
||||
}
|
||||
|
||||
public String getUpName() {
|
||||
return StrUtil.upperFirst(name);
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
if (StrUtil.isNotEmpty(type)) {
|
||||
return type;
|
||||
}
|
||||
return MySQLToJavaTypeConverter.convert(dataType).getType();
|
||||
}
|
||||
|
||||
public MySQLToJavaTypeConverter.Item getTypeInfo() {
|
||||
return MySQLToJavaTypeConverter.convert(dataType);
|
||||
}
|
||||
|
||||
public MySQLToJavaTypeConverter.Item getTypeInfo(String type) {
|
||||
return MySQLToJavaTypeConverter.trans(type);
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
if (StringUtils.isEmpty(comment)) {
|
||||
return "";
|
||||
}
|
||||
return comment.trim().replace("\n" , "");
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
package com.schisandra.core.utils;
|
||||
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
import org.apache.velocity.app.VelocityEngine;
|
||||
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* 使用velocity生成文件
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public class CodeGeneratorUtils {
|
||||
|
||||
private final VelocityEngine velocityEngine;
|
||||
|
||||
public CodeGeneratorUtils(String templateBasePath) {
|
||||
this.velocityEngine = new VelocityEngine();
|
||||
if (SystemUtils.isWindows()) {
|
||||
this.velocityEngine.setProperty("file.resource.loader.path", templateBasePath);
|
||||
}
|
||||
velocityEngine.init();
|
||||
|
||||
}
|
||||
|
||||
public void generateCode(VelocityContext context, String templatePath, String outputPath, AtomicInteger count) {
|
||||
|
||||
Template template = velocityEngine.getTemplate(templatePath, "UTF-8");
|
||||
StringWriter writer = new StringWriter();
|
||||
template.merge(context, writer);
|
||||
|
||||
try (FileWriter fileWriter = new FileWriter(outputPath)) {
|
||||
fileWriter.write(writer.toString());
|
||||
System.out.println();
|
||||
System.out.println("==========> " + count.getAndIncrement() + " genFile succeed : " + outputPath);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
package com.schisandra.core.utils;
|
||||
|
||||
import com.schisandra.core.entity.Context;
|
||||
|
||||
/**
|
||||
* 获取全局上下文的配置工具
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public class ConfigUtils {
|
||||
|
||||
private static Context ctx = new Context();
|
||||
|
||||
public static Context getContext() {
|
||||
return ctx;
|
||||
}
|
||||
|
||||
public static void reSetContext(Context context) {
|
||||
ctx = context;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
package com.schisandra.core.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.schisandra.core.entity.Context;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 使用json构建上下文的上下文工具
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public class ContextUtils {
|
||||
|
||||
public static Context buildContext(String json) {
|
||||
|
||||
Context context = new Context();
|
||||
JSONObject jsonObject = JSONObject.parseObject(json);
|
||||
for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
|
||||
context.put(entry.getKey(), entry.getValue());
|
||||
}
|
||||
return context;
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,96 @@
|
||||
package com.schisandra.core.utils;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.schisandra.core.core.sdk.Function;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.security.MessageDigest;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class FunctionUtils implements Serializable {
|
||||
|
||||
private static final Map<String, Function> functionMap = new ConcurrentHashMap<>();
|
||||
|
||||
static {
|
||||
register(new Function() {
|
||||
@Override
|
||||
public String funcKey() {
|
||||
return "now()";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String doFunc() {
|
||||
return DateUtil.now();
|
||||
}
|
||||
});
|
||||
|
||||
register(new Function() {
|
||||
@Override
|
||||
public String funcKey() {
|
||||
return "svUid()";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String doFunc() {
|
||||
try {
|
||||
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
|
||||
objectStream.writeObject(RandomUtil.randomBigDecimal());
|
||||
objectStream.flush();
|
||||
objectStream.close();
|
||||
|
||||
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
||||
byte[] byteArray = md5.digest(byteStream.toByteArray());
|
||||
long serialVersionUID = 0L;
|
||||
for (int i = Math.min(byteArray.length, 8) - 1; i >= 0; i--) {
|
||||
serialVersionUID = (serialVersionUID << 8) | (byteArray[i] & 0xFF);
|
||||
}
|
||||
return Long.toString(serialVersionUID);
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
register(new Function() {
|
||||
@Override
|
||||
public String funcKey() {
|
||||
return "date()";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String doFunc() {
|
||||
return DateUtil.formatDate(DateUtil.date());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
public static boolean isFunction(Object funKey) {
|
||||
if (!(funKey instanceof String)) {
|
||||
return false;
|
||||
}
|
||||
return functionMap.containsKey(funKey);
|
||||
}
|
||||
|
||||
public static void register(Function function) {
|
||||
functionMap.put(function.funcKey(), function);
|
||||
}
|
||||
|
||||
public static Object doFunction(Object funKey) {
|
||||
if (!(funKey instanceof String)) {
|
||||
return funKey;
|
||||
}
|
||||
Function function = functionMap.get(funKey);
|
||||
if (Objects.isNull(function)) {
|
||||
return funKey;
|
||||
}
|
||||
return function.doFunc();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
package com.schisandra.core.utils;
|
||||
|
||||
import com.schisandra.core.core.MysqlDataHandler;
|
||||
import com.schisandra.core.entity.JdbcConfig;
|
||||
import com.schisandra.core.entity.TableInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* jdbc工具 用于查询表字段信息 及 表结构信息
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public class JdbcUtil {
|
||||
|
||||
private JdbcUtil() {
|
||||
}
|
||||
|
||||
public static List<TableInfo> queryTableInfo(JdbcConfig config, String table) {
|
||||
|
||||
return new MysqlDataHandler(table, config.getUrl(), config.getUsername(),
|
||||
config.getPassword(), config.getDbName(), config.getDriver())
|
||||
.searchByDb();
|
||||
|
||||
}
|
||||
|
||||
public static String getComment(JdbcConfig config, String table) {
|
||||
|
||||
return new MysqlDataHandler(table, config.getUrl(), config.getUsername(),
|
||||
config.getPassword(), config.getDbName(), config.getDriver())
|
||||
.getTableComment();
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package com.schisandra.core.utils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 集合类
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public class Lists {
|
||||
|
||||
public static List<Object> newArrayList() {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,64 @@
|
||||
package com.schisandra.core.utils;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* mysql转java转换器
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public class MySQLToJavaTypeConverter {
|
||||
private static final Map<String, Item> typeMap = new HashMap<>();
|
||||
|
||||
static {
|
||||
typeMap.put("tinyint", new Item(false, "Integer"));
|
||||
typeMap.put("smallint", new Item(false, "Short"));
|
||||
typeMap.put("mediumint", new Item(false, "Integer"));
|
||||
typeMap.put("int", new Item(false, "Integer"));
|
||||
typeMap.put("bigint", new Item(false, "Long"));
|
||||
typeMap.put("float", new Item(false, "Float"));
|
||||
typeMap.put("double", new Item(false, "Double"));
|
||||
typeMap.put("char", new Item(false, "String"));
|
||||
typeMap.put("varchar", new Item(false, "String"));
|
||||
typeMap.put("text", new Item(false, "String"));
|
||||
typeMap.put("longtext", new Item(false, "String"));
|
||||
typeMap.put("blob", new Item(false, "Byte"));
|
||||
typeMap.put("decimal", new Item(true, "BigDecimal"));
|
||||
typeMap.put("date", new Item(true, "Date"));
|
||||
typeMap.put("time", new Item(true, "Date"));
|
||||
typeMap.put("datetime", new Item(true, "Date"));
|
||||
typeMap.put("timestamp", new Item(true, "Date"));
|
||||
}
|
||||
|
||||
public static Item convert(String mysqlType) {
|
||||
Item item = typeMap.get(mysqlType.toLowerCase());
|
||||
if (Objects.isNull(item)) {
|
||||
return new Item(false, mysqlType);
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
public static Item trans(String type) {
|
||||
for (Item value : typeMap.values()) {
|
||||
if (value.type.equals(type)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return new Item(false, type);
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public static class Item {
|
||||
private boolean flag;
|
||||
private String type;
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
package com.schisandra.core.utils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MySQLToMyBatisTypeConverter {
|
||||
private static final Map<String, String> typeMap = new HashMap<>();
|
||||
|
||||
static {
|
||||
// 添加MySQL数据类型到MyBatis数据类型的映射关系
|
||||
typeMap.put("VARCHAR", "VARCHAR");
|
||||
typeMap.put("CHAR", "CHAR");
|
||||
typeMap.put("TEXT", "VARCHAR");
|
||||
typeMap.put("LONGTEXT", "VARCHAR");
|
||||
typeMap.put("INT", "INTEGER");
|
||||
typeMap.put("TINYINT", "TINYINT");
|
||||
typeMap.put("SMALLINT", "SMALLINT");
|
||||
typeMap.put("MEDIUMINT", "INTEGER");
|
||||
typeMap.put("BIGINT", "BIGINT");
|
||||
typeMap.put("FLOAT", "REAL");
|
||||
typeMap.put("DOUBLE", "DOUBLE");
|
||||
typeMap.put("DECIMAL", "DECIMAL");
|
||||
typeMap.put("DATE", "DATE");
|
||||
typeMap.put("TIME", "TIME");
|
||||
typeMap.put("DATETIME", "TIMESTAMP");
|
||||
typeMap.put("TIMESTAMP", "TIMESTAMP");
|
||||
typeMap.put("YEAR", "DATE");
|
||||
}
|
||||
|
||||
public static String convert(String mysqlType) {
|
||||
return typeMap.getOrDefault(mysqlType.toUpperCase(), "VARCHAR");
|
||||
}
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package com.schisandra.core.utils;
|
||||
|
||||
/**
|
||||
* 判断是否是基本类型工具类
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public class PrimitiveTypeUtils {
|
||||
|
||||
public static boolean isPrimitive(Object obj) {
|
||||
return obj instanceof Integer ||
|
||||
obj instanceof Long ||
|
||||
obj instanceof Float ||
|
||||
obj instanceof Double ||
|
||||
obj instanceof Boolean ||
|
||||
obj instanceof Character ||
|
||||
obj instanceof Byte;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
package com.schisandra.core.utils;
|
||||
|
||||
public class SystemUtils {
|
||||
|
||||
/**
|
||||
* 判断操作系统是否是 Windows
|
||||
*
|
||||
* @return true:操作系统是 Windows
|
||||
* false:其它操作系统
|
||||
*/
|
||||
public static boolean isWindows() {
|
||||
String osName = getOsName();
|
||||
|
||||
return osName != null && osName.startsWith("Windows");
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断操作系统是否是 MacOS
|
||||
*
|
||||
* @return true:操作系统是 MacOS
|
||||
* false:其它操作系统
|
||||
*/
|
||||
public static boolean isMacOs() {
|
||||
String osName = getOsName();
|
||||
|
||||
return osName != null && osName.startsWith("Mac");
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断操作系统是否是 Linux
|
||||
*
|
||||
* @return true:操作系统是 Linux
|
||||
* false:其它操作系统
|
||||
*/
|
||||
public static boolean isLinux() {
|
||||
String osName = getOsName();
|
||||
|
||||
return (osName != null && osName.startsWith("Linux")) || (!isWindows() && !isMacOs());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取操作系统名称
|
||||
*
|
||||
* @return os.name 属性值
|
||||
*/
|
||||
public static String getOsName() {
|
||||
return System.getProperty("os.name");
|
||||
}
|
||||
}
|
@@ -0,0 +1,46 @@
|
||||
package com.schisandra.core.utils;
|
||||
|
||||
import com.schisandra.core.anno.Table;
|
||||
import com.schisandra.core.entity.TableInfo;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class TableUtils {
|
||||
|
||||
private TableUtils() {
|
||||
}
|
||||
|
||||
public static String getComment(Class<?> clazz) {
|
||||
Table table = clazz.getAnnotation(Table.class);
|
||||
if (Objects.isNull(table)) {
|
||||
return "";
|
||||
}
|
||||
return table.value();
|
||||
}
|
||||
|
||||
public static List<TableInfo> build(Class<?> clazz) {
|
||||
|
||||
String comment = getComment(clazz);
|
||||
return Arrays.stream(clazz.getDeclaredFields()).filter(item -> {
|
||||
Table table = item.getAnnotation(Table.class);
|
||||
return Objects.nonNull(table);
|
||||
}).map(item -> {
|
||||
Table table = item.getAnnotation(Table.class);
|
||||
TableInfo info = new TableInfo();
|
||||
info.setKeyType(table.keyType());
|
||||
info.setCol(table.col());
|
||||
info.setTableComment(comment);
|
||||
info.setType(table.jdbcType());
|
||||
info.setMyBatisType(table.myBatisType());
|
||||
info.setName(item.getName());
|
||||
info.setComment(table.value());
|
||||
info.setDataType(item.getType().getSimpleName());
|
||||
return info;
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
package com.schisandra.core.utils;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||
import com.schisandra.core.core.CodeGenerationCode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
* yml 转对象工具类
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023/9/4
|
||||
*/
|
||||
public class YamlUtils {
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
|
||||
|
||||
public static <T> T loadYaml(String filePath, Class<T> valueType) {
|
||||
try {
|
||||
ClassLoader classLoader = CodeGenerationCode.class.getClassLoader();
|
||||
URL resource = classLoader.getResource(filePath);
|
||||
InputStream inputStream = resource.openStream();
|
||||
return objectMapper.readValue(inputStream, valueType);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"fields": [
|
||||
{
|
||||
"type": "int",
|
||||
"name": "Element1"
|
||||
},
|
||||
{
|
||||
"type": "boolean",
|
||||
"name": "Element2"
|
||||
},
|
||||
{
|
||||
"type": "byte",
|
||||
"name": "Element3"
|
||||
},
|
||||
{
|
||||
"type": "double",
|
||||
"name": "Element4"
|
||||
},
|
||||
{
|
||||
"type": "float",
|
||||
"name": "Element5"
|
||||
},
|
||||
{
|
||||
"type": "short",
|
||||
"name": "Element6"
|
||||
},
|
||||
{
|
||||
"type": "int",
|
||||
"name": "Element7"
|
||||
},
|
||||
{
|
||||
"type": "char",
|
||||
"name": "Element8"
|
||||
},
|
||||
{
|
||||
"type": "boolean",
|
||||
"name": "Element9"
|
||||
},
|
||||
{
|
||||
"type": "long",
|
||||
"name": "Element10"
|
||||
}
|
||||
]
|
||||
}
|
@@ -0,0 +1,28 @@
|
||||
# 01 数据源输入 填充到全局的context中
|
||||
# 1.1 通过指定类的返回值写入 (优先级第一)
|
||||
#handler: com.loser.core.impl.JdbcPutContextHandler
|
||||
# 1.2 直接从classPath中引入一个json数据 可配合 mapperInfos 替换模板 (优先级第二)
|
||||
json: genCode/fields.json
|
||||
# 1.3 配置数据库信息从表字段中获取数据 (优先级第三)
|
||||
#jdbc:
|
||||
# url: jdbc:mysql://119.91.155.174:3306/
|
||||
# username: root
|
||||
# password: 6666888123
|
||||
# dbName: ape
|
||||
# driver: com.mysql.cj.jdbc.Driver
|
||||
# tableName: sys_user
|
||||
|
||||
# 02 模板与生成类的映射关系 通过修改该配置 切换不同的生成模板 (比如从基础的crud代码模板 切换到 rpc代码模板等)
|
||||
mapperInfos: genCode/mapper.yml
|
||||
|
||||
# 03 针对某个生成文件写入独有上下文 通过 mapper.yml > mappers > fileId 关联
|
||||
#filePutHandler: com.loser.core.impl.DemoFilePutContextHandler
|
||||
|
||||
# 04 全局上下文参数
|
||||
params:
|
||||
pre: Loser
|
||||
module: test
|
||||
author: loser
|
||||
# 内置函数:now()-获取当前时间;date()-获取当前时间不带时分秒;svUid()-生成序列化ID;
|
||||
# 扩展新的函数 则在调用生成方法前 使用 FunctionUtils.register(Function function)
|
||||
date: now()
|
@@ -0,0 +1,74 @@
|
||||
# 生成到当前项目的那个子模块,没有则配置 /
|
||||
module: /ape-demo/
|
||||
|
||||
# 模板文件和生成类的映射关系 多个文件 数组形式配置
|
||||
mappers:
|
||||
-
|
||||
- fileId: 001
|
||||
template: genCode/template/ReqAndRespTemplate.java.vm
|
||||
# 生成到 src.main.java 下的对应包中
|
||||
packageName: com.loser.user.${module}.vo.resp
|
||||
name: ${pre}InfoResp
|
||||
ext: java
|
||||
|
||||
# fileId 用于实现 FilePutContextHandler 接口时 忘对应文件的上下文补充数据
|
||||
- fileId: 002
|
||||
template: genCode/template/ReqAndRespTemplate.java.vm
|
||||
# 优先级比 packageName 使用场景为需要指定目录的文件比如resources下的 mapper 文件等
|
||||
filePath: src/main/resources/${module}/template
|
||||
name: ${pre}InfoResp
|
||||
ext: java.vm
|
||||
|
||||
- fileId: 003
|
||||
template: genCode/template/EntityTemplate.java.vm
|
||||
packageName: com.loser.user.${module}.entity
|
||||
name: ${pre}
|
||||
ext: java
|
||||
|
||||
- fileId: 004
|
||||
template: genCode/template/ReqAndRespTemplate.java.vm
|
||||
packageName: com.loser.user.${module}.vo.req
|
||||
name: ${pre}Req
|
||||
ext: java
|
||||
|
||||
- fileId: 005
|
||||
template: genCode/template/ReqAndRespTemplate.java.vm
|
||||
packageName: com.loser.user.${module}.vo.resp
|
||||
name: ${pre}Resp
|
||||
ext: java
|
||||
|
||||
- fileId: 006
|
||||
template: genCode/template/ServiceTemplate.java.vm
|
||||
packageName: com.loser.user.${module}.service
|
||||
name: ${pre}Service
|
||||
ext: java
|
||||
|
||||
- fileId: 007
|
||||
template: genCode/template/ServiceImplTemplate.java.vm
|
||||
packageName: com.loser.user.${module}.service.impl
|
||||
name: ${pre}ServiceImpl
|
||||
ext: java
|
||||
|
||||
- fileId: 008
|
||||
template: genCode/template/HandlerTemplate.java.vm
|
||||
packageName: com.loser.user.${module}.handler
|
||||
name: ${pre}Handler
|
||||
ext: java
|
||||
|
||||
- fileId: 009
|
||||
template: genCode/template/PageReqTemplate.java.vm
|
||||
packageName: com.loser.user.${module}.vo.req
|
||||
name: ${pre}PageReq
|
||||
ext: java
|
||||
|
||||
- fileId: 010
|
||||
template: genCode/template/ReqAndRespTemplate.java.vm
|
||||
packageName: com.loser.user.${module}.vo.req
|
||||
name: ${pre}SaveReq
|
||||
ext: java
|
||||
|
||||
- fileId: 011
|
||||
template: genCode/template/ReqAndRespTemplate.java.vm
|
||||
packageName: com.loser.user.${module}.vo.req
|
||||
name: ${pre}UpdateReq
|
||||
ext: java
|
@@ -0,0 +1,19 @@
|
||||
package ${packageName};
|
||||
|
||||
#if($importDate)
|
||||
import java.util.Date;
|
||||
#end
|
||||
|
||||
/**
|
||||
* ${tableComment}实体
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
*/
|
||||
public class ${className} {
|
||||
#foreach($field in $fields)
|
||||
|
||||
private $field.type $field.name;
|
||||
#end
|
||||
|
||||
}
|
@@ -0,0 +1,84 @@
|
||||
package ${packageName};
|
||||
|
||||
import com.loser.bean.Result;
|
||||
import com.loser.user.mongo.ParamsUtil;
|
||||
import com.loser.user.${module}.entity.${pre};
|
||||
import com.loser.user.${module}.service.${pre}Service;
|
||||
import com.loser.user.${module}.vo.req.${pre}PageReq;
|
||||
import com.loser.user.${module}.vo.req.${pre}SaveReq;
|
||||
import com.loser.user.${module}.vo.req.${pre}UpdateReq;
|
||||
import com.loser.user.${module}.vo.resp.${pre}InfoResp;
|
||||
import com.loser.core.entity.Page;
|
||||
import com.loser.core.wrapper.LambdaQueryWrapper;
|
||||
import com.loser.core.wrapper.Wrappers;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 测试数据 处理器
|
||||
*
|
||||
* @author loser
|
||||
* @date 2023-06-13
|
||||
*/
|
||||
@Component
|
||||
public class ${pre}Handler {
|
||||
|
||||
@Autowired
|
||||
private ${pre}Service ${pre}Service;
|
||||
|
||||
/**
|
||||
* 新增测试数据
|
||||
*/
|
||||
public Result<Boolean> save(${pre}SaveReq req) {
|
||||
|
||||
${pre} save = ParamsUtil.copyProperties(req, ${pre}.class);
|
||||
return Result.ok(${pre}Service.save(save));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改测试数据
|
||||
*/
|
||||
public Result<Boolean> update(${pre}UpdateReq req) {
|
||||
|
||||
${pre} update = ParamsUtil.copyProperties(req, ${pre}.class);
|
||||
return Result.ok(${pre}Service.updateById(update));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过id删除测试数据
|
||||
*/
|
||||
public Result<Boolean> deleteById(String id) {
|
||||
|
||||
return Result.ok(${pre}Service.removeById(id));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过id获取测试数据
|
||||
*/
|
||||
public Result<${pre}InfoResp> getById(String id) {
|
||||
|
||||
${pre} dbData = ${pre}Service.getById(id);
|
||||
if (Objects.isNull(dbData)) {
|
||||
return Result.ok();
|
||||
}
|
||||
return Result.ok(ParamsUtil.copyProperties(dbData, ${pre}InfoResp.class));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页获取测试数据
|
||||
*/
|
||||
public Result<Page<${pre}>> queryList(${pre}PageReq req) {
|
||||
|
||||
LambdaQueryWrapper<${pre}> query = Wrappers.<${pre}>lambdaQuery();
|
||||
Page<${pre}> page = ${pre}Service.page(query, req.getPageNo(), req.getPageSize());
|
||||
return Result.ok(page);
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package ${packageName};
|
||||
|
||||
#if($importDate)
|
||||
import java.util.Date;
|
||||
#end
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* ${tableComment}实体
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
*/
|
||||
@Data
|
||||
public class ${className} {
|
||||
|
||||
private Integer pageNo = 1;
|
||||
|
||||
private Integer pageSize = 20;
|
||||
|
||||
#foreach($field in $fields)
|
||||
|
||||
private $field.type $field.name;
|
||||
#end
|
||||
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package ${packageName};
|
||||
|
||||
#if($importDate)
|
||||
import java.util.Date;
|
||||
#end
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* ${tableComment}试图
|
||||
*
|
||||
* @author ${author}
|
||||
* @date ${date}
|
||||
*/
|
||||
@Data
|
||||
public class $className {
|
||||
#foreach($field in $fields)
|
||||
|
||||
private $field.type $field.name;
|
||||
#end
|
||||
|
||||
}
|
@@ -0,0 +1,11 @@
|
||||
package ${packageName};
|
||||
|
||||
import com.loser.user.${module}.entity.${pre};
|
||||
import com.loser.user.${module}.service.${pre}Service;
|
||||
import com.loser.core.impl.EasyMongoServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ${pre}ServiceImpl extends EasyMongoServiceImpl<String, ${pre}> implements ${pre}Service {
|
||||
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
package ${packageName};
|
||||
|
||||
import com.loser.user.${module}.entity.${pre};
|
||||
import com.loser.core.sdk.EasyMongoService;
|
||||
|
||||
public interface ${pre}Service extends EasyMongoService<String, ${pre}> {
|
||||
|
||||
}
|
44
schisandra-cloud-storage-common/pom.xml
Normal file
44
schisandra-cloud-storage-common/pom.xml
Normal file
@@ -0,0 +1,44 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.schisandra</groupId>
|
||||
<artifactId>schisandra-cloud-storage-common</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<name>schisandra-cloud-storage-common-starter</name>
|
||||
<modules>
|
||||
<module>easy-gen-code-spring-boot-starter</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>2.4.2</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>central</id>
|
||||
<name>aliyun maven</name>
|
||||
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
|
||||
<layout>default</layout>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
Reference in New Issue
Block a user