feat: 添加代码生成器

This commit is contained in:
landaiqing
2024-05-11 21:52:18 +08:00
parent fce3423ad7
commit 69a589fe5c
108 changed files with 2606 additions and 69 deletions

View File

@@ -0,0 +1,58 @@
import cn.hutool.core.convert.Convert;
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.sdk.PostCurFiledContextAware;
import com.schisandra.core.entity.TableInfo;
import com.schisandra.core.utils.MySQLToJavaTypeConverter;
import org.apache.velocity.VelocityContext;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BizPutContextHandler implements PostCurFiledContextAware {
public static String underlineToHump(String str, boolean removePre) {
if (removePre) {
str = str.substring(str.indexOf("_"));
}
str = str.toLowerCase();
Pattern compile = Pattern.compile("_[a-z]");
Matcher matcher = compile.matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(0).toUpperCase().replace("_", ""));
}
matcher.appendTail(sb);
return StrUtil.upperFirst(sb.toString());
}
private static String convert(String str) {
if (str == null || str.isEmpty()) {
return str;
}
char firstChar = str.charAt(0);
char lowerFirstChar = Character.toLowerCase(firstChar);
return lowerFirstChar + str.substring(1);
}
@Override
public void doAware(GenConfig genConfig, MapperConfig mapperConfig, VelocityContext context) {
// 处理导包标志位
List<TableInfo> files = JSONObject.parseArray(JSONObject.toJSONString(context.get("fields")), TableInfo.class);
for (TableInfo tableInfo : files) {
MySQLToJavaTypeConverter.Item typeInfo = tableInfo.getTypeInfo(tableInfo.getType());
if (typeInfo.isFlag()) {
context.put("import" + typeInfo.getType(), true);
}
}
context.put("modelName", underlineToHump(genConfig.getJdbc().getTableName(), Convert.toBool(context.get("removePre"))));
context.put("_modelName", convert(context.get("modelName").toString()));
context.put("tableName", genConfig.getJdbc().getTableName());
}
}

View File

@@ -0,0 +1,9 @@
import com.schisandra.core.core.CodeGenerationCode;
public class GenCodeMain {
public static void main(String[] args) {
CodeGenerationCode.doGenCode(new BizPutContextHandler());
}
}